答案 0 :(得分:7)
这就是我跟随@ divers的帖子所做的:
正如他所提到的,我将团队列表传递给已排序的适配器,并在下一个名称之前添加了字母。
这是用于设置适配器的代码
ArrayList<TeamMember> sortList(ArrayList<TeamMember> list) {
Collections.sort(list, new Comparator<TeamMember>() {
@Override
public int compare(TeamMember teamMember1, TeamMember teamMember2) {
return teamMember1.getFullname().compareTo(teamMember2.getFullname());
}
});
return list;
}
对从服务器获得的团队列表进行排序的代码如下:
ArrayList<TeamMember> addAlphabets(ArrayList<TeamMember> list) {
int i = 0;
ArrayList<TeamMember> customList = new ArrayList<TeamMember>(); 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;
}
在向列表中添加字母时,我设置了一个类型值,以确定其字母或团队名称是否在适配器内部检查以显示相应的布局。其代码如下所示:
@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;
}
}
最后在适配器内检查项目是否为teamMember名称或字母,并显示相应的布局,如下所示:
class Loan{
BigDecimal loanamount;
Date duedate;
----getters and setters
}
class Test
{
void sortLoan()
{
Loan loan1=new Loan();
loan1.setLoanamount(100);
loan1.setDuedate(new Date()); //Jan
Loan loan2=new Loan();
loan2.setLoanamount(100);
loan2.setDuedate(new Date()); //Feb
Loan loan3=new Loan();
loan3.setLoanamount(200);
loan3.setDuedate(new Date()); //Mar
Loan loan4=new Loan();
loan4.setLoanamount(200);
loan4.setDuedate(new Date()); //Apr
Loan loan5=new Loan();
loan5.setLoanamount(200);
loan5.setDuedate(new Date()); //May
Loan loan6=new Loan();
loan6.setLoanamount(100);
loan6.setDuedate(new Date()); //June
Loan loan7=new Loan();
loan7.setLoanamount(100);
loan7.setDuedate(new Date()); //July
List<Loan> loanList=new ArrayList<>();
loanList.add(loan1);
loanList.add(loan2);
loanList.add(loan3);
loanList.add(loan4);
loanList.add(loan5);
loanList.add(loan6);
loanList.add(loan7);
}
Expected output : Loan amount of $100 for 1st 2 months
Loan amount of $200 for next 3 months
Loan amount of $100 for next 2 months
希望这可以帮到你。一切顺利
答案 1 :(得分:5)
Adapter
地方特殊视图内。答案 2 :(得分:5)
我目前正在使用this。它很容易实现,与RecyclerView适配器兼容,而且轻量级你几乎不称它为库!
答案 3 :(得分:3)
比较你的模型并从标题中获得第一个角色....
private void getHeaderListLatter(ArrayList<CountriesModel> usersList) {
Collections.sort(usersList, new Comparator<CountriesModel>() {
@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);
}
}
并在你的适配器getItemViewType布局像这样....
@Override
public int getItemViewType(int position) {
if (mCountriesModelList.get(position).isSection) {
return SECTION_VIEW;
} else {
return CONTENT_VIEW;
}
}
完整参考。 https://github.com/sayanmanna/LetterSectionedRecyclerView
答案 4 :(得分:2)
答案 5 :(得分:1)