我已经实施了轻扫以解雇我的recyclerview。它触发2个功能(编辑记录和删除)。如果您向左滑动,则进行编辑,如果正确,则将其删除。我在recyclerview项目的一般部分下有这样的布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fe5722">
<ImageView
android:id="@+id/edit_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="24dp"
android:src="@mipmap/ic_mode_edit_white_24dp" />
<ImageView
android:id="@+id/delete_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="24dp"
android:src="@mipmap/ic_delete_white_24dp" />
</RelativeLayout>
因此,如果向左滑动(删除项目),则应隐藏左图标(用于编辑),直到滑动结束。但是,我不知道如何在ItemTouchHelper.SimpleCallback类中获取滑动方向。请帮忙。
RecyclerItemTouchHelper.java
public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {
private RecyclerItemTouchHelperListener listener;
private ImageView imageView;
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 onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder != null) {
final View foregroundView = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();
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 = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final View foregroundView = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();
getDefaultUIUtil().clearView(foregroundView);
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).setEditIconInvisible();
final View foregroundView = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive);
}
@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return super.getSwipeDirs(recyclerView, viewHolder);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
listener.onSwiped(viewHolder, direction, viewHolder.getAdapterPosition());
}
@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
if(layoutDirection == ItemTouchHelper.LEFT){
Log.d("myLogs", "Налево пошло");
}
return super.convertToAbsoluteDirection(flags, layoutDirection);
}
public interface RecyclerItemTouchHelperListener {
void onSwiped(RecyclerView.ViewHolder viewHolder, int direction, int position);
}
}
更新:我已经解决了这个问题,只是更新了onChildDraw方法:
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getLayout();
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
if (dX > 0) {
((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getEditIcon().setVisibility(View.VISIBLE);
((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getDeleteIcon().setVisibility(View.GONE);
} else {
((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getEditIcon().setVisibility(View.GONE);
((PasswordRecyclerViewAdapter.ViewHolder) viewHolder).getDeleteIcon().setVisibility(View.VISIBLE);
}
}
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
actionState, isCurrentlyActive);
}
答案 0 :(得分:2)
ItemTouchHelper.Callback
有一个公共方法onChildDraw()
,将在每个具有dX
值的帧上调用。此值是由于用户执行当前滑动操作而导致的视图的水平平移。
您可以使用dX
的值(以及当它之前为零时现在是正数还是先前为零时为负数)来检测用户是否从左向右或向右滑动到左。
private class MyItemTouchCallback extends ItemTouchHelper.Callback {
private float previousDx = 0;
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
...
previousDx = 0;
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (previousDx <= 0 && dX > 0) {
// swiping from left to right
}
else if (previousDx >= 0 && dX < 0) {
// swiping from right to left
}
...
previousDx = dX;
}
}