![](/img/trans.png)
[英]Swipe to another view in recyclerView with ItemTouchHelper
[英]RecyclerView ItemTouchHelper - Custom View
我试图在向左/向右滑动时显示自定义视图(或图像)。
我即将开始使用它,但是在使用自定义图像时遇到了问题。
我要达到的效果与此类似: RecyclerView ItemTouchHelper滑动删除动画
看看下面发生了什么。 滑动时会扭曲图像:
以下是我的代码:
@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视图代替图像? 与此类似: https : //github.com/wdullaer/SwipeActionAdapter
您可以准备回收者视图项目布局,以包括复选标记ImageView
以及可能仅包含背景颜色的另一个空白视图。 您还应该将所有可滑动内容移动到嵌套布局中。 为视图持有者中的可滑动内容分配引用。 然后只需在onChildDraw
的可滑动内容上调用setTranslationX
onChildDraw
,例如:
((MyViewHolder) viewHolder).swipeableContent.setTranslationX(dX);
回收者视图项目布局:
<?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的SwipeListView库为我的项目之一, SwipeMenuListView另一个项目的东西类似的要求。
两者都在他们的位置工作正常。 您应该尝试一下,并检查它应如何满足您的要求。
如果您需要我的更多帮助,请告诉我。
享受编码... :)
只需将d.setBounds()中的(int)dX
更改为所需的任何integer
。
例如:
d.setBounds(itemView.getLeft(), itemView.getTop(), 50, itemView.getBottom());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.