Sie können besser ausführliche Dokumentation in der Haupt-Website finden: https://advancedrecyclerview.h6ah4i.com
und die folgend von der swipeable Seite in der Dokumentation kopiert wird:
Schritt 1. die Adapter Stellen unterstützt stabil IDs
Dieser Schritt ist sehr wichtig. Falls der Adapter nicht stabil & eindeutige IDs zurückgibt, wird dazu führen, dass einige seltsame Verhalten (falsche Animationen, NPE, etc ...)
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();
}
}
Schritt 2. Ändern Layoutdatei von Element Ansichten
Wrap Inhalt Ansichten mit anderen FrameLayout
whitch hat @+id/container
ID.
<!-- 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>
Schritt 3. Ändern ViewHolder
- ändern Elternklasse
AbstractSwipeableItemViewHolder
.
- Implement
getSwipeableContainerView()
Methode.
Hinweis: Die AbstractSwipeableItemViewHolder
Klasse ist eine Komfort-Klasse, die boilerplace Methoden der `SwipeableItemViewHolder implementiert.
⏬ ⏬ ⏬
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;
}
}
}
Schritt 4. Umsetzung des SwipeableItemAdapter
Schnittstelle
class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
...
}
⏬ ⏬ ⏬
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();
}
}
}
Schritt 5. Ändern Initialisierung von RecyclerView
Legen Sie einige zusätzliche Initialisierung in Ihrem Activity
/Fragment
.
- Instanziieren
RecyclerViewSwipeManager
.
- Erstellen Sie einen verpackten Adapter und setzen Sie ihn auf
RecyclerView
.
- Befestigen Sie
RecyclerView
an 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);
}
ich meine Antwort hoffen helfen.
Ich kann keine Dokumentation finden, wenn ich die Abhängigkeit zum Projekt hinzugefügt habe. Es sagt sehr gut, was es mit der Beispiel-App tun kann, aber wenn es darum geht, es zu benutzen ... Ich bin verloren. Hoffe, du findest jemanden, der mehr über diese Bibliothek weiß, ich bin dabei, Alternativen auszuprobieren:/ – Jethro
Es gibt [eine Alternative] (https://github.com/davideas/FlexibleAdapter/wiki) mit etwas besserer Dokumentation. –
Wow, ich denke, das könnte die Antwort auf meine Fragen und Wünsche sein! Scheint auch sehr dokumentiert, aber ich muss es mir noch anschauen. Vielen Dank. Wenn Sie möchten, machen Sie Ihren Kommentar eine Antwort. – iBobb