Android RecyclerViewSwipeDismiss:水平、垂直方向的拖曳删除item
RecyclerViewSwipeDismiss是一种支持RecyclerView的水平、垂直方向拖曳删除RecyclerView里面条目item的第三方开源项目,RecyclerViewSwipeDismiss在gitbub上的项目主页是:https://github.com/CodeFalling/RecyclerViewSwipeDismiss
我在以前写过类似的项目内容,最典型的就是腾讯QQ的消息列表,见附录文章1所简介,QQ的消息列表中某一个条目左滑以后会滑出一系列选项,比如删除、置顶等。同样,也有像附录文章2那样左滑右滑可以删除ListView内容的开源项目。
这篇文章要介绍的RecyclerViewSwipeDismiss,除了支持RecyclerView这个最新的“ListView”外,RecyclerViewSwipeDismiss还支持垂直方向的上滑/下滑,以及水平方向的左滑/右滑删除,这和附录文章1,2的开源项目相比较有其应用场景特色,假设有一个水平方向的RecyclerView,那么我可以设置成垂直方向的滑动删除;假设是一个垂直方向的RecyclerView,那么我可以设置成水平方向的左滑/右滑删除。
写一个简单的例子,测试的MainActivity.java:
package zhangphil.demo; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import java.util.LinkedList; import java.util.List; import io.github.codefalling.recyclerviewswipedismiss.SwipeDismissRecyclerViewTouchListener; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(this); horizontalLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(horizontalLayoutManager); List<String> data = new LinkedList<String>(); for (int i = 0; i < 100; i++) data.add("数据:" + i); final MyAdapter adapter = new MyAdapter(data); mRecyclerView.setAdapter(adapter); SwipeDismissRecyclerViewTouchListener listener = new SwipeDismissRecyclerViewTouchListener.Builder( mRecyclerView, new SwipeDismissRecyclerViewTouchListener.DismissCallbacks() { @Override public boolean canDismiss(int position) { return true; } @Override public void onDismiss(View view) { int id = mRecyclerView.getChildPosition(view); adapter.data.remove(id); adapter.notifyDataSetChanged(); Toast.makeText(getApplication(), String.format("删除 %d", id), Toast.LENGTH_SHORT).show(); } }) .setIsVertical(true) /**true为垂直方向拖曳删除,如果是false,那么是水平方向的拖曳删除*/ .create(); mRecyclerView.setOnTouchListener(listener); } public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { public List<String> data; public MyAdapter(List<String> data) { super(); this.data = data; } @Override public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = View.inflate(viewGroup.getContext(), android.R.layout.simple_list_item_1, null); MyViewHolder holder = new MyViewHolder(view); return holder; } @Override public void onBindViewHolder(MyViewHolder viewHolder, int i) { viewHolder.mTextView.setText(data.get(i)); } @Override public int getItemCount() { return data.size(); } } private class MyViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public MyViewHolder(View itemView) { super(itemView); mTextView = (TextView) itemView; } } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> </RelativeLayout>
运行结果:
在设计时候要注意一些常识,比如,如果设计的是一个水平的RecyclerView,那么比较自然的滑动删除方向是垂直的上滑/下滑;而如果是一个垂直方向的RecyclerView,那么比较自然的滑动删除方向是水平的左滑/右滑。RecyclerViewSwipeDismiss通过setIsVertical(boolean b)布尔值控制,true为垂直方向的上下滑动删除,false则是水平方向的左右滑动删除,开发者应该设计相配套的RecyclerView方向。
附录文章:
1,《Android SwipeMenuListView》链接地址:http://blog.csdn.net/zhangphil/article/details/46999127
2,《Android SwipeToDismiss:左滑/右滑删除ListView条目Item》链接地址:http://blog.csdn.net/zhangphil/article/details/47081863