Add two sections in recyclerview android

后端 未结 6 1087
天命终不由人
天命终不由人 2020-12-04 12:12

In my application i am using recyclerview to display all contact list. I want two section in recyclerview.

Like one section is my application contact list and second

6条回答
  •  无人及你
    2020-12-04 13:08

    If you are looking for a solution that doesn't need to use hardcoded header/row indexes, you can use the library SectionedRecyclerViewAdapter.

    First create a Section class to group your items:

    class MySection extends StatelessSection {
    
        String title;
        List list;
    
        public MySection(String title, List 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);
        }
    
        public void addRow(String item) {
            this.list.add(item);
        }
    
    }
    

    Then you set up the RecyclerView with your Sections:

    // Create an instance of SectionedRecyclerViewAdapter 
    SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
    
    // Create your sections with the list of data
    MySection favoritesSection = new MySection("Favorites", favoritesList);
    MySection contactsSection = new MySection("Add Favorites", contactsList);
    
    // Add your Sections to the adapter
    sectionAdapter.addSection(favoritesSection);
    sectionAdapter.addSection(contactsSection);
    
    // Set up your RecyclerView with the SectionedRecyclerViewAdapter
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(sectionAdapter);
    

    You can also add new rows to your sections without having to recalculate indexes:

    favoritesSection.addRow("new item");
    sectionAdapter.notifyDataSetChanged();
    

提交回复
热议问题