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
This is what I did following @divers's post:
as he mentioned I pass a team list to the the adapter which is sorted and alphabets are added before the next name.
this is he code used to set adapter
void updateUI(ArrayList teamMembers) {
adapter = new TeamMemberActivityAdapter(this, addAlphabets(sortList(teamMembers)));
recList.setAdapter(adapter);
recList.setVisibility(View.VISIBLE);
spinningProgressView.setVisibility(View.GONE);
}
code to sort the team list obtained from server is given below:
ArrayList sortList(ArrayList list) {
Collections.sort(list, new Comparator() {
@Override
public int compare(TeamMember teamMember1, TeamMember teamMember2) {
return teamMember1.getFullname().compareTo(teamMember2.getFullname());
}
});
return list;
}
while adding alphabets to the list I am setting a type value to know whether its alphabet or team name to check this inside the adapter for showing corresponding layout .the code for that is as shown below:
ArrayList addAlphabets(ArrayList list) {
int i = 0;
ArrayList customList = new ArrayList(); TeamMember firstMember = new TeamMember();
firstMember.setFullname(String.valueOf(list.get(0).getFullname().charAt(0)));
firstMember.setType(1);
customList.add(firstMember);
for (i = 0; i < list.size() - 1; i++) {
TeamMember teamMember = new TeamMember();
char name1 = list.get(i).getFullname().charAt(0);
char name2 = list.get(i + 1).getFullname().charAt(0);
if (name1 == name2) {
list.get(i).setType(2);
customList.add(list.get(i));
} else {
list.get(i).setType(2);
customList.add(list.get(i));
teamMember.setFullname(String.valueOf(name2));
teamMember.setType(1);
customList.add(teamMember);
}
}
list.get(i).setType(2);
customList.add(list.get(i));
return customList;
}
And finally inside your adapter check if the item is teamMember name or alphabet and display corresponding layout as shown below:
@Override
public int getItemViewType(int position) {
int viewType = 0;
if (mMembers.get(position).getType() == TYPE_LETTER) {
viewType = TYPE_LETTER;
} else if (mMembers.get(position).getType() == TYPE_MEMBER) {
viewType = TYPE_MEMBER;
}
return viewType;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case TYPE_LETTER:
ViewGroup vGroupImage = (ViewGroup) mInflater.inflate(R.layout.board_team_letter_item, viewGroup, false);
ViewHolderLetter image = new ViewHolderLetter(vGroupImage);
return image;
case TYPE_MEMBER:
ViewGroup vGroupText = (ViewGroup) mInflater.inflate(R.layout.board_team_member_item, viewGroup, false);
ViewHolderMember text = new ViewHolderMember(vGroupText);
return text;
default:
ViewGroup vGroupText2 = (ViewGroup) mInflater.inflate(R.layout.board_team_member_item, viewGroup, false);
ViewHolderMember text1 = new ViewHolderMember(vGroupText2);
return text1;
}
}
hope this could help you. all the best