Skip to content

Commit

Permalink
Merge pull request #16 from veghtomi/tamas-vegh-add-other-shimmer-wit…
Browse files Browse the repository at this point in the history
…h-other-features

Add other shimmer implementation and remove application attributes from manifest
  • Loading branch information
sharish authored Aug 21, 2017
2 parents bdbea4b + d279ee8 commit 83d412a
Show file tree
Hide file tree
Showing 31 changed files with 633 additions and 1,254 deletions.
13 changes: 0 additions & 13 deletions FACEBOOK_LICENSE.md

This file was deleted.

36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,42 @@ A custom recycler view with shimmer views to indicate that views are loading. Th


Demo Screen
------
------

There are two kinds of shimmer animation which you can see here:

1. This type of shimmer effect uses the whole ViewHolder item to animate on.

| List Demo | Grid Demo |
| ---------------------------- | ----------------------------- |
| <img src='screenshots/list_demo.gif' height=444 width=250 /> | <img src='screenshots/grid_demo.gif' height=444 width=250 /> |

2. Here the shimmer effect only applied on for those views which background color is nontransparent.

| List Demo | Grid Demo |
| ---------------------------- | ----------------------------- |
| <img src='screenshots/second_list_demo.gif' height=444 width=250 /> | <img src='screenshots/second_grid_demo.gif' height=444 width=250 /> |


### Shimmer effect types

1. As you can see the first demo examples show that the whole ViewHolder item is animated. To achieve the desired effect, the children of the ShimmerLayout should have a nontransparent background.
2. You can achieve the second kind of shimmer effect by adding only one ViewGroup child to the ShimmerLayout with a transparent background. This ViewGroup will have the other views with nontransparent backgrounds on which the effect will be seen.

You may wonder how can you add background to the root view of the ViewHolder, if you do not have direct access to the ShimmerLayout and the only child has a nontransparent background. The solution for this is to use the `shimmer_demo_view_holder_item_background` attribute.

### Attributes and Methods

Following are the attributes and methods to initialise the demo views.

| XML Attributes | Java Methods | Explanation |
| ------------- | ------------ | ----------- |
|```app:demo_child_count``` | ```setDemoChildCount(int)``` | Integer value that sets the number of demo views should be present in shimmer adapter |
|```app:demo_layout``` | ```setDemoLayoutReference(int)``` | Layout reference to your demo view. Define your my_demo_view.xml and refer the layout reference here. |
|```app:demo_layout_manager_type``` | ```setDemoLayoutManager(LayoutManagerType)``` | Layout manager of demo view. Can be one among linear_veritical or linear_horizontal or grid. |
|```app:shimmer_demo_child_count``` | ```setDemoChildCount(int)``` | Integer value that sets the number of demo views should be present in shimmer adapter. |
|```app:shimmer_demo_layout``` | ```setDemoLayoutReference(int)``` | Layout reference to your demo view. Define your my_demo_view.xml and refer the layout reference here. |
|```app:shimmer_demo_layout_manager_type``` | ```setDemoLayoutManager(LayoutManagerType)``` | Layout manager of demo view. Can be one among linear_vertical or linear_horizontal or grid. |
|```app:shimmer_demo_shimmer_color``` | ``` - ``` | Color reference or value. It can be used to change the color of the shimmer line. |
|```app:shimmer_demo_angle``` | ``` - ``` | Integer value between 0 and 30 which can modify the angle of the shimmer line. The default value is zero. |
|```app:shimmer_demo_view_holder_item_background``` | ``` - ``` | Color or an xml drawable for the ViewHolder background if you want to achieve the second type of shimmer effect. |



Expand All @@ -44,6 +65,7 @@ Define your xml as:
app:demo_grid_child_count="2"
app:demo_layout="@layout/layout_demo_grid"
app:demo_layout_manager_type="grid"
app:shimmer_demo_angle="20"
/>

