Skip to main content

Flexbox inside the RecyclerView as a LayoutManager (FlexboxLayoutManager).

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
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

FlexboxLayout                FlexboxLayoutManager (RecyclerView)
flexDirection Check Check
flexWrap Check Check (except wrap_reverse)
justifyContent Check Check
alignItems Check Check
alignContent Check -
layout_order Check -
layout_flexGrow Check Check
layout_flexShrink Check Check
layout_alignSelf Check Check
layout_flexBasisPercent Check Check
layout_(min/max)Width Check Check
layout_(min/max)Height Check Check
layout_wrapBefore Check Check
Divider Check Check
View recycling - Check
Scrolling *1 Check

*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:

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

Comments

  1. 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

Post a Comment

Popular posts from this blog

Vertical AutoScrolling TextView in Android

In android by default we can scroll the text in horizontal using marquee in layout, but if we want to scroll the text in vertical its not possible by default. So here we will learn to create a custom TextView which will auto-scroll in vertical direction. Source Code:  VerticalScrollingTextView-Android Create a AutoScrollingTextView.class which extends TextView: @SuppressLint ( "AppCompatCustomView" ) public class AutoScrollingTextView extends TextView { private static final float DEFAULT_SPEED = 65.0f ; public Scroller scroller ; public float speed = DEFAULT_SPEED ; public boolean continuousScrolling = true; public AutoScrollingTextView (Context context) { super (context) ; init( null, 0 ) ; scrollerInstance(context) ; } public AutoScrollingTextView (Context context , AttributeSet attrs) { super (context , attrs) ; init(attrs , 0 ) ; scr

Android RecyclerView and StaggeredGridLayoutManager with Picasso/Glide

This project is there in GitHub https://github.com/yuvaraj119/Picasso-RecyclerView-StaggeredGridLayoutManager You can download and start customizing it for your project also. How to use with Picasso Picasso + RecyclerView + StaggeredGridLayoutManager Its the enhanced version of this project https://github.com/pohh/slotmachinepicasso were there was a problem with Picasso + RecyclerView + StaggeredGridLayoutManager shuffles resizable recycler views infinitely issue posted on github https://github.com/square/picasso/issues/918 I have made some changes now it works with Picasso and Glide without any shuffles and position change Currently this project is done with Picasso If you want to use it with Glide How to use with Glide Glide + RecyclerView + StaggeredGridLayoutManager Add dependencies for Glide https://github.com/bumptech/glide Remove Picasso library from dependency and remove all the codes of Picasso from MyGridAdapter.java and also from other p