我正在使用ItemTouchHelp允许向左和向右滑动以执行不同的操作,即向左滑动以进行删除和向右滑动以进行编辑。但是,我无法使背景正常工作。
我没有编码onDraw方法,而是在主布局下使用了两种不同的布局,并且在滑动时根据方向仅对其中的两个进行动画处理。或者至少那是我打算做的。这类似于Gmail或Outlook应用程序。
但是,当我开始滑动然后放开时,顶层不返回。取而代之的是,它被我随其移动的图层所代替。例如。如果我想向左滑动即可删除,并且还要移动编辑层以显示删除层。所以,如果我放手,那么我会看到编辑层。
以下是recyclerview中一种视图类型的布局文件(我有两个,但是它们都使用相同的UX):
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:id="@+id/midground_view">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="2dp"
android:id="@+id/edit_icon"
android:src="@drawable/icon_edit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/edit_icon"
android:text="EDIT"
android:layout_margin="2dp"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:id="@+id/background_view">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_margin="2dp"
android:id="@+id/delete_icon"
android:src="@drawable/icon_delete"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/delete_icon"
android:layout_margin="2dp"
android:text="DELETE"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:id="@+id/foreground_view">
<ImageView
android:id="@+id/menu_item_image"
android:layout_width="match_parent"
android:layout_height="160dp"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/menu_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/menu_item_image"
android:layout_alignParentStart="true"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:text="Item Name"
android:textStyle="bold" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/menu_item_price_pickup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/menu_item_description"
android:layout_alignParentStart="true"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:text="Pickup price"
android:textStyle="bold" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/menu_item_price_delivery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/menu_item_description"
android:layout_toEndOf="@id/menu_item_price_pickup"
android:layout_marginStart="32dp"
android:layout_marginTop="4dp"
android:text="Delivery price"
android:textStyle="bold" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/menu_item_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/menu_item_name"
android:layout_alignParentStart="true"
android:layout_marginStart="4dp"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:ellipsize="end"
android:marqueeRepeatLimit="marquee_forever"
android:maxLines="2"
android:text="Lengthy menu description goes here" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
这是我的ItemTouchHelper类,其中前景是包含带有数据的子项的布局,背景是删除图标(向左滑动时显示),而中间具有编辑图标(向右滑动时显示):< / p>
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(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return true;
}
@Override
public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder != null) {
int viewType = viewHolder.getItemViewType();
if (viewType == R.layout.item_menu_dish) {
final View foregroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().onSelected(foregroundView);
} else if (viewType == R.layout.item_menu_section) {
final View foregroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().onSelected(foregroundView);
}
}
}
@Override
public void onChildDrawOver(@NonNull Canvas c, @NonNull RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (viewHolder != null) {
if (dX < 0) {
int viewType = viewHolder.getItemViewType();
if (viewType == R.layout.item_menu_dish) {
final View foregroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewForeground;
final View backgroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewBackground;
getDefaultUIUtil().onDrawOver(c, recyclerView, backgroundView, dX, dY, actionState, isCurrentlyActive);
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
} else if (viewType == R.layout.item_menu_section) {
final View foregroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewForeground;
final View backgroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewBackground;
getDefaultUIUtil().onDraw(c, recyclerView, backgroundView, dX, dY, actionState, isCurrentlyActive);
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}
} else if (dX > 0) {
int viewType = viewHolder.getItemViewType();
if (viewType == R.layout.item_menu_dish) {
final View foregroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewForeground;
final View midgroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewMidGround;
getDefaultUIUtil().onDrawOver(c, recyclerView, midgroundView, dX, dY, actionState, isCurrentlyActive);
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
} else if (viewType == R.layout.item_menu_section) {
final View midgroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewMidGround;
final View foregroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().onDrawOver(c, recyclerView, midgroundView, dX, dY, actionState, isCurrentlyActive);
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}
}
}
}
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
int viewType = viewHolder.getItemViewType();
if (viewType == R.layout.item_menu_dish) {
final View foregroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().clearView(foregroundView);
} else if (viewType == R.layout.item_menu_section) {
final View foregroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewForeground;
getDefaultUIUtil().clearView(foregroundView);
}
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
if (dX < 0) {
int viewType = viewHolder.getItemViewType();
if (viewType == R.layout.item_menu_dish) {
final View foregroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewForeground;
final View backgroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewBackground;
getDefaultUIUtil().onDraw(c, recyclerView, backgroundView, dX, dY, actionState, isCurrentlyActive);
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
} else if (viewType == R.layout.item_menu_section) {
final View foregroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewForeground;
final View backgroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewBackground;
getDefaultUIUtil().onDraw(c, recyclerView, backgroundView, dX, dY, actionState, isCurrentlyActive);
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}
} else if (dX > 0) {
int viewType = viewHolder.getItemViewType();
if (viewType == R.layout.item_menu_dish) {
final View foregroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewForeground;
final View midgroundView = ((MenuAdapter.DishViewHolder) viewHolder).viewMidGround;
getDefaultUIUtil().onDraw(c, recyclerView, midgroundView, dX, dY, actionState, isCurrentlyActive);
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
} else if (viewType == R.layout.item_menu_section) {
final View foregroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewForeground;
final View midgroundView = ((MenuAdapter.SectionViewHolder) viewHolder).viewMidGround;
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
getDefaultUIUtil().onDraw(c, recyclerView, midgroundView, dX, dY, actionState, isCurrentlyActive);
}
}
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
@Override
public void onSwiped(@NonNull 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);
}
}