Skip to content Skip to sidebar Skip to footer

How To Implement Recycler View With Multiple Layout

I want to implement a Recycler view like this: at start, we have two different layout , first layout is a ViewPager and second layout is an ImageView and in following is gridView

Solution 1:

How can you do it:

  • Use GridLayoutManager
  • Define 3 types of view item (view pager, image view, normal) and implement getItemViewType method. Return correspond view type with it position.
  • Implement onCreateViewHolder create the correspond ViewHolder with view type given.
  • Finally set span size for each view type like:

Use setSpanSizeLookup method.

mLayoutManager.setSpanSizeLookup(newGridLayoutManager.SpanSizeLookup() {@OverridepublicintgetSpanSize(int position) {
        switch (mAdapter.getItemViewType(position)) {
            case MyAdapter.TYPE_VIEW_PAGER:
                return2;
            case MyAdapter.TYPE_IMAGE_VIEW:
                return2;
            case MyAdaper.TYPE_NORMAL:
                return1;
        }
    }
});

Post a Comment for "How To Implement Recycler View With Multiple Layout"