简体   繁体   中英

how to filter the recycler view card view and show that card on top that is as 1st cardView based on some boolean flag?

how to sort/filter recycler view card view and show that card view on top of all card view in android. I have some boolean flag as true on check of true I need to show that particular card on top as 1st card view then others below and how to achieve this ?

使用多个视图持有者制作适配器并将特定布局绑定为条件多项目recyclelerview

Add the method inside the adaptre, hope it helps:

 public void sortItem() {
    ArrayList<Item> items = new ArrayList<>();
    Item top =null ;
    for(Item item : data){
        if(!item.isTop){
            items.add(item);
        }else{
            top = item;
        }
    }
    if(top!=null){
       items.add(0, top);
    }
    data.clear();
    data.addAll(items);
    adapter.notifyDataSetChanged();
}





 class Item{

     public Boolean isTop; 

  }

if You want get sorted list right away You can sort the list and submit yo recycler view adaper inside the activity:

ArrayList<Item> items = new ArrayList<>();
        Item top =null ;
        for(Item item : data){
            if(!item.isTop){
                items.add(item);
            }else{
                top = item;
            }
        }
        if(top!=null){
           items.add(0, top);
        }
  yourAdapter.submitList(items)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM