[英]RecyclerView ItemTouchHelper - Custom View
I am trying to have a custom view (or image) appear when swiping left/right. 我试图在向左/向右滑动时显示自定义视图(或图像)。
I'm close to having it working, however I'm having issues with using a custom image. 我即将开始使用它,但是在使用自定义图像时遇到了问题。
The effect I'm going for is similar to this: RecyclerView ItemTouchHelper swipe remove animation 我要达到的效果与此类似: RecyclerView ItemTouchHelper滑动删除动画
See what's happening below. 看看下面发生了什么。 It's warping the image as the swipe happens:
滑动时会扭曲图像:
Below is what my code looks like: 以下是我的代码:
@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);
}
}
How can I keep the image static while continuing to draw the background color? 在继续绘制背景色的同时如何保持图像静止? Is it possible to even use an Android view instead of an image?
是否可以甚至使用Android视图代替图像? Similar to this: https://github.com/wdullaer/SwipeActionAdapter
与此类似: https : //github.com/wdullaer/SwipeActionAdapter
You can prepare your recycler view item layout to include the check symbol ImageView
and maybe another blank view with just the background color. 您可以准备回收者视图项目布局,以包括复选标记
ImageView
以及可能仅包含背景颜色的另一个空白视图。 You should also move all the swipeable content into a nested layout. 您还应该将所有可滑动内容移动到嵌套布局中。 Assign a reference to the swipeable content in your view holder.
为视图持有者中的可滑动内容分配引用。 Then just call
setTranslationX
on your swipeable content in onChildDraw
, like: 然后只需在
onChildDraw
的可滑动内容上调用setTranslationX
onChildDraw
,例如:
((MyViewHolder) viewHolder).swipeableContent.setTranslationX(dX);
Recycler view item layout: 回收者视图项目布局:
<?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>
View holder class: 查看持有人类别:
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 subclass: 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);
}
}
}
I am not sure whether you got solution or not and also not sure whether you have seen this library or not. 我不确定您是否有解决方案,也不确定您是否已看到此库。
I have try Android-SwipeListView library for one of my project and SwipeMenuListView for another project for something similar requirements. 我有尝试Android的SwipeListView库为我的项目之一, SwipeMenuListView另一个项目的东西类似的要求。
Both are working fine in their place. 两者都在他们的位置工作正常。 You should give a try on that and check how it should work for your requirement.
您应该尝试一下,并检查它应如何满足您的要求。
Let me know if you need more help from me. 如果您需要我的更多帮助,请告诉我。
Enjoy Coding... :) 享受编码... :)
Simply change the (int)dX
in d.setBounds() to any integer
you want. 只需将d.setBounds()中的
(int)dX
更改为所需的任何integer
。
eg: 例如:
d.setBounds(itemView.getLeft(), itemView.getTop(), 50, itemView.getBottom());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.