简体   繁体   English

如何在回收视图中制作部分?

[英]How to make section in recycle view?

I spend much time in searching about recycle multi type view . 我花了很多时间来搜索回收多类型视图。 I already use navigation drawer using recycle view there i use two view one for header and one for menu item. 我已经在使用回收视图的导航抽屉了,在这里我使用了两个视图,一个用于标题,一个用于菜单项。 Now i need to inflate two array list like first one for 'feature' item and another for 'normal' list of item in recycle view .In this case i need to make two section in recycle view first one for 'feature' item after finish feature item , need to make another section for normal item. 现在我需要在回收视图中添加两个数组列表,例如第一个用于“功能”项目,另一个用于“正常”项目列表。在这种情况下,我需要在回收视图中创建两个部分,第一个用于完成后的“功能”项目特色商品,需要为普通商品另辟一个部分。 How can i get this ? 我怎么能得到这个? any idea ? 任何想法 ?

You can use the library SectionedRecyclerViewAdapter to group your data into sections. 您可以使用SectionedRecyclerViewAdapter库将数据分组。

First create a Section class: 首先创建一个Section类:

class MySection extends StatelessSection {

    String title;
    List<String> list;

    public MySection(String title, List<String> list) {
        // call constructor with layout resources for this Section header, footer and items 
        super(R.layout.section_header, R.layout.section_item);

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size(); // number of items of this section
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        itemHolder.tvItem.setText(list.get(position));
    }

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

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

        // bind your header view here
        headerHolder.tvItem.setText(title);
    }
}

Then you set up the RecyclerView with your Sections: 然后,使用您的Sections设置RecyclerView:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data for each year
MySection section1 = new MySection("Feature", firstDataList);
MySection section2 = new MySection("Normal", secondDataList);

// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM