gpt4 book ai didi

android - 高级 RecyclerView 库 - 代码示例

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:46 26 4
gpt4 key购买 nike

https://github.com/h6ah4i/android-advancedrecyclerview

就其提供的功能而言,这似乎是一个很棒的库。但是,它缺乏良好的文档。它有一个关于 Swipeable 项目的“教程”,但像其他一些人一样我无法理解它。

有没有人有一个有效的例子,或者有没有人可以使用这个库制作一个简单的用例来滑动一个项目并在它下面显示一个按钮?它对很多对此功能感兴趣的人很有用。

最佳答案

您可以在主网站找到更详细的文档: https://advancedrecyclerview.h6ah4i.com

以下是从 swipeable 复制而来的文档中的页面:


第一步,使适配器支持稳定的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();
}
}

第二步. 修改项目 View 的布局文件

用另一个具有 @+id/container ID 的 FrameLayout 包装内容 View 。

<!-- 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

  1. 将父类更改为 AbstractSwipeableItemViewHolder
  2. 实现 getSwipeableContainerView() 方法。

Note: The AbstractSwipeableItemViewHolder class is a convenience class which implements boilerplace methods of `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;
}
}
}

第 4 步。实现 SwipeableItemAdapter 接口(interface)

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 中添加一些额外的初始化过程。

  1. 实例化 RecyclerViewSwipeManager
  2. 创建一个包装适配器并将其设置为 RecyclerView
  3. 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);
}

希望我的回答对您有所帮助。

关于android - 高级 RecyclerView 库 - 代码示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36134294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com