FirebaseRecycleAdapter以正确的方式添加onSwipe

时间:2017-12-07 22:38:38

标签: android firebase-realtime-database firebaseui

我有一个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();
}

1 个答案:

答案 0 :(得分:1)

您需要查找传递给position的{​​{1}}参数刷过的项目。这些方面的东西:

onSwiped