Display Namelist In Recyclerview under each letter in alphabetic Order Android

前端 未结 6 1550
無奈伤痛
無奈伤痛 2021-02-06 08:19

I have list of contacts which has to be displayed in alphabetic under each alphabet as shown in the image shown

How can I do this in RecyclerView, please suggest a solut

6条回答
  •  生来不讨喜
    2021-02-06 08:44

    compare your model and get first character from title ....

    private void getHeaderListLatter(ArrayList usersList) {
    
            Collections.sort(usersList, new Comparator() {
                @Override
                public int compare(CountriesModel user1, CountriesModel user2) {
                    return String.valueOf(user1.name.charAt(0)).toUpperCase().compareTo(String.valueOf(user2.name.charAt(0)).toUpperCase());
                }
            });
    
            String lastHeader = "";
    
            int size = usersList.size();
    
            for (int i = 0; i < size; i++) {
    
                CountriesModel user = usersList.get(i);
                String header = String.valueOf(user.name.charAt(0)).toUpperCase();
    
                if (!TextUtils.equals(lastHeader, header)) {
                    lastHeader = header;
                    mSectionList.add(new CountriesModel(header,true));
                }
    
                mSectionList.add(user);
            }
        }
    

    and in your adapter getItemViewType Layout like this ....

    @Override
        public int getItemViewType(int position) {
            if (mCountriesModelList.get(position).isSection) {
                return SECTION_VIEW;
            } else {
                return CONTENT_VIEW;
            }
        }
    

    for complete reference . image https://github.com/sayanmanna/LetterSectionedRecyclerView

提交回复
热议问题