How to implement Recycler View with multiple layout

荒凉一梦 提交于 2019-12-12 02:57:00

问题


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... after scrolling gridView come up and other layouts should be invisible

how can do it? thanks


回答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(new GridLayoutManager.SpanSizeLookup() {@Override
    public int getSpanSize(int position) {
        switch (mAdapter.getItemViewType(position)) {
            case MyAdapter.TYPE_VIEW_PAGER:
                return 2;
            case MyAdapter.TYPE_IMAGE_VIEW:
                return 2;
            case MyAdaper.TYPE_NORMAL:
                return 1;
        }
    }
});


来源:https://stackoverflow.com/questions/34385416/how-to-implement-recycler-view-with-multiple-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!