В моем коде я хочу реализовать функцию пролистывания влево / вправо
Один из размахов должен быть принят, другой - отклонен
Свифт слева направо работает отлично, никаких проблем вообще нет. Вы можете увидеть скриншот.
Но когда я пытаюсь показать справа налево, у меня возникает проблема, она ничего не показывает. У меня просто белый экран. Смотрите скриншот здесь
Ожидается, что это будет
Я могу поставить свой код здесь.
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
метод - без результата
Пожалуйста, кто-нибудь может мне помочь или привести пример?