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..!
Meal: Italian Pasta | Type: pasta | Price: 20$
Meal: Hotdogs Pizza | Type: pizza | Price: 10$
Meal: Burger | Type: fast food | Price: 30$
Meal: Rice | Type: lunch | Price: 18$
Meal: Mac & Cheese | Type: lunch | Price: 17$
Meal: CheeseBurger | Type: fast food | Price: 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..!?
where do I perform this sorting logic, is it inside the adapter or the activity of the recyclerview? is there like an existing method for this function like moveToTop()
to be used like this:
for(Meals item: dataList){
if (item.getType.equals("fast food") || item.getType.equals("pizza")){
item.moveToTop();
}
}
Is it possible?
Update 2
I figured out, I found a method a special parameter of interface for the recyclerview in these situations 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..?