RecycleView结合ItemTouchHelper实现拖动排序

2024-02-23 01:04

本文主要是介绍RecycleView结合ItemTouchHelper实现拖动排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近项目中需要实现对某一类条目进行拖动排序功能,实现技术方案就是利用ItemTouchHelper绑定RecyclerView、ItemTouchHelper.Callback来实现UI更新,并且实现动态控制是否开启拖动功能。其中,ItemTouchHelper是Google在androidx包中添加的,其于RecyclerView配合可以比较容易地实现这个功能。

1、布局文件

a、activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/cf5f5f5"><include layout="@layout/title_layout"/><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@color/ce5e5e5"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:background="@color/white"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/custom_order"android:textColor="@color/c333333"android:textSize="14sp"android:layout_centerVertical="true"android:layout_marginLeft="12dp"/><ToggleButtonandroid:id="@+id/toggleBtn"android:layout_width="48dp"android:layout_height="28dp"android:layout_centerVertical="true"android:layout_marginRight="12dp"android:background="@drawable/toggle_drawable_selector"android:button="@null"android:textOff=""android:textOn=""android:layout_alignParentRight="true"/></RelativeLayout><RelativeLayoutandroid:id="@+id/tipLayout"android:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="20dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/custom_order_tip1"android:textColor="@color/c808080"android:textSize="12sp"android:layout_centerVertical="true"android:layout_marginLeft="12dp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/custom_order_tip2"android:textColor="@color/c808080"android:textSize="12sp"android:layout_centerVertical="true"android:layout_marginRight="12dp"android:layout_alignParentRight="true"/></RelativeLayout><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recycleView"android:layout_width="match_parent"android:layout_height="wrap_content"/>
</LinearLayout>

bRecycleViewlistitem布局文件

<?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="50dp"><ImageViewandroid:id="@+id/image"android:layout_width="24dp"android:layout_height="24dp"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:layout_marginLeft="12dp"android:src="@mipmap/icon_bank_others"/><TextViewandroid:id="@+id/cardTypeTxt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="xx借记卡"android:textColor="@color/c333333"android:textSize="14sp"android:layout_centerVertical="true"android:layout_marginLeft="12dp"android:layout_toRightOf="@id/image"/><TextViewandroid:id="@+id/cardNoTxt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="(8800)"android:textColor="@color/c808080"android:textSize="14sp"android:layout_centerVertical="true"android:layout_marginLeft="6dp"android:layout_toRightOf="@id/cardTypeTxt"/><ImageViewandroid:id="@+id/selectImage"android:layout_width="50dp"android:layout_height="50dp"android:scaleType="center"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:src="@mipmap/sort_line"/>
</RelativeLayout>

2、实现ItemTouchHelper.Callback

