简体   繁体   English

如何在顶部显示特定的recyclerview项目?

[英]How do show specific recyclerview items at the top?

Let's say I have web-request for a list of meals and implemented using a Recyclerview, now what if I want to sort/move specific items at the top..! 比方说,我有一个Web请求,可获取一份食物清单,并使用Recyclerview实现,现在,如果我想在顶部排序/移动特定项目该怎么办。


Meal: Italian Pasta | 餐点: 意大利面食 | Type: pasta | 类型: 面食 | Price: 20$ 价格: 20 $

Meal: Hotdogs Pizza | 餐: 热狗比萨 | Type: pizza | 类型: 披萨 | Price: 10$ 价格: 10 $

Meal: Burger | 餐: 汉堡 | Type: fast food | 类型: 快餐 | Price: 30$ 价格: 30 $

Meal: Rice | 餐点: 米饭 | Type: lunch | 类型: 午餐 | Price: 18$ 价格: 18 $

Meal: Mac & Cheese | 餐: Mac和奶酪 | Type: lunch | 类型: 午餐 | Price: 17$ 价格: 17 $

Meal: CheeseBurger | 餐: CheeseBurger | Type: fast food | 类型: 快餐 | Price: 8$ 价格: 8 $


So for example, what I want is for every item who has a type of fast food and pizza to show at the top of the list and then the rest..!? 因此,例如,我想要的是每一个具有fast foodpizza类型的商品都显示在列表的顶部,然后显示其余部分。

where do I perform this sorting logic, is it inside the adapter or the activity of the recyclerview? 我在哪里执行此排序逻辑,它是在适配器内部还是在recyclerview的活动中? is there like an existing method for this function like moveToTop() to be used like this: 有没有像该函数那样的现有方法moveToTop()moveToTop()被这样使用:

for(Meals item: dataList){

   if (item.getType.equals("fast food") || item.getType.equals("pizza")){
      item.moveToTop(); 
   }

}

Is it possible? 可能吗?


Update 2 更新2

I figured out, I found a method a special parameter of interface for the recyclerview in these situations SortedListAdapterCallback 我发现,在这种情况下,我找到了一个方法作为recyclerview接口的接口的特殊参数SortedListAdapterCallback

if (homeData.size() > 0) {

            Collections.sort(homeData, Collections.reverseOrder(new SortedListAdapterCallback<HomeItemModel>(adapter) {
              @Override
              public int compare(HomeItemModel o1, HomeItemModel o2) {

               int weight1;
               int weight2;

               if(item1.getType.equals("fast food") || item1.getType.equals("pizza")){
                   weight1 = 1;
               }else{
                   weight1 = 0;
               }

               if(item2.getType.equals("fast food") || item2.getType.equals("pizza")){
                weight2 = 1;
               }else{
                weight2 = 0;
               }

                if (weight1 == weight2)
                   return 0;
                else if (weight1 > weight2)
                   return 1;
                else
                   return -1;

                 }

              @Override
              public boolean areContentsTheSame(HomeItemModel oldItem, HomeItemModel newItem) {
                return false;
              }

              @Override
              public boolean areItemsTheSame(HomeItemModel item1, HomeItemModel item2) {
                return false;
              }
            }));

          }

The only problem here, is the order at the top..! 这里唯一的问题是顶部的订单。 they show like this: 他们显示如下:

  • fast food 快餐
  • pizza 比萨
  • pizza 比萨
  • fast food 快餐
  • pizza 比萨
  • fast food 快餐
  • ... the rest of the list ...列表的其余部分

I want them to show ordered.. like this: 我希望他们显示有序..像这样:

  • fast food // or the pizzas first 快餐//首先是披萨
  • fast food 快餐
  • fast food 快餐
  • pizza 比萨
  • pizza 比萨
  • pizza 比萨
  • ... the rest of the list ...列表的其余部分

Can this be done..? 能做到吗..?

Create a Comparator inside your POJO class Meals 在您的POJO类用餐中创建一个比较器

public static class CustomComparator implements Comparator<Meals> {
    public int compare(Meals  item1, Meals  item2) {

        int weight1;
        int weight2;

        if(item1.getType.equals("fast food") || item1.getType.equals("pizza")){
            weight1 = 1;
        }else{
            weight1 = 0;
        }

        if(item2.getType.equals("fast food") || item2.getType.equals("pizza")){
            weight2 = 1;
        }else{
            weight2 = 0;
        }


        if (weight1 == weight2)
            return 0;
        else if (weight1 > weight2)
            return 1;
        else
            return -1;
    }
}

And before setting your adapter call this line then pass the data list into your Adapter constructor. 并在设置适配器之前调用此行,然后将数据列表传递到Adapter构造函数中。

Collections.sort(yourDataLIst, Collections.reverseOrder(new Meals.CustomComparator()));

EDIT If you want to all fast food first then pizza and then all the list then just tweak your sort logic as below 编辑如果您要先吃所有快餐,然后是披萨,然后是所有列表,则只需调整您的排序逻辑,如下所示

        if(item1.getType.equals("fast food")){
            weight1 = 2;
        }else if (item1.getType.equals("pizza")){
            weight1 = 1;
        } else {
            weight1 = 0;
        }

        if(item2.getType.equals("fast food")){
            weight2 = 2;
        }else if (item2.getType.equals("pizza")){
            weight2 = 1;
        } else{
            weight2 = 0;
        }

There is no simple method like moveToTop() in Arraylist . Arraylist中没有像moveToTop()这样的简单方法。 But you can shift the data manually. 但是您可以手动移动数据。

You can do that like this: 您可以这样做:

for(Meals item: dataList){

   if (item.getType.equals("fast food") || item.getType.equals("pizza")){
      int index = dataList.indexOf(item);
      dataList.remove(index);
      dataList.add(0, item);
      adapter.notifyDataSetChanged()
   }

}

Hope it helps:) 希望能帮助到你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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