How to sort RecyclerView item in android

前端 未结 7 1783
暖寄归人
暖寄归人 2021-01-01 11:29

I\'m working on a android chatting application. When I called my api it returns me the chat list sorted by a user_id. But what I need to do is serialized by

相关标签:
7条回答
  • 2021-01-01 11:57

    Add this lines of code before passing to RecyclerView Adapter

    Collections.sort(yourLists, new Comparator<YourList>() {
            @Override
            public int compare(YourList lhs, YourList rhs) {
                return lhs.getId().compareTo(rhs.getId());
            }
        });
    
    0 讨论(0)
  • 2021-01-01 12:01
     Collections.sort(response.body(), new Comparator<All_posts>() {
                            @Override
                            public int compare(All_posts lhs, All_posts rhs) {
                                if(lhs.getId() > rhs.getId()) {
                                    return -1;
                                } else {
                                    return 1;
                                }
                            }
                        });
    
    • "response.body" is the arraylist I got from json, this is what I pass to the recycler view adapter,

    • "All_posts" is the "Model" class, the one which contains only fields;

    • And getId is the value I want comparison to take place on it, it's from my model class,

    I wrote this before I set my adapter to my recycler view. and after I set my adpater to my recyclerView, I wrote recyclerView.getAdapter().notifyDataSetChanged();

    0 讨论(0)
  • 2021-01-01 12:02

    in Kotlin use like this after loading data in array:

    myItems.sortWith(Comparator { lhs, rhs ->
                // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
                if (lhs.name > rhs.name) -1 else if (lhs.id < rhs.id) 1 else 0
            })
    

    After that apply:

    myItemAdapter.notifyDataSetChanged()
    
    0 讨论(0)
  • 2021-01-01 12:05

    try this before passing your list to the adapter (after API call and before adapter notifydatasetchanged):

     Collections.sort(data, new Comparator<CustomData>() {
                @Override
                public int compare(CustomData lhs, CustomData rhs) {
                    // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
                    return lhs.getId() > rhs.getId() ? -1 : (lhs.customInt < rhs.customInt ) ? 1 : 0;
                }
            });
    
    0 讨论(0)
  • 2021-01-01 12:11

    A simpler solution for someone still stuck on the same

      Collections.sort(data, new Comparator<CustomData>() {
                @Override
                public int compare(CustomData lhs, CustomData rhs) {
                    return Integer.compare( rhs.getId(),lhs.getId());
                }
            });
    
    YourAdapter adapter = new YourAdapter(context, data);
    
    //Setup Linear or Grid Layout manager
     recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
     recyclerView.setAdapter(adapter);
    
    
    0 讨论(0)
  • 2021-01-01 12:18

    Before passing the data to RecyclerView adapter

    data.sort(new Comparator<Datum>() {
                @Override
                public int compare(Datum o1, Datum o2) {
                    return o1.get(position).getMessageId().compareTo(o2.get(position).getMessageId());
                }
            });
    

    then pass (notify) the sorted list to the adapter.

    0 讨论(0)
提交回复
热议问题