1

我有一个带有滑动删除选项的自定义 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 中的 Viewholder。

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);

    }
}

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

4

1 回答 1

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);
    }
}
于 2018-05-10T15:17:58.713 回答