0

我正在尝试创建一个功能,如果用户在 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课程。

public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
    private RecyclerItemTouchHelperListener listener;

    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 时,此滑动功能不起作用。

4

1 回答 1

0

我通过在初始化 RecyclerView 后移动回调解决了我的问题。

recyclerView = itemView.findViewById(R.id.recyclerView);
ItemTouchHelper.SimpleCallback ItemTouchHelperCallbackLeft = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
new ItemTouchHelper(ItemTouchHelperCallbackLeft).attachToRecyclerView(recyclerView);
于 2019-01-18T05:12:34.117 回答