[英]how to sort card view in recycler view in android
I have some API response as below:我有一些 API 响应如下:
[
{
accountType: a,
accountId: 1,
accountStatus: active,
isDefault: false,
accountName: texas1
}, {
accountType: b,
accountId: 1,
accountStatus: active,
isDefault: false,
accountName: texas2
}, {
accountType: c,
accountId: 1,
accountStatus: active,
isDefault: true,
accountName: texas4
}, {
accountType: a,
accountId: 1,
accountStatus: active,
isDefault: false,
accountName: texas5
}, {
accountType: b,
accountId: 1,
accountStatus: active,
isDefault: false,
accountName: texas6
},
{
accountType: a,
accountId: 1,
accountStatus: active,
isDefault: false,
accountName: texas7
}, {
accountType: b,
accountId: 1,
accountStatus: active,
isDefault: false,
accountName: texas9
} ]
I want the isDefault
true account to be shown as first cardview
with accountType
as c
then account type sorting should be done like account type a and its all account list and the account type b
and all its account list.我想
isDefault
真实账户显示为第一cardview
与accountType
为c
那么账户类型排序应该像帐户类型及其所有账户列表,账户类型进行b
及其所有账户列表。 over all my card should like this总的来说,我的卡应该是这样的
c
c
a
a
b
b
I always want isDefault
card to be on top irrespective of its account type then I want to sort cardView
based on accountType
as a,b,c etc. I m displaying account type then cardView
below in xml
layout How to achieve this on Bindview
?我总是想
isDefault
卡在最前面,不论其帐户类型的话,我想那种cardView
基于accountType
为a,b,则c等I M显示帐户类型cardView
下面xml
布局如何实现这一目标上Bindview
? any help is appreciated任何帮助表示赞赏
The RecyclerView
will display elements in the exact order that you pass them to your adapter. RecyclerView
将按照您将元素传递给适配器的确切顺序显示元素。 What you need to do is to rearrange your elements in the order you want them to be in and then pass them to the adapter so that they can be shown.您需要做的是按照您希望元素的顺序重新排列元素,然后将它们传递给适配器,以便可以显示它们。 A simple example based on your input
基于您输入的简单示例
//This is just a data class for our API response
class Account {
String accountType;
int accountId;
boolean accountStatus;
boolean isDefault;
String accountName;
}
//Lets say that you have your API response in a list as such
List<Account> accountList = new ArrayList<>();
accountList.add(/*Response from API*/);
//Now we create a sorted list based on your rules
List<Account> sortedAccountList = new ArrayList<>();
//First we need the isDefault account
for (Account account : accountList) {
if (account.isDefault) {
sortedAccountList.add(account);
accountList.remove(account);
break;
}
}
//Now we add all 'c' type accounts
for (Account account : accountList) {
if (account.accountType.equals("c")) {
sortedAccountList.add(account);
accountList.remove(account);
}
}
//Do the same as above for the other account types. You can also apply more rules as per your needs.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.