这是一个使用RecyclerView,做的一个排序例子,利用到ItemTouchHelper,实现手势识别功能
我们首先需要引RecyclerView jar包,
6.png然后我们在Activity中使用RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".adddelete.AddActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/addrv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
接下来开始操作!
我们首先需要一个ItemTouchHelperAdapter的接口
package com.example.ryan.itemtouch.adddelete;
public interface IOperationData {
/**
* 数据交换
* @param fromPosition
* @param toPosition
*/
void onItemMove(int fromPosition,int toPosition);
/**
* 数据删除
* @param position
*/
void onItemDissmiss(int position);
}
在接口中生死线数据变换和删除的功能
然后我们重写RecyclerViewAdapter
package com.example.ryan.itemtouch.adddelete;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.ryan.itemtouch.R;
import com.example.ryan.itemtouch.simple.SimpleAdapterDemo;
import java.util.Collections;
import java.util.List;
public class AddAdapterDemo extends RecyclerView.Adapter<AddAdapterDemo.DragViewHolder> implements IOperationData {
private Context context;
private List<String> list;
//
public AddAdapterDemo(Context context, List<String> list) {
this.context=context;
this.list=list;
}
@NonNull
@Override
public DragViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new DragViewHolder(LayoutInflater.from(context).inflate(R.layout.item_move,viewGroup,false));
}
@Override
public void onBindViewHolder(@NonNull DragViewHolder dragViewHolder, int i) {
dragViewHolder.tv_move.setText(list.get(i));
}
@Override
public int getItemCount() {
// mDataList == null ? 0 : mDataList.size()
return list == null ? 0 : list.size();
}
@Override
public void onItemMove(int fromPosition, int toPosition) {
//交换位置
Collections.swap(list,fromPosition,toPosition);
notifyItemMoved(fromPosition,toPosition);
}
@Override
public void onItemDissmiss(int position) {
//移除数据
list.remove(position);
notifyItemRemoved(position);
}
//创建ViewHolder
public class DragViewHolder extends RecyclerView.ViewHolder {
TextView tv_move;
public DragViewHolder(@NonNull View itemView) {
super(itemView);
tv_move=itemView.findViewById(R.id.tv_move);
}
}
}
我们还需要重写一个ItemTouchHelper
package com.example.ryan.itemtouch.adddelete;
import android.graphics.Canvas;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
public class TouchHelperDemo extends android.support.v7.widget.helper.ItemTouchHelper.Callback {
private AddAdapterDemo addAdapterDemo;
public TouchHelperDemo(AddAdapterDemo addAdapterDemo) {
this.addAdapterDemo=addAdapterDemo;
}
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
//允许上下得拖动
int dragFlags= android.support.v7.widget.helper.ItemTouchHelper.UP| android.support.v7.widget.helper.ItemTouchHelper.DOWN;
//允许左右拖动
int swipeFlags = android.support.v7.widget.helper.ItemTouchHelper.RIGHT|android.support.v7.widget.helper.ItemTouchHelper.LEFT;
return makeMovementFlags(dragFlags,swipeFlags);
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
addAdapterDemo.onItemMove(viewHolder.getAdapterPosition(),viewHolder1.getAdapterPosition());
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
addAdapterDemo.onItemDissmiss(viewHolder.getAdapterPosition());
}
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if(actionState == android.support.v7.widget.helper.ItemTouchHelper.ACTION_STATE_SWIPE) {
//滑动时改变Item的透明度
final float alpha = 1 - Math.abs(dX) / (float)viewHolder.itemView.getWidth();
viewHolder.itemView.setAlpha(alpha);
}
}
}
最后MainActivity开始引入
package com.example.ryan.itemtouch.adddelete;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import com.example.ryan.itemtouch.R;
import java.util.ArrayList;
import java.util.List;
public class AddActivity extends AppCompatActivity {
private RecyclerView addrv;
private AddAdapterDemo adapterDemo;
private List<String> list;
private TouchHelperDemo touchHelperDemo;
private ItemTouchHelper.Callback callback;
private ItemTouchHelper itemTouchHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
initView();
}
private void initView() {
addrv = findViewById(R.id.addrv);
List<String> list= new ArrayList<>();
for (int i = 0; i < 18; i++) {
list.add("Index " + i);
}
adapterDemo=new AddAdapterDemo(this,list);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
callback = new TouchHelperDemo(adapterDemo);
addrv.setAdapter(adapterDemo);
addrv.setLayoutManager(manager);
itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(addrv);
}
}
网友评论