我有一个FirebaseRecyclerAdapter,我尝试添加一个使用ItemTouchHelper.SimpleCallback
创建的onSwipe方法。下面是我的代码,其中尝试在滑动时删除firebase条目。下面的代码确实删除了一个firebase条目,除了它总是删除列表中的最后一个条目。我希望它与以错误的方式解析list_id有关。是否可以在populateViewHolder中调用onSwipe?
private String other_id;
protected void onStart() {
super.onStart();
final FirebaseRecyclerAdapter<Lists, ListAdapter.ListsViewHolder> listsRecycleViewAdapter = new FirebaseRecyclerAdapter<Lists, ListAdapter.ListsViewHolder>(
Lists.class,
R.layout.list_single_layout,
ListAdapter.ListsViewHolder.class,
mDoneListRef
) {
@Override
protected void populateViewHolder(final ListAdapter.ListsViewHolder viewHolder, Lists model, final int position) {
final String list_id = getRef(viewHolder.getAdapterPosition()).getKey();
other_id = list_id;
mDoneListRef.child(list_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
//do stuff
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mListView.setAdapter(listsRecycleViewAdapter);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
mDoneListRef.child(other_id).removeValue();
}
答案 0 :(得分:1)
您需要查找传递给position
的{{1}}参数刷过的项目。这些方面的东西:
onSwiped