在轻扫时禁用recycleView中的少数单元格或在滑动时隐藏删除

时间:2018-05-10 07:44:35

标签: android android-recyclerview itemtouchhelper

我有一个带有滑动删除选项的自定义recycleView。我能够显示自定义recycleView并通过刷新删除该项目。通过使用实现SimpleCallback的单独类来实现滑动以删除。现在,我希望禁用滑动删除或不显示删除按钮,同时根据RecycleView中显示的用户名滑动少数项目。

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

SwipeHelper(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 onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
    if (viewHolder != null) {
        final View foregroundView = ((Adapter.ViewHolder) viewHolder).viewforeground;

        getDefaultUIUtil().onSelected(foregroundView);
    }
}

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

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

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

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

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

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

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

}

我是如何实施的

   ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new SwipeHelper(0, ItemTouchHelper.LEFT, this);
    new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);

recycleView中的查看器。

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView uname,time,comm;
    CircleImageView civ;
    RelativeLayout viewBackground,viewforeground;

    ViewHolder(View itemView) {
        super(itemView);

        civ = (CircleImageView)itemView.findViewById(R.id.profile_image);
        uname = (TextView) itemView.findViewById(R.id.username);
        time = (TextView) itemView.findViewById(R.id.time);
        comm = (TextView) itemView.findViewById(R.id.comments);
        viewBackground = (RelativeLayout)itemView.findViewById(R.id.view_background);
        viewforeground = (RelativeLayout)itemView.findViewById(R.id.view_foreground);

    }
}

现在我想禁用在viewholder中与uname不匹配的用户名的滑动。我搜索了他们要求在SwipeDirs方法中进行更改,但是我无法在其中获取viewHolder值。请帮忙。

1 个答案:

答案 0 :(得分:0)

覆盖ItemTouchHelper.SimpleCallback中的getMovementFlags以检查视图持有者是否允许滑动的条件如下:

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        final int dragFlags = 0;
        final int swipeFlags;
        swipeFlags = ([OK to swipe this view]) 
            ? ItemTouchHelper.START | ItemTouchHelper.END
            : 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    }
}