あなたがメインのウェブサイトでより良い詳細なドキュメントを見つけることができます: https://advancedrecyclerview.h6ah4i.com
と、次のドキュメントにswipeableページからコピーされます。
ステップ1.アダプターは、安定したサポートしてくださいID
この手順は非常に重要です。アダプターは、いくつかの奇妙な行動が発生します安定&固有のID、(間違ったアニメーション、NPE、など...)アイテムビュー
の
class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
MyAdapter() {
setHasStableIds(true);
}
@Override
public long getItemId(int position) {
// requires static value, it means need to keep the same value
// even if the item position has been changed.
return mItems.get(position).getId();
}
}
ステップ2.修正レイアウトファイルを返さない場合
別のFrameLayout
のwhitchでラップするコンテンツビューは@+id/container
IDです。 AbstractSwipeableItemViewHolder
へ
<!-- for itemView -->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp">
<!-- Content View(s) -->
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"/>
</FrameLayout>
⏬⏬⏬
<!-- for itemView -->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp">
<!-- for getSwipeableContainerView() -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content View(s) -->
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"/>
</FrameLayout>
</FrameLayout>
ステップ3.変更ViewHolder
- 変更親クラス。
getSwipeableContainerView()
メソッドを実装します。
注:AbstractSwipeableItemViewHolder
クラスSwipeableItemViewHolder `のboilerplace方法を実装する便利なクラスです。
⏬⏬⏬
class MyAdapter ... {
static class MyViewHolder extends AbstractSwipeableItemViewHolder {
TextView textView;
FrameLayout containerView;
public MyViewHolder(View v) {
super(v);
textView = (TextView) v.findViewById(android.R.id.text1);
containerView = (FrameLayout) v.findViewById(R.id.container);
}
@Override
public View getSwipeableContainerView() {
return containerView;
}
}
}
ステップ4. SwipeableItemAdapter
インターフェース
class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
...
}
⏬⏬⏬
を実装しますRecyclerView
の
class MyAdapter
extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
implements SwipeableItemAdapter<MyAdapter.MyViewHolder> {
@Override
public int onGetSwipeReactionType(MyViewHolder holder, int position, int x, int y) {
// Make swipeable to LEFT direction
return Swipeable.REACTION_CAN_SWIPE_LEFT;
}
@Override
public void onSetSwipeBackground(MyViewHolder holder, int position, int type) {
// You can set background color/resource to holder.itemView.
// The argument "type" can be one of the followings;
// - Swipeable.DRAWABLE_SWIPE_NEUTRAL_BACKGROUND
// - Swipeable.DRAWABLE_SWIPE_LEFT_BACKGROUND
// (- Swipeable.DRAWABLE_SWIPE_UP_BACKGROUND)
// (- Swipeable.DRAWABLE_SWIPE_RIGHT_BACKGROUND)
// (- Swipeable.DRAWABLE_SWIPE_DOWN_BACKGROUND)
if (type == Swipeable.DRAWABLE_SWIPE_LEFT_BACKGROUND) {
holder.itemView.setBackgroundColor(Color.YELLOW);
} else {
holder.itemView.setBackgroundColor(Color.TRANSPARENT);
}
}
@Override
public SwipeResultAction onSwipeItem(MyViewHolder holder, int position, int result) {
// Return sub class of the SwipeResultAction.
//
// Available base (abstract) classes are;
// - SwipeResultActionDefault
// - SwipeResultActionMoveToSwipedDirection
// - SwipeResultActionRemoveItem
// - SwipeResultActionDoNothing
// The argument "result" can be one of the followings;
//
// - Swipeable.RESULT_CANCELED
// - Swipeable.RESULT_SWIPED_LEFT
// (- Swipeable.RESULT_SWIPED_UP)
// (- Swipeable.RESULT_SWIPED_RIGHT)
// (- Swipeable.RESULT_SWIPED_DOWN)
if (result == Swipeable.RESULT_LEFT) {
return new SwipeResultActionMoveToSwipedDirection() {
// Optionally, you can override these three methods
// - void onPerformAction()
// - void onSlideAnimationEnd()
// - void onCleanUp()
};
} else {
return new SwipeResultActionDoNothing();
}
}
}
ステップ5変更の初期化プロセスは、あなたのActivity
/Fragment
にいくつかの追加の初期化プロセスを置きます。
- インスタンス化
RecyclerViewSwipeManager
。
- ラップされたアダプタを作成し、
RecyclerView
に設定します。
RecyclerView
からRecyclerViewSwipeManager
を付けます。
void onCreate() {
...
RecyclerView recyclerView = findViewById(R.id.recyclerView);
MyAdapter adapter = new MyAdapter();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
⏬⏬⏬
void onCreate() {
...
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerViewSwipeManager swipeManager = new RecyclerViewSwipeManager();
MyAdapter adapter = new MyAdapter();
RecyclerView.Adapter wrappedAdapter = swipeManager.createWrappedAdapter(adapter);
recyclerView.setAdapter(wrappedAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// disable change animations
((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
swipeManager.attachRecyclerView(recyclerView);
}
私は私の答えは役立つことを願っています。
私はプロジェクトに依存関係を追加したときにドキュメントを見つけることができません。それは非常によく、それはそれを使用することになると、しかし、サンプルアプリケーションを使用して行うことができます...私は失った。このライブラリについてもっと知っている人がいらっしゃいましたら、私は代替案を試してみたいと思っています。/ – Jethro
もう少し詳しく書かれた[代替](https://github.com/davideas/FlexibleAdapter/wiki)があります。 –
うわーこれは私の質問や欲望の答えかもしれないと思います!大幅に文書化されているようですが、まだ見ていません。どうもありがとうございました。あなたが望むなら、あなたのコメントを答えにしてください。 – iBobb