高级RecyclerView库-代码示例

11

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

这似乎是一款功能丰富的库,但它缺乏良好的文档。它有一个“教程”,介绍了如何使用可滑动(Swipeable)的项,但与其他人一样,我也无法理解。

是否有人可以提供运行示例或制作一个简单的使用案例,使用此库滑动项目并在其下方显示按钮?这对于许多对此功能感兴趣的人很有用。


1
我在将依赖项添加到项目后找不到任何文档。它使用示例应用程序非常清楚地说明了它的功能,但是当涉及到使用它时...我迷失了方向。希望你能找到更多了解这个库的人,我正在考虑尝试其他替代方案 :/ - Jethro
另一种选择,文档略好一些。 - S.D.
哇,我觉得这可能是我问题和需求的答案!看起来文档也很好,但我还没有看过。非常感谢。如果您愿意,可以将您的评论作为答案。 - iBobb
如果您想要滑动或拖动,您可以使用ItemTouchHelper和RecyclerView轻松实现,而无需添加库作为依赖项,请参考此处的教程:https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf#.x8ka8qcw7至于按钮下面的部分,您只需要限制滑动动画足够远以暴露您的按钮并处理其单击即可。上述教程有第二部分,其中包含自定义滑动动画的部分:https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-6a6f0c422efd - Dom
2个回答

3
您可以在主网站上找到更详细的文档: https://advancedrecyclerview.h6ah4i.com 以下内容摘自文档中的可滑动页面:
步骤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. 修改条目视图的布局文件

将内容视图包裹在另一个带有@+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

  1. 将父类更改为 AbstractSwipeableItemViewHolder
  2. 实现 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中添加一些额外的初始化过程。
  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);
}

我希望我的答案能够帮到你。


虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅链接的答案可能会失效。-【来自审查】 - Dev-iL
@Dev-iL,你能否现在修改一下评论? - Zacktamondo
嗨,@Zacktamondo,我不确定你的意思是什么 - 这篇评论是针对帖子之前的版本,所以每个看到它的人都会看到帖子是在之后进行了编辑 - 这是评论审核的预期结果。我没有给你-1,而且不管怎样,看到你是一个新用户,你的行为在SO上得到鼓励(即通过编辑帖子来回应评论审核),你可以得到我的+1 :) - Dev-iL
@Dev-iL 谢谢你。你建议我删除并重新发布答案吗? - Zacktamondo
请不要这样做。您在此答案上有一个+1/-1,这意味着您的声誉值净增加。删除答案只会伤害您,但更重要的是,在此网站上重新发布相同的内容是不被赞同的。 - Dev-iL

1
我发现这个库的文档很完善,易于使用。
我从原始示例中选择了所需的代码来实现带有按钮下方滑动的功能,可以在这里找到。
希望以下提示能够更容易地理解样本实现的模式。
概述 LauncherPageFragment中的createAdapter方法提供了一个概述,指出哪个活动包含哪个功能。
每个示例都遵循以下两种模式之一:
基本示例
在基本示例中,用于回收视图的适配器和视图持有者在同一个活动类中定义。

复杂示例
在复杂示例中,适配器和视图持有者是分别创建的,而且回收站视图本身定义在另一个片段中。
在这种情况下,在活动中会添加额外的片段。这些都在com.h6ah4i.android.example.advrecyclerview.common.fragment包中,用于提供需要显示在回收站视图中的数据。

对于带按钮的滑动,您需要创建 RecyclerViewTouchActionGuardManager(以抑制滑动时滑动关闭动画运行)和RecyclerViewSwipeManager以创建包装适配器。

对于适配器,您需要实现SwipeableItemAdapter接口,视图持有者需要扩展AbstractSwipeableItemViewHolder而不是RecyclerView.ViewHolder。

注意: 我已经更改了onSetSwipeBackground的实现 在原始示例中,它在itemview上设置了一些背景。
在需要显示下面的视图的情况下,这是不必要的。此外,它会导致不必要的重绘。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接