private ItemTouchHelper.Callback callback = 
new ItemTouchHelper.Callback() {public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {//首先回调的方法,返回int表示是否监听该方向int dragFlag = ItemTouchHelper.DOWN | ItemTouchHelper.UP;//拖拽int swipeFlag = 0;//侧滑删除return makeMovementFlags(dragFlag, swipeFlag);}@Overridepublic void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {}public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {if (mAdapter != null) {mAdapter.onMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());}return true;}public void onSelectedChanged(ViewHolder viewHolder, int actionState) {if (actionState != 0) {viewHolder.itemView.setAlpha(0.9f);}super.onSelectedChanged(viewHolder, actionState);}public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {super.clearView(recyclerView, viewHolder);viewHolder.itemView.setAlpha(1.0f);if (mAdapter != null) {mAdapter.notifyDataSetChanged();mSortedList = mAdapter.getSortedDataList();LogUtils.debug(TAG, "callback clearView: " + JSONObject.toJSONString(mSortedList));refreshCardSort(mSortedList);}}};

3、初始化RecycleView,并绑定ItemTouchHelper

 mAdapter = new MyAdapter(mSortedList);recyclerView.setAdapter(mAdapter);LinearLayoutManager llm = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);recyclerView.setLayoutManager(llm);//这两句是关键,完成RecycleView和ItemTouchHelper的绑定ItemTouchHelper helper = new ItemTouchHelper(callback);helper.attachToRecyclerView(recyclerView);

4、定义自己的Adapter

private class MyAdapter extends RecyclerView.Adapter<ViewHolder> {private List<AccountInfo> mDataList;public MyAdapter (List<AccountInfo> dataList) {this.mDataList = dataList;}public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.card_order_listitem, parent, false));}public void onBindViewHolder(ViewHolder holder, final int position) {final AccountInfo item = mDataList.get(position);if (item != null) {holder.tvContentType.setText(getCardNoType(item.getAccountType()));holder.tvContentNo.setText(getCardNo(item.getAccountNo()));}}public int getItemCount() {return mDataList.size();}public void onMove(int fromPosition, int toPosition) {//对原数据进行移动Collections.swap(mDataList, fromPosition, toPosition);//通知数据移动notifyItemMoved(fromPosition, toPosition);LogUtils.debug(TAG, "MyAdapter onMove fromPosition: " + fromPosition + ", toPosition: " + toPosition);}public List<AccountInfo> getSortedDataList() {return this.mDataList;}}public class ViewHolder extends RecyclerView.ViewHolder {public TextView tvContentType;public TextView tvContentNo;public ViewHolder(View view) {super(view);this.tvContentType = (TextView)view.findViewById(R.id.cardTypeTxt);this.tvContentNo = (TextView)view.findViewById(R.id.cardNoTxt);}}其他参数定义:private List<AccountInfo> mSortedList;private MyAdapter mAdapter;bean定义如下:
package com.qdone.qrcode.pay.qrcodesdkdemo.model;/*** Time: 2024/1/26* Author:* Description:*/
public class AccountInfo {private String accountId;private String custId;private String accountNo;private String accountType;private String bankName;private String displayOrder;/*** 是否是默认账户,0-普通账户 1-默认账户*/private String defaultFlag;@Overridepublic String toString() {return "AccountInfo{" +"accountId='" + accountId + '\'' +", custId='" + custId + '\'' +", accountNo='" + accountNo + '\'' +", accountType='" + accountType + '\'' +", bankName='" + bankName + '\'' +", displayOrder='" + displayOrder + '\'' +", defaultFlag='" + defaultFlag + '\'' +'}';}public String getAccountId() {return accountId;}public void setAccountId(String accountId) {this.accountId = accountId;}public String getCustId() {return custId;}public void setCustId(String custId) {this.custId = custId;}public String getAccountNo() {return accountNo;}public void setAccountNo(String accountNo) {this.accountNo = accountNo;}public String getAccountType() {return accountType;}public void setAccountType(String accountType) {this.accountType = accountType;}public String getBankName() {return bankName;}public void setBankName(String bankName) {this.bankName = bankName;}public String getDisplayOrder() {return displayOrder;}public void setDisplayOrder(String displayOrder) {this.displayOrder = displayOrder;}public String getDefaultFlag() {return defaultFlag;}public void setDefaultFlag(String defaultFlag) {this.defaultFlag = defaultFlag;}
}

这篇关于RecycleView结合ItemTouchHelper实现拖动排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/737057

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略 1. 特权模式限制2. 宿主机资源隔离3. 用户和组管理4. 权限提升控制5. SELinux配置 💖The Begin💖点点关注,收藏不迷路💖 Kubernetes的PodSecurityPolicy(PSP)是一个关键的安全特性,它在Pod创建之前实施安全策略,确保P

hdu 1285(拓扑排序)

题意: 给各个队间的胜负关系,让排名次,名词相同按从小到大排。 解析: 拓扑排序是应用于有向无回路图(Direct Acyclic Graph,简称DAG)上的一种排序方式,对一个有向无回路图进行拓扑排序后,所有的顶点形成一个序列,对所有边(u,v),满足u 在v 的前面。该序列说明了顶点表示的事件或状态发生的整体顺序。比较经典的是在工程活动上,某些工程完成后,另一些工程才能继续,此时