Android使用ItemTouchHelper实现侧滑删除和拖拽
本文实例为大家分享了如何使用ItemTouchHelper实现侧滑删除和拖拽的具体代码,供大家参考,具体内容如下
1.定义一个简单bean类:
publicclassImgText{ publicintresId; publicStringdes; }
2.实现一个RecyclerView.Adapter
publicclassSwipeRecyclerAdapterextendsRecyclerView.Adapter{ privateList mList; publicSwipeRecyclerAdapter(List list){ mList=list; } @Override publicMyHolderonCreateViewHolder(ViewGroupparent,intviewType){ Viewview=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_swipe_recycler,parent,false); returnnewMyHolder(view); } @Override publicvoidonBindViewHolder(MyHolderholder,intposition){ ImgTextimgText=mList.get(position); holder.img.setImageResource(imgText.resId); holder.des.setText(imgText.des); } @Override publicintgetItemCount(){ returnmList.size(); } publicclassMyHolderextendsRecyclerView.ViewHolder{ privateImageViewimg; privateTextViewdes; publicMyHolder(ViewitemView){ super(itemView); img=(ImageView)itemView.findViewById(R.id.img); des=(TextView)itemView.findViewById(R.id.text); } } }
3.绘制分割线:
publicclassDividerItemDecorationextendsRecyclerView.ItemDecoration{ privatestaticfinalint[]ATTRS=newint[]{android.R.attr.listDivider}; publicstaticfinalintHORIZONTAL_LIST=LinearLayoutManager.HORIZONTAL; publicstaticfinalintVERTICAL_LIST=LinearLayoutManager.VERTICAL; privateDrawablemDivider; privateintmOrientation; publicDividerItemDecoration(Contextcontext,intorientation){ finalTypedArraya=context.obtainStyledAttributes(ATTRS); mDivider=a.getDrawable(0); a.recycle(); setOrientation(orientation); } publicvoidsetOrientation(intorientation){ if(orientation!=HORIZONTAL_LIST&&orientation!=VERTICAL_LIST){ thrownewIllegalArgumentException("invalidorientation"); } mOrientation=orientation; } @Override publicvoidonDraw(Canvasc,RecyclerViewparent){ if(mOrientation==VERTICAL_LIST){ drawVertical(c,parent); }else{ drawHorizontal(c,parent); } } publicvoiddrawVertical(Canvasc,RecyclerViewparent){ finalintleft=parent.getPaddingLeft(); finalintright=parent.getWidth()-parent.getPaddingRight(); finalintchildCount=parent.getChildCount(); for(inti=0;i4.实现ItemTouchHelper.Callback接口:
publicclassSwipeCallbackextendsItemTouchHelper.Callback{ privateRecyclerView.AdaptermAdapter; privateList mList; publicSwipeCallback(RecyclerView.Adapteradapter,List list){ mAdapter=adapter; mList=list; } @Override publicbooleanisLongPressDragEnabled(){ returntrue; } @Override publicbooleanisItemViewSwipeEnabled(){ returntrue; } @Override publicintgetMovementFlags(RecyclerViewrecyclerView,RecyclerView.ViewHolderviewHolder){ intdragFlag=ItemTouchHelper.UP|ItemTouchHelper.DOWN; intswipeFlags=ItemTouchHelper.END|ItemTouchHelper.START; returnmakeMovementFlags(dragFlag,swipeFlags); } //用于移动item的位置 @Override publicbooleanonMove(RecyclerViewrecyclerView,RecyclerView.ViewHolderviewHolder,RecyclerView.ViewHoldertarget){ Collections.swap(mList,viewHolder.getAdapterPosition(),target.getAdapterPosition()); mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition()); returntrue; } //一般用于滑动删除 @Override publicvoidonSwiped(RecyclerView.ViewHolderviewHolder,intdirection){ mList.remove(viewHolder.getAdapterPosition()); mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition()); } } 5.编写侧滑删除和拖拽Activity
publicclassSwipeRecyclerViewActivityextendsAppCompatActivity{ privateRecyclerViewmRecyclerView; privateListmList; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_swipe_recycler); mRecyclerView=(RecyclerView)findViewById(R.id.recycler); mList=newArrayList<>(); for(inti=0;i<20;i++){ ImgTextit=newImgText(); it.resId=R.mipmap.ic_launcher; it.des=i+"侧滑删除文字"; mList.add(it); } finalSwipeRecyclerAdapteradapter=newSwipeRecyclerAdapter(mList); RecyclerView.LayoutManagermanager=newLinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); mRecyclerView.setLayoutManager(manager); mRecyclerView.setItemAnimator(newDefaultItemAnimator()); mRecyclerView.addItemDecoration(newDividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST)); mRecyclerView.setAdapter(adapter); ItemTouchHelperswipeItemHelper=newItemTouchHelper(newSwipeCallback<>(adapter,mList)); swipeItemHelper.attachToRecyclerView(mRecyclerView); } } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。