以编程方式“轻扫至撤消"操作, ListView/RecyclerView中的项目? [英] Programmatically "swipe-to-dismiss" an item in a ListView/RecyclerView?

查看:76
本文介绍了以编程方式“轻扫至撤消"操作, ListView/RecyclerView中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够以编程方式消除RecyclerView中的某个项目,而无需用户实际刷卡(相反,我想让他们点击卡中的按钮时消除该项目).我见过的许多图书馆似乎只支持实际的刷卡操作.

I need to be able to programmatically dismiss an item inside a RecyclerView without the user actually swiping (instead I want to dismiss the item when they tap a button in the card). A lot of libraries I've seen only seem to support actual swipes.

我尝试使用现有的库,只是通过以编程方式自己创建滑动来模拟MotionEvent,但这会干扰另一个水平滑动的侦听器,因此我想知道如何进一步实现此理想RecyclerView,但是如果有人知道如何使用ListView,我可以尝试改编.

I've tried using an existing library and just simulate a MotionEvent by creating a swipe on my own programmatically, but this interferes with another horizontal-swipe listener, so I'm wondering how else this could be done, ideally for a RecyclerView but if anyone knows how to for a ListView instead I can try to adapt that.

我已经查看了库以及其他库以获取灵感,但是我可以而是要弄清楚如何以编程方式触发滑动.

I've looked at this library as well as others for inspiration but I can't figure out how to trigger the swipes programmatically instead.

推荐答案

在自定义适配器中使用ListViewRecyclerView,并在从数据列表中删除项目后调用notifyDataSetChanged:

Use a ListView or RecyclerView with custom adapter, and call notifyDataSetChanged after removing an item from the datalist:

private void removeListItem(View rowView, final int position) {

    Animation anim = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_out_right);
    anim.setDuration(500);
    rowView.startAnimation(anim);

    new Handler().postDelayed(new Runnable() {

        public void run() {

            values.remove(position); //Remove the current content from the array

            adapter.notifyDataSetChanged(); //Refresh list
        }

    }, anim.getDuration());
}

这篇关于以编程方式“轻扫至撤消"操作, ListView/RecyclerView中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