https://github.com/h6ah4i/android-advancedrecyclerview
这似乎是一款功能丰富的库,但它缺乏良好的文档。它有一个“教程”,介绍了如何使用可滑动(Swipeable
)的项,但与其他人一样,我也无法理解。
是否有人可以提供运行示例或制作一个简单的使用案例,使用此库滑动项目并在其下方显示按钮?这对于许多对此功能感兴趣的人很有用。
https://github.com/h6ah4i/android-advancedrecyclerview
这似乎是一款功能丰富的库,但它缺乏良好的文档。它有一个“教程”,介绍了如何使用可滑动(Swipeable
)的项,但与其他人一样,我也无法理解。
是否有人可以提供运行示例或制作一个简单的使用案例,使用此库滑动项目并在其下方显示按钮?这对于许多对此功能感兴趣的人很有用。
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. 修改条目视图的布局文件
将内容视图包裹在另一个带有@+id/container
ID的FrameLayout
中。
<!-- 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>
步骤三. 修改 ViewHolder
AbstractSwipeableItemViewHolder
。getSwipeableContainerView()
方法。注意:
AbstractSwipeableItemViewHolder
类是一个方便的类,它实现了 `SwipeableItemViewHolder` 的样板方法。
class MyAdapter ... {
static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
MyViewHolder(View v) {
super(v);
textView = (TextView) v.findViewById(android.R.id.text1);
}
}
...
}
⏬ ⏬ ⏬
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;
}
}
}
SwipeableItemAdapter
接口。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();
}
}
}
步骤五. 修改RecyclerView
的初始化过程
在你的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);
}
我希望我的答案能够帮到你。
createAdapter
方法提供了一个概述,指出哪个活动包含哪个功能。复杂示例
在复杂示例中,适配器和视图持有者是分别创建的,而且回收站视图本身定义在另一个片段中。
在这种情况下,在活动中会添加额外的片段。这些都在com.h6ah4i.android.example.advrecyclerview.common.fragment
包中,用于提供需要显示在回收站视图中的数据。
对于带按钮的滑动,您需要创建
RecyclerViewTouchActionGuardManager
(以抑制滑动时滑动关闭动画运行)和RecyclerViewSwipeManager
以创建包装适配器。
对于适配器,您需要实现SwipeableItemAdapter
接口,视图持有者需要扩展AbstractSwipeableItemViewHolder
而不是RecyclerView.ViewHolder。
注意:
我已经更改了onSetSwipeBackground
的实现
在原始示例中,它在itemview上设置了一些背景。
在需要显示下面的视图的情况下,这是不必要的。此外,它会导致不必要的重绘。