gpt4 book ai didi

android - RecyclerView ItemTouchHelper - 自定义 View

转载 作者:可可西里 更新时间:2023-11-01 19:07:13 26 4
gpt4 key购买 nike

我试图在向左/向右滑动时显示自定义 View (或图像)。

我快要让它正常工作了,但是我在使用自定义图像时遇到了问题。

我想要的效果类似于:RecyclerView ItemTouchHelper swipe remove animation

看看下面发生了什么。它会在滑动时扭曲图像:

enter image description here

下面是我的代码:

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
View itemView = viewHolder.itemView;

Paint p = new Paint();
if (dX > 0) {
Drawable d = ContextCompat.getDrawable(getActivity(), R.drawable.test_check);
d.setBounds(itemView.getLeft(), itemView.getTop(), (int) dX, itemView.getBottom());
d.draw(c);

super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}

super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}

如何在继续绘制背景颜色的同时保持图像静止?甚至可以使用 Android View 而不是图像吗?类似于:https://github.com/wdullaer/SwipeActionAdapter

最佳答案

您可以准备您的回收站 View 项目布局,以包含复选符号 ImageView 以及另一个仅具有背景颜色的空白 View 。您还应该将所有可滑动的内容移动到嵌套布局中。在 View 持有者中分配对可滑动内容的引用。然后只需在 onChildDraw 中对可滑动内容调用 setTranslationX,例如:

((MyViewHolder) viewHolder).swipeableContent.setTranslationX(dX);

更新源代码示例:

Recycler 查看项目布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/recycler_view_item_height"
android:background="@color/transparent">

<!-- background color view -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple" />

<!-- check symbol image view -->
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/check_symbol_width"
android:contentDescription="@null"
android:src="@drawable/check_symbol" />

<!-- swipeable content container -->
<LinearLayout
android:id="@+id/swipeable_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- here goes your swipeable content -->
</LinearLayout>

</RelativeLayout>

查看持有者类:

class MyViewHolder extends RecyclerView.ViewHolder {

// Swipeable content container.
LinearLayout swipeableContent;

/*
... here goes other sub-views
*/

public ExpenseViewHolder(final View itemView) {
super(itemView);
// bind swipeable content container view
swipeableContent = (LinearLayout) itemView.findViewById(R.id.swipeable_content);

/*
... bind other sub-views
*/
}
}

ItemTouchHelper.Callback 子类:

class ItemTouchHelperCallback extends ItemTouchHelper.Callback {

@Override
public void onChildDraw(final Canvas c,
final RecyclerView recyclerView,
final RecyclerView.ViewHolder viewHolder,
final float dX,
final float dY,
final int actionState,
final boolean isCurrentlyActive) {
if (viewHolder instanceof MyViewHolder) {
// translate by swipe amount,
// the check symbol and the background will stay in place
((MyViewHolder) viewHolder).swipeableContent.setTranslationX(dX);
}
}

}

关于android - RecyclerView ItemTouchHelper - 自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31777677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com