我有饮料类别(7项)的回收者视图,我希望所有7个项目根据保存在收藏夹表中的客户最喜欢的饮料动态更改位置。
这怎么可能以这种方式出现?
我目前的代码:
private void loadMenu() {
Query query = category.orderByChild("views");
adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(
Category.class,
R.layout.menu_item,
MenuViewHolder.class,
query
) {
@Override
protected void populateViewHolder(final MenuViewHolder viewHolder, final Category model, int position) {
viewHolder.txtMenuName.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage())
.into(viewHolder.imageView);
viewHolder.txtViews.setText(model.getViews());
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
//No of click
count++;
category.child(adapter.getRef(position).getKey()).child("views").setValue(count);
//Get CategoryId and send to new Activity
Intent drinkList = new Intent(Home.this, DrinkList.class);
//Because CategoryId is key, so we just get key of this item
drinkList.putExtra("CategoryId",adapter.getRef(position).getKey());
startActivity(drinkList);
}
});
}
};
adapter.notifyDataSetChanged();
recycler_menu.setAdapter(adapter);
}
答案 0 :(得分:0)
如果我说得对,你想根据喜欢的字段或用户喜欢的类别订购类别,如果是的话
要获得已排序和筛选的结果,您必须将查询传递到FirebaseRecyclerAdapter
构造函数,您现在在其中传递DatabaseReference
。因此,在适配器中添加一个带有Query
:
public MyFireAdapterDiasFiestaRecyclerView(
Class<DiaFiestaMeta> modelClass,
int modelLayout,
Class<MyFireViewHolder> viewHolderClass,
Query query) {
...
然后使用订单和过滤器的查询调用它:
adapter = new MyFireAdapterDiasFiestaRecyclerView(
DiaFiestaMeta.class,
layout_that_you_use,
MyFireViewHolder.class,
ref.orderByChild("property_to_sort_and_filter_on").equalTo("value_to_filter_on")
);
如果您使用DatabaseReference填充FirebaseRecyclerAdapter,那么orderByChild()
将按升序对您的查询进行排序,以便在使用orderByChild()
时使用降序查询使用此技巧良好的解决方案:
在FirebaseRecyclerAdapter声明中,像我们一样覆盖getItem()
函数:
@Override
public User getItem(int position) {
return super.getItem(getItemCount() - 1 - position);
}