RecyclerView的使用及SectionedRecyclerViewAdapter的使用

RecyclerView 的使用是相对简单的!它分多种模式,主要的是“水平布局”LInearLayout的样式和GridLayout的样式。最近看到了一个很牛叉的Adapter---SectionedRecyclerVIewAdapter!

它可以在RecyclerView中的Item添加俩种Item(header,item)!不难理解为item里面嵌套多个Item且样式和个数都可以不同。

例如1:


item_Header

item_Item1


item_Header

item_Item1

item_Item1


item_Header1

item_Item1

item_Item2


public class GridFragment extends Fragment {
private SectionedRecyclerViewAdapter adapter;
private  GridLayoutManager glm;
    public GridFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.recycle_fragment_grid, container, false);


        RecyclerView recyclerView= (RecyclerView)view. findViewById(R.id.m_recyclerview);
        adapter = new SectionedRecyclerViewAdapter();


        String[] strings1 = new String[]{"世界上","世界上空间","胡国华","给你讲个"};
        String[] strings2 = new String[]{"1","2","3","4","5","6","7","8","9","0","12","23"};
        adapter.addSection(new GridSection("功能框",strings1));
        adapter.addSection(new GridSection("选择框",strings2));


        glm= new GridLayoutManager(getContext(), 5);
        recyclerView.setLayoutManager(glm);
        glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch (adapter.getSectionItemViewType(position)) {
                    case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
                        return 5;
                    default:
                        return 1;
                }
            }
        });

        recyclerView.setAdapter(adapter);
        return view;
    }
    public class GridSection extends StatelessSection {
        private String title;
        private String[] strings;
        public GridSection (String title,String[] strings){
            super(R.layout.recycle_content_header, R.layout.recycle_content_item1);

            this.title = title;
            this.strings = strings;
        }

        @Override
        public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
            return new HeaderHolder(view);
        }

        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
            HeaderHolder headerHolder= (HeaderHolder) holder;
            headerHolder.title.setText(title);
        }

        @Override
        public int getContentItemsTotal() {
            return strings.length;
        }

        @Override
        public RecyclerView.ViewHolder getItemViewHolder(View view) {
            return new ItemHolder(view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {
            ItemHolder itemHolder= (ItemHolder) holder;
            itemHolder.content.setText(strings[position]);
            itemHolder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getContext(), strings[position], Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    class ItemHolder extends RecyclerView.ViewHolder{
        public TextView content;
        public View rootView;
        public ItemHolder(View itemView) {
            super(itemView);
            rootView=itemView;
            content= (TextView) itemView.findViewById(R.id.m_tv_content);
        }
    }
    class HeaderHolder extends RecyclerView.ViewHolder {
        public TextView title;

        public HeaderHolder(View itemView) {
            super(itemView);

            title= (TextView) itemView.findViewById(R.id.m_tv_name);
        }
    }
}

  • 一段简单的代码,是我参照github里说明文档进行编写的。github中下载的示例源码中还有其他的效果,如ExpandableListView的效果等等。
    github链接:https://github.com/luizgrp/SectionedRecyclerViewAdapter
    额外一些小Demo的地址:https://github.com/2954722256/use_little_demo//需求集合Demo

你可能感兴趣的:(RecyclerView的使用及SectionedRecyclerViewAdapter的使用)