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

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged. SeekBar support only int values for progress: void setMax(int max) Set the upper range of the progress bar max. void setMin(int min) Set the lower range of the progress bar to min. void setProgress(int progress) Sets the current progress to the specified value. int getMax() Return the upper limit of this progress bar's range. int getMin() Return the lower limit of this progress bar's range. int getProgress() Get the progress bar's current level of progress. In some scenario we would need floating point seekbar progress, which is not supported by default in android. Here the floating seekbar which supports floating point value: You can download the full sample and library from https://github.com/yuvaraj119/Cus...