Sorting a Recyclerview

后端 未结 2 1464
时光说笑
时光说笑 2021-01-07 15:22

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

2条回答
  •  不知归路
    2021-01-07 15:57

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

    Collections.sort(categories, new Comparator() {
                @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

提交回复
热议问题