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, newComparator<AnyClass>() {
            @Overridepublicintcompare(AnyClasslhs, AnyClassrhs) {
                return lhs.label.compareTo(rhs.label);
            }
        });

In above I sort a lable in AnyClassalphabetically. 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:

classMySectionextendsStatelessSection {

    String title;
    List<String> list;

    publicMySection(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;
    }

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

    @OverridepublicRecyclerView.ViewHoldergetItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this sectionreturnnewMyItemViewHolder(view);
    }

    @OverridepublicvoidonBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

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

    @OverridepublicRecyclerView.ViewHoldergetHeaderViewHolder(View view) {
        returnnewSimpleHeaderViewHolder(view);
    }

    @OverridepublicvoidonBindHeaderViewHolder(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 SectionedRecyclerViewAdaptersectionAdapter=newSectionedRecyclerViewAdapter();

// Create your sections with the list of data for each yearMySectionsection1=newMySection("Header 1", section1DataList);
MySectionsection2=newMySection("Header 2", section2DataList);

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

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

Post a Comment for "Sorting A Recyclerview"