点击添加、左滑删除、长按移动交换
1、Item 操作的接口
/**
* 作者:张风捷特烈<br/>
* 时间:2018/9/17 0017:14:18<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:Item 操作的接口
*/
public interface ItemChangeAdapter<T> {
/**
* 交换条目
*
* @param from 起点
* @param to 终点
*/
void onItemMove(int from, int to);
/**
* 删除条目
*
* @param position 位置
*/
void onItemDelete(int position);
/**
* 添加条目
*
* @param position 位置
* @param t 数据
*/
void onItemAdd(int position, T t);
}
2.RecyclerView条目回调
/**
* 作者:张风捷特烈<br/>
* 时间:2018/9/17 0017:14:41<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:RecyclerView条目回调
*/
public class ItemTouchCallback extends ItemTouchHelper.Callback{
private ItemChangeAdapter mAdapter;
public ItemTouchCallback(ItemChangeAdapter adapter){
mAdapter = adapter;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//可向上下拖动
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
//可向左滑动
int swipeFlags = ItemTouchHelper.LEFT;
return makeMovementFlags(dragFlags,swipeFlags);
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//移动时:
mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//滑动时
mAdapter.onItemDelete(viewHolder.getAdapterPosition());
}
}
3.适配器实现接口
/**
* 作者:张风捷特烈<br/>
* 时间:2018/9/17 0017:14:19<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:RecyclerView适配器
*/
public class MyAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements ItemChangeAdapter {
private List<T> mData;
private Context mContext;
public MyAdapter(List<T> data, Context context) {
mData = data;
mContext = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_rv, null);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
View view = holder.itemView;
view.setOnClickListener((i) ->{
onItemAdd(position, ZRandom.randomCnName());
});
TextView tv = view.findViewById(R.id.tv_title);
tv.setText((String) mData.get(position));
}
@Override
public int getItemCount() {
return mData.size();
}
@Override
public void onItemMove(int from, int to) {
//交换位置
ToastUtil.showAtOnce(mContext,"已交换:"+mData.get(from)+"和"+mData.get(to)+"的位置");
Collections.swap(mData, from, to);
notifyItemMoved(from, to);
}
@Override
public void onItemDelete(int position) {
//移除数据
ToastUtil.showAtOnce(mContext,"已删除:"+mData.get(position));
mData.remove(position);
notifyItemRemoved(position);
}
@Override
public void onItemAdd(int position, Object o) {
mData.add(position, (T) o);
notifyItemInserted(position);//刷新数据
}
class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
}
}
}
4.使用:
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
MyAdapter adapter = new MyAdapter(mData, this);
//实例化ItemTouchHelper
ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchCallback(adapter));
//调用ItemTouchHelper的attachToRecyclerView方法建立联系
touchHelper.attachToRecyclerView(mRecyclerView);
//设置适配器
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(layoutManager);
二、布局文件
1.Activity布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg11">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
2.item布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/MyBaseCard"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:padding="10dp">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/head"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="3dp"
android:layout_toRightOf="@+id/iv_icon"
android:text="Content"
android:textAllCaps="false"
android:textColor="#000000"
android:textSize="@dimen/dp_16"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
后记、
1.声明:
[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力
2.连接传送门:
更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
3.联系我
QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328