cms网站地图模板,免费企业黄页网站入口,免费seo排名工具,网站域名实名认证怎么做最近项目中需要实现对某一类条目进行拖动排序功能#xff0c;实现技术方案就是利用ItemTouchHelper绑定RecyclerView、ItemTouchHelper.Callback来实现UI更新#xff0c;并且实现动态控制是否开启拖动功能。其中#xff0c;ItemTouchHelper是Google在androidx包中添加的实现技术方案就是利用ItemTouchHelper绑定RecyclerView、ItemTouchHelper.Callback来实现UI更新并且实现动态控制是否开启拖动功能。其中ItemTouchHelper是Google在androidx包中添加的其于RecyclerView配合可以比较容易地实现这个功能。
1、布局文件
a、activity的布局文件
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:orientationverticalandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:backgroundcolor/cf5f5f5include layoutlayout/title_layout/Viewandroid:layout_widthmatch_parentandroid:layout_height1dpandroid:backgroundcolor/ce5e5e5/RelativeLayoutandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:backgroundcolor/whiteTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/custom_orderandroid:textColorcolor/c333333android:textSize14spandroid:layout_centerVerticaltrueandroid:layout_marginLeft12dp/ToggleButtonandroid:idid/toggleBtnandroid:layout_width48dpandroid:layout_height28dpandroid:layout_centerVerticaltrueandroid:layout_marginRight12dpandroid:backgrounddrawable/toggle_drawable_selectorandroid:buttonnullandroid:textOffandroid:textOnandroid:layout_alignParentRighttrue//RelativeLayoutRelativeLayoutandroid:idid/tipLayoutandroid:layout_widthmatch_parentandroid:layout_height50dpandroid:layout_marginTop20dpTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/custom_order_tip1android:textColorcolor/c808080android:textSize12spandroid:layout_centerVerticaltrueandroid:layout_marginLeft12dp/TextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/custom_order_tip2android:textColorcolor/c808080android:textSize12spandroid:layout_centerVerticaltrueandroid:layout_marginRight12dpandroid:layout_alignParentRighttrue//RelativeLayoutandroidx.recyclerview.widget.RecyclerViewandroid:idid/recycleViewandroid:layout_widthmatch_parentandroid:layout_heightwrap_content/
/LinearLayoutb、RecycleView的listitem布局文件
?xml version1.0 encodingutf-8?
RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_height50dpImageViewandroid:idid/imageandroid:layout_width24dpandroid:layout_height24dpandroid:layout_alignParentLefttrueandroid:layout_centerVerticaltrueandroid:layout_marginLeft12dpandroid:srcmipmap/icon_bank_others/TextViewandroid:idid/cardTypeTxtandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textxx借记卡android:textColorcolor/c333333android:textSize14spandroid:layout_centerVerticaltrueandroid:layout_marginLeft12dpandroid:layout_toRightOfid/image/TextViewandroid:idid/cardNoTxtandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text(8800)android:textColorcolor/c808080android:textSize14spandroid:layout_centerVerticaltrueandroid:layout_marginLeft6dpandroid:layout_toRightOfid/cardTypeTxt/ImageViewandroid:idid/selectImageandroid:layout_width50dpandroid:layout_height50dpandroid:scaleTypecenterandroid:layout_alignParentRighttrueandroid:layout_centerVerticaltrueandroid:srcmipmap/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.AdapterViewHolder {private ListAccountInfo mDataList;public MyAdapter (ListAccountInfo 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 ListAccountInfo 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 ListAccountInfo 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;}
}