[英]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.