在我的代码中,我想实现向左/向右滑动功能
其中一项可以接受,另一项可以拒绝
从左到右快速运行,根本没有问题。您可以看到屏幕截图。
但是当我尝试从右向左显示时,我有一个问题,它什么也没显示。我只是一个白色的屏幕。在此处查看屏幕截图
我应该有这个
我可以将代码放在这里。
public class SwipeToDeleteCallback extends ItemTouchHelper.SimpleCallback {
private DriverOrderAdapter mAdapter;
public SwipeToDeleteCallback(final DriverOrderAdapter adapter) {
super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
mAdapter = adapter;
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder != null) {
final View foregroundView = ((DMBaseAdapter.BaseViewHolder<DriverOrderItemBinding>) viewHolder).binding.llForeground;
getDefaultUIUtil().onSelected(foregroundView);
}
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
mAdapter.deleteItem(position);
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((DMBaseAdapter.BaseViewHolder<DriverOrderItemBinding>) viewHolder).binding.llForeground;
drawBackground(viewHolder, dX, actionState);
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}
@Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
final View foregroundView = ((DMBaseAdapter.BaseViewHolder<DriverOrderItemBinding>) viewHolder).binding.llForeground;
drawBackground(viewHolder, dX, actionState);
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final View rlBackground = ((DMBaseAdapter.BaseViewHolder<DriverOrderItemBinding>) viewHolder).binding.rlBackground;
final View foregroundView = ((DMBaseAdapter.BaseViewHolder<DriverOrderItemBinding>) viewHolder).binding.llForeground;
// TODO: should animate out instead. how?
rlBackground.setRight(0);
getDefaultUIUtil().clearView(foregroundView);
}
private static void drawBackground(RecyclerView.ViewHolder viewHolder, float dX, int actionState) {
final View rlBackground = ((DMBaseAdapter.BaseViewHolder<DriverOrderItemBinding>) viewHolder).binding.rlBackground;
final View rlBackground2 = ((DMBaseAdapter.BaseViewHolder<DriverOrderItemBinding>) viewHolder).binding.rlBackground2;
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
//noinspection NumericCastThatLosesPrecision
if (dX > 0) {
rlBackground.setRight((int) Math.max(dX, 0));
rlBackground.setVisibility(View.VISIBLE);
rlBackground2.setVisibility(View.GONE);
// Log.d("myLogs", "1 " + dX);
} else if (dX < 0) {
rlBackground.setRight((int) Math.max((-1) * dX, 0));
// rlBackground2.setRight((int) Math.max(aaa, 0));
rlBackground.setVisibility(View.GONE);
rlBackground2.setVisibility(View.VISIBLE);
// Log.d("myLogs", "2 " + dX);
} else {
rlBackground.setVisibility(View.GONE);
rlBackground2.setVisibility(View.GONE);
// Log.d("myLogs", "0");
rlBackground.setRight((int) Math.max(dX, 0));
// rlBackground2.setRight((int) Math.max(dX, 0));
}
}
}
}
这是我的xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="order"
type="com.core.model.Order" />
<variable
name="context"
type="android.content.Context" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl_background"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/bg_color_9">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="30dp"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:ignore="UselessParent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_driver_accept"
tools:ignore="ContentDescription" />
<TextView
style="@style/text_view_text_size_1"
android:layout_marginTop="7dp"
android:text="Accept"
android:textColor="@color/text_color_8"
android:textSize="@dimen/text_view_text_size_3" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_background_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/bg_color_10">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="30dp"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:ignore="UselessParent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_driver_decline"
tools:ignore="ContentDescription" />
<TextView
style="@style/text_view_text_size_1"
android:layout_marginTop="7dp"
android:text="Decline"
android:textColor="@color/text_color_9"
android:textSize="@dimen/text_view_text_size_3" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/ll_foreground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/activity_horizontal_margin_for_content"
android:paddingEnd="@dimen/activity_horizontal_margin_for_content">
<ImageView
android:id="@+id/iv_right_arrow"
android:layout_width="9dp"
android:layout_height="9dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_arrow_right"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="@dimen/activity_horizontal_margin_for_content"
android:layout_marginTop="15dp"
android:layout_toStartOf="@id/iv_right_arrow"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="vertical">
<View
android:layout_width="6dp"
android:layout_height="6dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/shape_circle_1" />
</LinearLayout>
<TextView
android:id="@+id/tv_from"
style="@style/text_view_text_size_1"
android:layout_gravity="center_vertical"
android:layout_marginStart="16dp"
android:text="@{order.point_a}"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="vertical">
<View
android:layout_width="6dp"
android:layout_height="6dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/shape_circle_2" />
</LinearLayout>
<TextView
android:id="@+id/tv_to"
style="@style/text_view_text_size_1"
android:layout_marginStart="16dp"
android:text="@{order.point_b}" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:paddingStart="@dimen/activity_horizontal_margin_for_content"
android:paddingEnd="@dimen/activity_horizontal_margin_for_content">
<TextView
android:id="@+id/tv_price_and_payment_type"
style="@style/text_view_text_size_1"
android:layout_width="match_parent"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@id/tv_time"
android:singleLine="true" />
<TextView
android:id="@+id/tv_time"
style="@style/text_view_text_size_1"
android:layout_alignParentEnd="true"
android:singleLine="true"
android:text="@{order.direction_duration}"
android:textColor="@color/text_color_4"
android:textSize="@dimen/text_view_text_size_3" />
</RelativeLayout>
<TextView
android:id="@+id/tv_text"
style="@style/text_view_text_size_1"
android:layout_marginStart="56dp"
android:layout_marginTop="7dp"
android:paddingStart="@dimen/activity_horizontal_margin_for_content"
android:paddingEnd="@dimen/activity_horizontal_margin_for_content"
android:text="@{order.comment}"
android:textColor="@color/text_color_4"
android:textSize="@dimen/text_view_text_size_3" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="12dp"
android:background="@color/bg_color_3" />
</LinearLayout>
</FrameLayout>
</layout>
我知道问题来自rlBackground.setRight((int) Math.max((-1) * dX, 0));
这行。
谁能告诉我该在哪里显示我被拒绝的观点?
我尝试了很多方法。我也尝试了setLeft
方法-没有结果
请有人可以帮我还是给我一个例子?