Skip to content Skip to sidebar Skip to footer

Sorting A Recyclerview

Hi guys How would i be able to sort a recyclerview by the data that is within a certain textview which is inside each item inside the recyclerview. I also would love to know how to

Solution 1:

First of all Sort the data using Comparator before setting adapter. using Collections. import java.util.Collections like

Collections.sort(categories, new Comparator<AnyClass>() {
            @Override
            public int compare(AnyClasslhs, AnyClassrhs) {
                return lhs.label.compareTo(rhs.label);
            }
        });

In above I sort a lable in AnyClass alphabetically. And In order to add a header you have to convert the label into charArray and compare it with last item in array. if it is not that then add a header.

Some Helpful links

https://eshyu.wordpress.com/2010/08/15/cursoradapter-with-alphabet-indexed-section-headers/

https://github.com/amalChandran/ListviewAlphabetIndexer

Display Namelist In Recyclerview under each letter in alphabetic Order Android


Solution 2:

This library makes it easy to group your items and display headers without having to compare indexes.

First you create a Section class:

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:

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

// Create your sections with the list of data for each year
MySection section1 = new MySection("Header 1", section1DataList);
MySection section2 = new MySection("Header 2", section2DataList);

// 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);

Post a Comment for "Sorting A Recyclerview"