```
Expand Down Expand Up @@ -76,17 +98,17 @@ Developed By
* Harish Sridharan - <harish.sridhar@gmail.com>


Acknowledgements
Used libraries
----------------

* Facebook for <a href="https://github.com/facebook/shimmer-android">Shimmer Android</a> which lies as a base for this repo.
* <a href="https://github.com/team-supercharge/ShimmerLayout">ShimmerLayout</a>: the library which achieves the shimmer effect in a memory efficient way.

License
--------
The repo is released under following licenses

<a href="LICENSE.md">Apache License</a> for ShimmerRecycler<br>
<a href="FACEBOOK_LICENSE.md">BSD License</a> for Facebook's Shimmer-Android
<a href="https://github.com/team-supercharge/ShimmerLayout/blob/master/LICENSE.md">Apache License</a> for ShimmerLayout



Expand Down
29 changes: 11 additions & 18 deletions app/src/main/java/com/cooltechworks/sample/DemoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.cooltechworks.sample.adapters.CardAdapter;
import com.cooltechworks.sample.utils.BaseUtils;
import com.cooltechworks.sample.utils.DemoConfiguration;
import com.cooltechworks.views.shimmer.ShimmerRecyclerView;

public class DemoActivity extends AppCompatActivity {
Expand All @@ -37,26 +36,21 @@ public class DemoActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

int type = getType();
final int type = getType();

RecyclerView.LayoutManager layoutManager;

if (type == BaseUtils.TYPE_LIST) {
setTheme(R.style.AppTheme);
setContentView(R.layout.activity_list);
layoutManager = new LinearLayoutManager(this);
setTitle(R.string.ab_list_title);

} else {
setTheme(R.style.AppThemeGrid);
setContentView(R.layout.activity_grid);
layoutManager = new GridLayoutManager(this, 2);
setTitle(R.string.ab_grid_title);

}
final DemoConfiguration demoConfiguration = BaseUtils.getDemoConfiguration(type, this);
setTheme(demoConfiguration.getStyleResource());
setContentView(demoConfiguration.getLayoutResource());
layoutManager = demoConfiguration.getLayoutManager();
setTitle(demoConfiguration.getTitleResource());

shimmerRecycler = (ShimmerRecyclerView) findViewById(R.id.shimmer_recycler_view);

if (demoConfiguration.getItemDecoration() != null) {
shimmerRecycler.addItemDecoration(demoConfiguration.getItemDecoration());
}

mAdapter = new CardAdapter();
mAdapter.setType(type);
Expand All @@ -70,8 +64,7 @@ protected void onCreate(Bundle savedInstanceState) {
public void run() {
loadCards();
}
}, 2000);

}, 3000);
}

private void loadCards() {
Expand Down
28 changes: 22 additions & 6 deletions app/src/main/java/com/cooltechworks/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.cooltechworks.sample.utils.BaseUtils;

Expand All @@ -29,17 +30,32 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Button firstListDemoButton = (Button) findViewById(R.id.list_demo_button);
final Button firstGridDemoButton = (Button) findViewById(R.id.grid_demo_button);

createClickListener(firstListDemoButton, BaseUtils.TYPE_LIST);
createClickListener(firstGridDemoButton, BaseUtils.TYPE_GRID);

final Button secondListDemoButton = (Button) findViewById(R.id.list_second_demo_button);
final Button secondGridDemoButton = (Button) findViewById(R.id.grid_second_demo_button);

createClickListener(secondListDemoButton, BaseUtils.TYPE_SECOND_LIST);
createClickListener(secondGridDemoButton, BaseUtils.TYPE_SECOND_GRID);
}

public void listButtonClick(View v) {
Intent intent = new Intent(this, DemoActivity.class);
intent.putExtra(DemoActivity.EXTRA_TYPE, BaseUtils.TYPE_LIST);
startActivity(intent);
private void createClickListener(Button button, final int demoType) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startDemo(demoType);
}
});
}

public void gridButtonClick(View v) {
private void startDemo(int demoType) {
Intent intent = new Intent(this, DemoActivity.class);
intent.putExtra(DemoActivity.EXTRA_TYPE, BaseUtils.TYPE_GRID);
intent.putExtra(DemoActivity.EXTRA_TYPE, demoType);
startActivity(intent);
}
}
66 changes: 64 additions & 2 deletions app/src/main/java/com/cooltechworks/sample/utils/BaseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@
*/
package com.cooltechworks.sample.utils;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;

import com.cooltechworks.sample.R;
import com.cooltechworks.sample.models.ItemCard;
import com.cooltechworks.sample.utils.view.CardPaddingItemDecoration;

/**
* Created by sharish on 27/12/16.
*/

public class BaseUtils {

public static final int TYPE_LIST = 0, TYPE_GRID = 1;
public static final int TYPE_LIST = 0;
public static final int TYPE_GRID = 1;
public static final int TYPE_SECOND_LIST = 2;
public static final int TYPE_SECOND_GRID = 3;

private static ItemCard[] getListCards(Resources resources) {

Expand Down Expand Up @@ -115,6 +122,61 @@ private static ItemCard[] getGridCards(Resources resources) {
}

public static ItemCard[] getCards(Resources resources, int type) {
return type == TYPE_LIST ? getListCards(resources) : getGridCards(resources);
ItemCard[] itemCards;

switch (type) {
case TYPE_LIST:
case TYPE_SECOND_LIST:
itemCards = getListCards(resources);
break;
case TYPE_GRID:
case TYPE_SECOND_GRID:
itemCards = getGridCards(resources);
break;
default:
itemCards = null;
}

return itemCards;
}

public static DemoConfiguration getDemoConfiguration(int configurationType, Context context) {
DemoConfiguration demoConfiguration;

switch (configurationType) {
case TYPE_LIST:
demoConfiguration = new DemoConfiguration();
demoConfiguration.setStyleResource(R.style.AppTheme);
demoConfiguration.setLayoutResource(R.layout.activity_list);
demoConfiguration.setLayoutManager(new LinearLayoutManager(context));
demoConfiguration.setTitleResource(R.string.ab_list_title);
break;
case TYPE_GRID:
demoConfiguration = new DemoConfiguration();
demoConfiguration.setStyleResource(R.style.AppThemeGrid);
demoConfiguration.setLayoutResource(R.layout.activity_grid);
demoConfiguration.setLayoutManager(new GridLayoutManager(context, 2));
demoConfiguration.setTitleResource(R.string.ab_grid_title);
break;
case TYPE_SECOND_LIST:
demoConfiguration = new DemoConfiguration();
demoConfiguration.setStyleResource(R.style.AppTheme);
demoConfiguration.setLayoutResource(R.layout.activity_second_list);
demoConfiguration.setLayoutManager(new LinearLayoutManager(context));
demoConfiguration.setTitleResource(R.string.ab_list_title);
demoConfiguration.setItemDecoration(new CardPaddingItemDecoration(context));
break;
case TYPE_SECOND_GRID:
demoConfiguration = new DemoConfiguration();
demoConfiguration.setStyleResource(R.style.AppThemeGrid);
demoConfiguration.setLayoutResource(R.layout.activity_second_grid);
demoConfiguration.setLayoutManager(new GridLayoutManager(context, 2));
demoConfiguration.setTitleResource(R.string.ab_grid_title);
break;
default:
demoConfiguration = null;
}

return demoConfiguration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.cooltechworks.sample.utils;


import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.support.v7.widget.RecyclerView;

public class DemoConfiguration {
@StyleRes
private int styleResource;

@LayoutRes
private int layoutResource;

@StringRes
private int titleResource;

private RecyclerView.LayoutManager layoutManager;

private RecyclerView.ItemDecoration itemDecoration;

public RecyclerView.ItemDecoration getItemDecoration() {
return itemDecoration;
}

public void setItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
this.itemDecoration = itemDecoration;
}

public int getStyleResource() {
return styleResource;
}

public void setStyleResource(int styleResource) {
this.styleResource = styleResource;
}

public int getLayoutResource() {
return layoutResource;
}

public void setLayoutResource(int layoutResource) {
this.layoutResource = layoutResource;
}

public int getTitleResource() {
return titleResource;
}

public void setTitleResource(int titleResource) {
this.titleResource = titleResource;
}

public RecyclerView.LayoutManager getLayoutManager() {
return layoutManager;
}

public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
this.layoutManager = layoutManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.cooltechworks.sample.utils.view;


import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

public class CardPaddingItemDecoration extends RecyclerView.ItemDecoration {

private int paddingBetweenItems;

public CardPaddingItemDecoration(Context context) {
this.paddingBetweenItems = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, context.getResources().getDisplayMetrics());
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(0, 0, 0, paddingBetweenItems);
}
}
Loading

0 comments on commit 83d412a

Please sign in to comment.