一般任务:我想实施“拖拽和放大将“和”滑动到“解雇”功能下放到FirebaseRecyclerAdapter。它应该是这样的: Drag and swipe .gif expample. 首先,我将创建一个允许传递事件回调的接口:
public interface ItemTouchHelperAdapter {
boolean onItemMove(int fromPosition, int toPosition);
void onItemDismiss(int position); }
创建ItemTouchHelperCallback,该界面允许侦听“移动”和“滑动”事件:
public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final ItemTouchHelperAdapter mAdapter;
public ItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
mAdapter = adapter;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}}
最后创建FirebaseRecyclerAdapter,它实现了之前创建的ItemTouchHelperAdapter:
public class PurchaseListAdapter extends FirebaseRecyclerAdapter<Purchase, PurchaseListHolder> implements ItemTouchHelperAdapter {
public PurchaseListAdapter(Class<Purchase> modelClass, int modelLayout, Class<PurchaseListHolder> viewHolderClass, Query ref) {
super(modelClass, modelLayout, viewHolderClass, ref);
}
@Override
protected void populateViewHolder(PurchaseListHolder viewHolder, final Purchase model, final int position) {
viewHolder.mName.setText(model.getName());
}
@Override
protected void onChildChanged(ChangeEventListener.EventType type, int index, int oldIndex) {
super.onChildChanged(type, index, oldIndex);
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
// TODO: Add functionality for swapping items in FirebaseDatabase
return true;
}
@Override
public void onItemDismiss(int position) {
getRef(position).removeValue();
}}
“swipe-to-dismiss”(onItemDismiss)的功能非常好用。但我无法弄清楚如何实现onItemMove函数。理想情况下,它应该在Firebase数据库中滑动项目,但我找不到办法。
也许有人知道任何其他方式实现RecyclerView“刷卡到解雇”和“药物和丢弃”功能,这将与Firebase一起使用?