以前觉得item上移下移侧滑删除挺难的,也不好写直接用人家的轮子就好了,可是这几天看到别人写的这种效果,用RecyclerView来写也不是很难吗?
思路
RecyclerView有个工具类,ItemTouchHelper,借助这个工具类就可以实现上面全部的效果了,主要用到ItemTouchHelper.CallBall这个接口。这里有三个主要用到的重写方法onMovementFlage() 、onMove()、onSwiped(),具体用法看代码。
item布局
<?xml version="1.0" encoding="utf-8"?>
<!--背景view,用于提示用户操作结果-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_background"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
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/ic_launcher"
/>
<!--滑动操作的view-->
<LinearLayout
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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:text="Item" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/ll_item"
android:background="#B2B2b2" />
</RelativeLayout>
主要代码
ItemTouchHelper.Callback callback = new ItemTouchHelper.Callback() {
//判断手指滑动方向
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int drag = ItemTouchHelper.UP|ItemTouchHelper.DOWN;
int swipe =ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
return makeMovementFlags(drag,swipe);
}
//上下移动Item
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//判断移动的item和目的位置是不是同种item
if(viewHolder.getItemViewType() != target.getItemViewType())
{
return false;
}
((MyViewHolder)viewHolder).ivDown.setVisibility(View.GONE);
adapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
return true;
}
//左右滑动item
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
((MyViewHolder)viewHolder).ivDown.setVisibility(View.VISIBLE);
adapter.onItemSwipe(viewHolder.getAdapterPosition());
}
//选择一个item
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
if(viewHolder != null)
{
getDefaultUIUtil().onSelected(((MyViewHolder)viewHolder).llitem);
}
}
//重绘item,在item下面
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
getDefaultUIUtil().onDraw(c,recyclerView,((MyViewHolder)viewHolder).llitem, dX, dY, actionState, isCurrentlyActive);
}
//重绘item,在item上面
@Override
public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
getDefaultUIUtil().onDraw(c,recyclerView,((MyViewHolder)viewHolder).llitem, dX, dY, actionState, isCurrentlyActive);
}
//清理视图,回到默认状态
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
getDefaultUIUtil().clearView(((MyViewHolder)viewHolder).llitem);
}
};
adapter
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_recycler,parent,false);
return new MyViewHolder(view);
}
//判断滑动的方向
void onItemMove(int from,int to)
{
if(from<to)
{
for(int i=0;i<to-from;i++)
{
Collections.swap(data,from+i,from+1+i);
}
}
if(from>to)
{
for(int i=0;i<from-to;i++)
Collections.swap(data,from-1-i,from-i);
}
adapter.notifyItemMoved(from,to);
}
//删除一个item
void onItemSwipe(int position){
data.remove(position);
adapter.notifyDataSetChanged();
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((MyViewHolder)holder).textView .setText(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ImageView ivDown;
public LinearLayout llitem;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
ivDown = (ImageView) itemView.findViewById(R.id.iv_done);
llitem = (LinearLayout) itemView.findViewById(R.id.ll_item);
}
}
设置RecyclerView
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new RecyclerAdapter();
ItemTouchHelper helper = new ItemTouchHelper(callback);
//RecyclerView与ItemTouchHelper连接起来
helper.attachToRecyclerView(mRecyclerView);
mRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();