Currently google has release the Flexbox which can be used for building flexible layouts using FlexboxLayout, it can be interpreted as an advanced LinearLayout
because both layouts align their child views sequentially.
For more detail on this flexbox-layout
For more detail on this flexbox-layout
But here we are gonna work on Flexbox with RecyclerView. Flexbox with a large number of items in a scrollable container!
Let's first see what are the Supported attributes / features comparison
Due to some characteristics of the RecyclerView, some Flexbox attributes are not available/not implemented
to the FlexboxLayoutManager.
Here is a quick overview of the attributes/features comparison between the two containers.
Attribute / Feature
*1 Partially possible by wrapping it with ScrollView. But it isn't likely to work with large set of views inside the layout. Because it doesn't consider view recycling.
Setting Flexbox attributes through Java code instead of settings those from XML for the FlexboxLayoutManager For example:
Add the following dependency to your build.gradle file:
dependencies {
compile 'com.google.android:flexbox:0.3.0-alpha3'
}
layout.xml is gonna be same as we do for normal recyclerview. only through java code we will use flexbox.
In Activity:
Add the following dependency to your build.gradle file:
dependencies {
compile 'com.google.android:flexbox:0.3.0-alpha3'
}
layout.xml is gonna be same as we do for normal recyclerview. only through java code we will use flexbox.
In Activity:
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(); layoutManager.setFlexWrap(FlexWrap.WRAP); layoutManager.setFlexDirection(FlexDirection.ROW); layoutManager.setAlignItems(AlignItems.STRETCH); recyclerView.setLayoutManager(layoutManager); RecyclerView.Adapter adapter = new CatAdapter(this); recyclerView.setAdapter(adapter); adapter.notifyDataSetChanged();
Create a CatAdapter:
public class CatAdapter extends RecyclerView.Adapter<CatViewHolder>{ private static final int[] CAT_IMAGE_IDS = new int[]{ R.drawable.cat_1,R.drawable.cat_2,R.drawable.cat_3}; private Context mContext; CatAdapter(Context context){ mContext = context; } @Override public CatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.main_activity, parent, false); return new CatViewHolder(view); } @Override public void onBindViewHolder(CatViewHolder holder, int position) { int pos = position % CAT_IMAGE_IDS.length; Drawable drawable = ResourcesCompat.getDrawable(mContext.getResources(), CAT_IMAGE_IDS[pos], null); holder.bindTo(drawable); } @Override public int getItemCount() { return CAT_IMAGE_IDS.length * 4; } }
Create a CatViewHolder:
public class CatViewHolder extends RecyclerView.ViewHolder { private ImageView mImageView; CatViewHolder(View itemView) { super(itemView); mImageView = (ImageView) itemView.findViewById(R.id.icon_view); } void bindTo(Drawable drawable) { mImageView.setImageDrawable(drawable); ViewGroup.LayoutParams lp = mImageView.getLayoutParams(); if (lp instanceof FlexboxLayoutManager.LayoutParams) { FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams)lp; flexboxLp.setFlexGrow(1.0f); } } }
For more details on this
demo-cat-gallery
Hi, do you still have the xml file? I'd like to know what the "icon_view" and "R.drawable.cat_1,R.drawable.cat_2,R.drawable.cat_3" are.
ReplyDelete