如何使用支持库在android中滑动以删除cardview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26422948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:22:42  来源:igfitidea点击:

How to do swipe to delete cardview in android using support library

androidandroid-support-library

提问by N Sharma

Hi I am using support library android.support.v7.widget.CardViewto achieve cardview functionality in my android app. I want to achieve swipe to delete functionality in it as well.

嗨,我正在使用支持库android.support.v7.widget.CardView在我的 android 应用程序中实现 cardview 功能。我也想实现滑动以删除其中的功能。

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="200dp"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v7.widget.CardView>

How to achieve swipe to delete functionality in it?

里面怎么实现刷卡删除功能呢?

Thanks in advance.

提前致谢。

回答by brnunes

I adapted romannurik's Android-SwipeToDismissto do exactly that.

我改编了romannurik 的 Android-SwipeToDismiss来做到这一点。

The code is on githubwith a woking sample application, and consists of:

代码位于 github 上,包含一个 woking 示例应用程序,包括:

  • A subclass of RecyclerView.OnItemTouchListenerthat listens to touch events and detects when an item is being swiped, animating it accordingly.
  • A SwipeListenerthat is called in order to know if an item can be dismissed and called again when items are dismissed.
  • 它的子类RecyclerView.OnItemTouchListener侦听触摸事件并检测何时滑动项目,并相应地对其进行动画处理。
  • 一个SwipeListener被称为要想知道如果一个项目可以被排除,再次调用时的项目被解雇。

To use it, follow the instructions on github, or just copy the class SwipeableRecyclerViewTouchListenerto your project and use it like this:

要使用它,请按照 github 上的说明进行操作,或者只是将类复制SwipeableRecyclerViewTouchListener到您的项目中并像这样使用它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mItems = new ArrayList<>(30);
    for (int i = 0; i < 30; i++) {
        mItems.add(String.format("Card number %2d", i));
    }

    mAdapter = new CardViewAdapter(mItems);

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setAdapter(mAdapter);

    SwipeableRecyclerViewTouchListener swipeTouchListener =
            new SwipeableRecyclerViewTouchListener(mRecyclerView,
                    new SwipeableRecyclerViewTouchListener.SwipeListener() {
                        @Override
                        public boolean canSwipe(int position) {
                            return true;
                        }

                        @Override
                        public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                mItems.remove(position);
                                mAdapter.notifyItemRemoved(position);
                            }
                            mAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                mItems.remove(position);
                                mAdapter.notifyItemRemoved(position);
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                    });

    mRecyclerView.addOnItemTouchListener(swipeTouchListener);
}

回答by Avi Levin

There is a new approach for swipe delete gesture in android support v7 API. The class name is called ItemTouchHelper.

在 android support v7 API 中有一种新的滑动删除手势方法。类名称为ItemTouchHelper

Mr. Paul Burke wrote an amazing example on how to implement this feature. See this link.

Paul Burke 先生写了一个关于如何实现此功能的惊人示例。请参阅此链接

回答by lostknight

Adding due to this being the first result.

由于这是第一个结果而添加。

I was looking for something like this and was having some issues, checked out the first two answers but this was the easiest one I found to implement. Simply stick it in the activity and change the itemlist/adapater/recyclerview to whatever your name is.

我正在寻找这样的东西并且遇到了一些问题,检查了前两个答案,但这是我发现实现的最简单的一个。只需将其粘贴到活动中并将 itemlist/adapater/recyclerview 更改为您的名字即可。

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new 
ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {

        ItemList.remove(viewHolder.getAdapterPosition());
        Adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
    }

    @Override
    public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
        super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
    }
};

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(RecyclerViewer);

found here: codeproject

在这里找到:代码项目