Programmatically “swipe-to-dismiss” an item in a ListView/RecyclerView?

橙三吉。 提交于 2019-12-11 11:56:29

问题


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.

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.


回答1:


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());
}



回答2:


Use one of the libraries that offer the swipe to dissmis funcionality ad extract the animation part, if im not mistaken its at the action_up at the onTouch(). Then call it from your onClick of the button.



来源:https://stackoverflow.com/questions/30650286/programmatically-swipe-to-dismiss-an-item-in-a-listview-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!