Created
June 22, 2016 17:08
-
-
Save shakil807g/fa02f533e37c1be9f5ff6d33db1c8b3d to your computer and use it in GitHub Desktop.
Recycleview Setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BaseAdapter< T extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<T> { | |
protected static final int ITEM_VIEW_TYPE_ITEM = 1; | |
protected boolean animateItemsOnScroll = true; | |
protected int defaultItemAnimationDuration = 0; | |
protected int lastAnimatedPosition = -1; | |
protected long nextAnimationStartTime; | |
protected static final int ANIMATION_DELAY_INTERVAL = 50; | |
protected final Handler handler = new Handler(Looper.getMainLooper()); | |
public Activity mactivity; | |
BaseAdapter(Activity activity){ | |
mactivity = activity; | |
if (defaultItemAnimationDuration == 0) { | |
defaultItemAnimationDuration = mactivity.getResources().getInteger(android.R.integer.config_mediumAnimTime); | |
} | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return ITEM_VIEW_TYPE_ITEM; | |
} | |
protected AnimationDirection getAnimationDirection() { | |
return AnimationDirection.UpFromBottom; | |
} | |
protected void runAnimation(final RecyclerView.ViewHolder targetViewHolder, | |
final int position, | |
final int duration, | |
final AnimationDirection animationDirection) { | |
if (animateItemsOnScroll) { | |
final float maxAlpha = 1f; | |
final View targetView = targetViewHolder.itemView; | |
// Don't actually run the animation right a way. This gives a nice effect | |
// when adding a large batch of items. | |
if (position > lastAnimatedPosition) { | |
int delay = 0; | |
long currTime = System.currentTimeMillis(); | |
if (currTime < nextAnimationStartTime + ANIMATION_DELAY_INTERVAL) { | |
delay = (int) ((nextAnimationStartTime + ANIMATION_DELAY_INTERVAL) - currTime); | |
} | |
nextAnimationStartTime = currTime + delay; | |
targetView.setAlpha(0); | |
switch (animationDirection) { | |
case UpFromBottom: | |
targetView.setTranslationY(500.0f); | |
break; | |
case DownFromTop: | |
targetView.setTranslationY(-500.0f); | |
break; | |
case InFromLeft: | |
targetView.setTranslationX(500.0f); | |
break; | |
case InFromRight: | |
targetView.setTranslationX(-500.0f); | |
break; | |
} | |
handler.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
switch (animationDirection) { | |
case DownFromTop: | |
case UpFromBottom: | |
targetView.animate().alpha(maxAlpha).translationY(0).setDuration(duration); | |
break; | |
case InFromRight: | |
case InFromLeft: | |
targetView.animate().alpha(maxAlpha).translationX(0).setDuration(duration); | |
break; | |
} | |
targetView.animate().setInterpolator(new LinearOutSlowInInterpolator()); | |
targetView.animate().start(); | |
} | |
}, delay); | |
lastAnimatedPosition = position; | |
} | |
} | |
} | |
public enum AnimationDirection { | |
UpFromBottom, | |
DownFromTop, | |
InFromLeft, | |
InFromRight, | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GroupMarginDecoration extends RecyclerView.ItemDecoration{ | |
private int margin; | |
private int headerbottommargin; | |
public GroupMarginDecoration(Context context) { | |
margin = context.getResources().getDimensionPixelSize(R.dimen.item_margin); | |
headerbottommargin = context.getResources().getDimensionPixelSize(R.dimen.header_bottom_margin); | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
if(parent.getChildAdapterPosition(view)==0){ | |
outRect.set(margin, headerbottommargin, margin, headerbottommargin); | |
} | |
else | |
outRect.set(margin, margin, margin, margin); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v7.widget.CardView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="horizontal" | |
xmlns:card_view="http://schemas.android.com/apk/res-auto" | |
card_view:cardCornerRadius="2dp" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
card_view:contentPadding="2dp" | |
android:background="?android:selectableItemBackground" | |
> | |
<com.example.empire.empire.widgets.SquareImageView | |
android:id="@+id/countryImage" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:scaleType="centerCrop" | |
android:layout_centerInParent="true" | |
/> | |
</android.support.v7.widget.CardView> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<android.support.v7.widget.RecyclerView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/product_list_detail" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:clipToPadding="false"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@BindView(R.id.product_list_detail) | |
RecyclerView recyclerView; | |
ProductListAdapter adapter; | |
recyclerView.addItemDecoration(new GroupMarginDecoration(this)); | |
recyclerView.setHasFixedSize(true); | |
final GridLayoutManager manager = new GridLayoutManager(this, 3); | |
recyclerView.setLayoutManager(manager); | |
adapter = new ProductListAdapter(null, | |
images, | |
ProductListDetailActivity.this, | |
ProductListAdapter.PRODUCT_LIST_DETAIL); | |
recyclerView.setAdapter(adapter); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductListAdapter extends BaseAdapter<ProductListAdapter.TextViewHolder> { | |
public static final int PRODUCT_LIST = 1; | |
public static final int PRODUCT_LIST_DETAIL = 2; | |
public Activity mactivity; | |
ArrayList<ProductImages> mproductImages; | |
ArrayList<Product> mproduct_list; | |
int mAdapterType = PRODUCT_LIST; | |
public ProductListAdapter(ArrayList<Product> product_list ,ArrayList<ProductImages> productImages , Activity activity,int AdapterType) { | |
super(activity); | |
mproduct_list = product_list; | |
mproductImages = productImages; | |
mactivity = activity; | |
mAdapterType = AdapterType; | |
} | |
@Override | |
public TextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_grid_item, parent, false); | |
return new TextViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(final TextViewHolder holder, final int position) { | |
String url = null; | |
if(mAdapterType == PRODUCT_LIST) | |
{ | |
final Product product = mproduct_list.get(position); | |
if (product != null) { | |
if (product.product_images != null) { | |
if (product.product_images.size() > 0) { | |
url = product.product_images.get(0).url; | |
} | |
} | |
} | |
} | |
else if(mAdapterType == PRODUCT_LIST_DETAIL) { | |
url = mproductImages.get(position).url; | |
} | |
final String murl = url; | |
Picasso.with(mactivity).load(murl).placeholder(R.drawable.placeholder).resize(100, 100).networkPolicy(NetworkPolicy.OFFLINE).into(holder.imageView, new Callback() { | |
@Override | |
public void onSuccess() { | |
} | |
@Override | |
public void onError() { | |
Picasso.with(mactivity). | |
load(murl).placeholder(R.drawable.placeholder). | |
resize(100, 100).into(holder.imageView); | |
} | |
}); | |
runAnimation(holder, position, defaultItemAnimationDuration, getAnimationDirection()); | |
} | |
@Override | |
public int getItemCount() | |
{ | |
if(mAdapterType == PRODUCT_LIST){ | |
return mproduct_list.size(); | |
} | |
else { | |
return mproductImages.size(); | |
} | |
} | |
public class TextViewHolder extends RecyclerView.ViewHolder { | |
@BindView(R.id.countryImage) | |
protected SquareImageView imageView; | |
public TextViewHolder(View itemView) { | |
super(itemView); | |
ButterKnife.bind(this,itemView); | |
itemView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if(mAdapterType == PRODUCT_LIST_DETAIL) { | |
String URL = mproductImages.get(getAdapterPosition()).url; | |
Intent returnIntent = new Intent(); | |
returnIntent.putExtra("url",URL); | |
mactivity.setResult(Activity.RESULT_OK, returnIntent); | |
mactivity.finish(); | |
} | |
else if(mAdapterType == PRODUCT_LIST){ | |
Intent returnIntent = new Intent(mactivity, ProductListDetailActivity.class); | |
returnIntent.putExtra("product_id",mproduct_list.get(getAdapterPosition()).product_id); | |
returnIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); | |
mactivity.startActivity(returnIntent); | |
mactivity.finish(); | |
} | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment