【问题标题】:how to sort card view in recycler view in android如何在android的回收器视图中对卡片视图进行排序
【发布时间】:2019-03-27 17:29:05
【问题描述】:

我有一些 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
        }  ]

我希望 isDefault 真实帐户显示为第一个 cardviewaccountType 作为 c 然后帐户类型排序应该像帐户类型 a 及其所有帐户列表和帐户类型 b及其所有帐户列表。总的来说,我的卡片应该是这样的

  • 账户类型c
  • 低于默认卡
  • 然后账号类型a
  • 所有卡片
  • 然后账户类型b
  • 所有卡片

我总是希望isDefault 卡位于顶部,而不管其帐户类型如何,然后我想根据accountTypecardView 排序为a、b、c 等。 我在 xml 布局中显示帐户类型,然后是 cardView 如何在Bindview 上实现这一点?任何帮助表示赞赏

【问题讨论】:

    标签: android android-recyclerview cardview


    【解决方案1】:

    RecyclerView 将按照您将元素传递给适配器的确切顺序显示元素。您需要做的是按照您希望它们所在的顺序重新排列元素,然后将它们传递给适配器以便显示它们。一个基于您输入的简单示例

    //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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 2021-06-26
      • 2021-10-28
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 2022-07-01
      相关资源
      最近更新 更多