RecyclerView刷卡删除片段?

时间:2019-01-17 00:57:46

标签: android android-fragments android-recyclerview swipe itemtouchhelper

我正在尝试创建一项功能,如果用户在RecyclerView中向左滑动某个项目,该项目将被删除。

我的RecyclerView在一个Fragment的内部,该Fragment在一个由BottomNavigation控制的FrameLayout内。

当RecyclerView处于其自己的Activity中时,我具有在其中工作的代码,但是当我将其传输到Fragment中时,滑动功能会中断。我根本无法滑动。

在我的片段中,我正在扩展RecyclerItemTouchHelperListener界面。

interface RecyclerItemTouchHelperListener {
    void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
}

然后我创建一个回调

ItemTouchHelper.SimpleCallback ItemTouchHelperCallbackLeft = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
new ItemTouchHelper(ItemTouchHelperCallbackLeft).attachToRecyclerView(recyclerView);

现在,我创建了一个名为OnSwiped的函数来处理刷卡操作。

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position) {
    if (viewHolder instanceof ListAdapter.ViewHolder) {
        if (direction == ItemTouchHelper.LEFT) {
            String cityName = stationList.get(viewHolder.getAdapterPosition()).getData().getCity();

            //Create a backup of the deleted item in case user wants to undo delete
            Station deletedStation = stationList.get(viewHolder.getAdapterPosition());
            int deletedStationIndex = viewHolder.getAdapterPosition();

            //Remove the item from RecyclerView
            listAdapter.removeItem(deletedStationIndex);

            Snackbar snackbar = Snackbar.make(coordinatorLayout, cityName + " removed from list!", Snackbar.LENGTH_LONG);
            snackbar.setAction("UNDO", new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    // undo is selected, restore the deleted item
                    listAdapter.restoreItem(deletedStation, position);
                }
            });
            snackbar.show();
        }
    }
}

这是RecyclerItemTouchHelper类。

公共类RecyclerItemTouchHelper扩展了ItemTouchHelper.SimpleCallback {     私人RecyclerItemTouchHelperListener侦听器;

public RecyclerItemTouchHelper(int dragDirs, int swipeDirs, RecyclerItemTouchHelperListener listener) {
    super(dragDirs, swipeDirs);
    this.listener = listener;
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
    if (viewHolder != null) {
        final View foregroundView = ((ListAdapter.ViewHolder) viewHolder).listViewForeground;
        getDefaultUIUtil().onSelected(foregroundView);
    }
}

@Override
public void onChildDrawOver(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    final View foregroundView = ((ListAdapter.ViewHolder) viewHolder).listViewForeground;
    getDefaultUIUtil().onDrawOver(canvas, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    final View foregroundView = ((ListAdapter.ViewHolder) viewHolder).listViewForeground;
    getDefaultUIUtil().clearView(foregroundView);
}

@Override
public void onChildDraw(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    final View foregroundView = ((ListAdapter.ViewHolder) viewHolder).listViewForeground;

    getDefaultUIUtil().onDraw(canvas, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}

@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
    return super.convertToAbsoluteDirection(flags, layoutDirection);
}

}

请记住,当RecyclerView处于其自己的Activity中时,一切都正常,但是当我将RecyclerView转换为Fragment时,此滑动功能不起作用。

1 个答案:

答案 0 :(得分:0)

在初始化RecyclerView之后,我通过移动回调来解决问题。

recyclerView = itemView.findViewById(R.id.recyclerView);
ItemTouchHelper.SimpleCallback ItemTouchHelperCallbackLeft = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
new ItemTouchHelper(ItemTouchHelperCallbackLeft).attachToRecyclerView(recyclerView);