完成类似于下图的效果:
1.侧滑删除
2.拖拽移动
一.写一个接口,TouchCallBack
public interface TouchCallBack {
//数据交换
void onItemMove(int fromPosition, int toPosition);
//数据删除
void onItemDelete(int position);
}
二.RecyclerView的适配器实现这个接口
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> implements TouchCallBack {
private Context context;
private ArrayList<String> list;
public MyAdapter(Context context, ArrayList<String> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_item, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
String str = list.get(i);
viewHolder.tv.setText(str);
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onItemMove(int fromPosition, int toPosition) {
//交换位置
Collections.swap(list, fromPosition, toPosition);
//局部刷新(移动)
notifyItemMoved(fromPosition, toPosition);
}
@Override
public void onItemDelete(int position) {
//删除数据
list.remove(position);
//局部刷新(移除)
notifyItemRemoved(position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
}
}
}
三.需要用到一个类,ItemTouchCallBack ,它是RecyclerView.ItemDecoration的子类.
这是一个工具类,可实现滑动删除和拖拽移动,使用这个工具类需要RecyclerView和Callback,它配置了启用了何种类型的交互,并且当用户执行这些操作时也会接收事件。
这个类中有如下方法可以实现侧滑删除和拖拽移动功能:
public class SimpleItemTouchCallBack extends ItemTouchHelper.Callback {
private TouchCallBack mCallBack;
//左滑删除的
private boolean mSwipeEnable = true;
public SimpleItemTouchCallBack(TouchCallBack mCallBack) {
this.mCallBack = mCallBack;
}
/**
* 返回可以滑动的方向,一般使用makeMovementFlags(int,int)
* 或makeFlag(int, int)来构造我们的返回值
*
* @param recyclerView
* @param viewHolder
* @return
*/
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
//允许上下拖拽
int drag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
//允许向左滑动
int swipe = ItemTouchHelper.LEFT;
//设置
return makeMovementFlags(drag, swipe);
}
/**
* 上下拖动item时回调,可以调用Adapter的notifyItemMoved方法来交换两个ViewHolder的位置,
* 最后返回true,
* 表示被拖动的ViewHolder已经移动到了目的位置
*
* @param recyclerView
* @param viewHolder
* @param target
* @return
*/
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
//通知适配器,两个子条目位置发生改变
mCallBack.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
/**
* 当用户左右滑动item时达到删除条件就会调用,一般为一半,条目继续滑动删除,否则弹回
*
* @param viewHolder
* @param direction
*/
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mCallBack.onItemDelete(viewHolder.getAdapterPosition());
}
/**
* 支持长按拖动,默认是true
*
* @return
*/
@Override
public boolean isLongPressDragEnabled() {
return super.isLongPressDragEnabled();
}
/**
* 支持滑动,即可以调用到onSwiped()方法,默认是true
*
* @return
*/
@Override
public boolean isItemViewSwipeEnabled() {
return mSwipeEnable;
}
/**
* 设置是否支持左滑删除
*
* @param enable
*/
public void setmSwipeEnable(boolean enable) {
this.mSwipeEnable = enable;
}
}
四、在MainActivity中
public class MainActivity extends AppCompatActivity {
private RecyclerView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
lv = (RecyclerView) findViewById(R.id.lv);
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 8; i++) {
list.add("安琪拉"+i);
}
MyAdapter adapter = new MyAdapter(this, list);
lv.setAdapter(adapter);
lv.setLayoutManager(new LinearLayoutManager(this));
// 拖拽移动和左滑删除
SimpleItemTouchCallBack simpleItemTouchCallBack = new SimpleItemTouchCallBack(adapter);
// 要实现侧滑删除条目,把 false 改成 true 就可以了
simpleItemTouchCallBack.setmSwipeEnable(false);
ItemTouchHelper helper = new ItemTouchHelper(simpleItemTouchCallBack);
helper.attachToRecyclerView(lv);
}
}
网友评论