一、ItemTouchHelper介绍
This is a utility class to add swipe to dismiss and drag & drop support to RecyclerView.
It works with a RecyclerView and a Callback class, which configures what type of interactions are enabled and also receives events when user performs these actions.
itemTouchHelper是support:v7包中提供的一个工具来,使用来处理item的touch事件,主要为拖拽和侧滑事件。
构造方法为:ItemTouchHelper(ItemTouchHelper.Callback callback)
其中需要传入一个Callback对象。
Callback是一个抽象类,需要继承去实现其中的方法,在v7包中已经有一个简单的子类SimpleCallback可以直接使用。
构造方法为:SimpleCallback(int dragDirs, int swipeDirs)
dragDirs表示可以拖拽的方向
swipeDirs表示可以滑动的方向
值分别有:
LEFT
RIGHT
START
END
UP
DOWN
使用时必须重写其中的onMove(RecyclerView recyclerView,ViewHolder viewHolder, ViewHolder target)和onSwiped(ViewHolder viewHolder, int direction)两个方法。
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(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 direction) {
//处理侧滑的事件
}
});
创建好ItemTouchHelper对象后,调用attachToRecyclerView(RecyclerView recyclerView)方法传入需要操作的RecycleView。
二、实现仿Inbox的滑动操作
先看下效果:
- 布局文件
activity_sample.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SampleActivity">
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
tools:listitem="@layout/list_item_sample"/>
list_item_sample.xml
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_background"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/iv_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:src="@mipmap/bt_ic_done_white_24" />
android:id="@+id/iv_schedule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@mipmap/bt_ic_schedule_white_24" />
android:id="@+id/ll_item"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp">
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:text="Item" />
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/ll_item"
android:background="#B2B2b2" />
- Activity中实现滑动操作
ViewHolder
static class ItemViewHolder extends RecyclerView.ViewHolder {
View vBackground; // 背景
View vItem;
TextView tvItem; // 滑动的view
ImageView ivDone;
ImageView ivSchedule;
public ItemViewHolder(View itemView) {
super(itemView);
vBackground = itemView.findViewById(R.id.rl_background);
vItem = itemView.findViewById(R.id.ll_item);
tvItem = (TextView) itemView.findViewById(R.id.tv_item);
ivDone = (ImageView) itemView.findViewById(R.id.iv_done);
ivSchedule = (ImageView) itemView.findViewById(R.id.iv_schedule);
}
}
SampleActivity.java
public class SampleActivity extends AppCompatActivity {
private RecyclerView rvList;
private SampleAdapter mSampleAdapter;
private ArrayList mData = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
initView();
}
private void initView() {
rvList = (RecyclerView) findViewById(R.id.rv_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvList.setLayoutManager(linearLayoutManager);
for (int i = 0; i < 20; i++) {
mData.add("RecyclerView" + i);
}
mSampleAdapter = new SampleAdapter(this, mData);
rvList.setAdapter(mSampleAdapter);
// new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) 表示不可以拖拽,只可以在左右方向滑动
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(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 direction) {
// 处理滑动事件回调
final int pos = viewHolder.getAdapterPosition();
final String item = mData.get(pos);
mData.remove(pos);
mSampleAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
String text;
// 判断方向,进行不同的操作
if (direction == ItemTouchHelper.RIGHT) {
text = "删除一项";
} else {
text = "延迟一项";
}
Snackbar.make(viewHolder.itemView, text, Snackbar.LENGTH_LONG)
.setAction("撤销", new View.OnClickListener() {
@Override
public void onClick(View v) {
mData.add(pos, item);
mSampleAdapter.notifyItemInserted(pos);
}
}).show();
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
// 释放View时回调,清除背景颜色,隐藏图标
// 默认是操作ViewHolder的itemView,这里调用ItemTouchUIUtil的clearView方法传入指定的view
getDefaultUIUtil().clearView(((SampleAdapter.ItemViewHolder) viewHolder).vItem);
((SampleAdapter.ItemViewHolder) viewHolder).vBackground.setBackgroundColor(Color.TRANSPARENT);
((SampleAdapter.ItemViewHolder) viewHolder).ivSchedule.setVisibility(View.GONE);
((SampleAdapter.ItemViewHolder) viewHolder).ivDone.setVisibility(View.GONE);
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
// 当viewHolder的滑动或拖拽状态改变时回调
if (viewHolder != null) {
// 默认是操作ViewHolder的itemView,这里调用ItemTouchUIUtil的clearView方法传入指定的view
getDefaultUIUtil().onSelected(((SampleAdapter.ItemViewHolder) viewHolder).vItem);
}
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
// ItemTouchHelper的onDraw方法会调用该方法,可以使用Canvas对象进行绘制,绘制的图案会在RecyclerView的下方
// 默认是操作ViewHolder的itemView,这里调用ItemTouchUIUtil的clearView方法传入指定的view
getDefaultUIUtil().onDraw(c, recyclerView, ((SampleAdapter.ItemViewHolder) viewHolder).vItem, dX, dY, actionState, isCurrentlyActive);
if (dX > 0) { // 向左滑动是的提示
((SampleAdapter.ItemViewHolder) viewHolder).vBackground.setBackgroundResource(R.color.colorDone);
((SampleAdapter.ItemViewHolder) viewHolder).ivDone.setVisibility(View.VISIBLE);
((SampleAdapter.ItemViewHolder) viewHolder).ivSchedule.setVisibility(View.GONE);
}
if (dX < 0) { // 向右滑动时的提示
((SampleAdapter.ItemViewHolder) viewHolder).vBackground.setBackgroundResource(R.color.colorSchedule);
((SampleAdapter.ItemViewHolder) viewHolder).ivSchedule.setVisibility(View.VISIBLE);
((SampleAdapter.ItemViewHolder) viewHolder).ivDone.setVisibility(View.GONE);
}
}
@Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
// ItemTouchHelper的onDrawOver方法会调用该方法,可以使用Canvas对象进行绘制,绘制的图案会在RecyclerView的上方
// 默认是操作ViewHolder的itemView,这里调用ItemTouchUIUtil的clearView方法传入指定的view
getDefaultUIUtil().onDrawOver(c, recyclerView, ((SampleAdapter.ItemViewHolder) viewHolder).vItem, dX, dY, actionState, isCurrentlyActive);
}
});
itemTouchHelper.attachToRecyclerView(rvList);
}
}
默认情况下,Callback操作的是ViewHolder的itemVIew;getDefaultUIUtil()方法返回ItemTouchUIUtil对象,通过ItemTouchUIUtil的方法可以指定操作的view。
源码地址:https://github.com/zly394/RecycleViewSwipeDismiss