Android使用ItemTouchHelper实现侧滑删除和拖拽

  

本文实例为大家分享了如何使用ItemTouchHelper实现侧滑删除和拖拽的具体代码,供大家参考,具体内容如下

1。定义一个简单bean类:

public  class  ImgText  {   public 才能;int 渣油;   public 才能;String  des;   }

2。实现一个RecyclerView。适配器

public  class  SwipeRecyclerAdapter  extends  RecyclerView.Adapter, {      private 才能;List< ImgText>, mList;      public 才能;SwipeRecyclerAdapter (List

3。绘制分割线:

public  class  DividerItemDecoration  extends  RecyclerView.ItemDecoration  {   private 才能static  final  int [], ATTRS =, new  int [] {android.R.attr.listDivider};   public 才能static  final  int  HORIZONTAL_LIST =, LinearLayoutManager.HORIZONTAL;   public 才能static  final  int  VERTICAL_LIST =, LinearLayoutManager.VERTICAL;   private 才能;Drawable  mDivider;   private 才能;int  mOrientation;   public 才能;DividerItemDecoration (Context 上下文,int 取向),{   ,,,final  TypedArray  a =, context.obtainStyledAttributes (ATTRS);   ,,,mDivider =, a.getDrawable (0);   ,,,a.recycle ();   ,,,setOrientation(方向);   ,,}   public 才能;void  setOrientation (int 取向),{   ,,,if  (orientation  !=, HORIZONTAL_LIST ,,, orientation  !=, VERTICAL_LIST), {   ,,,,,throw  new  IllegalArgumentException (“invalid  orientation");   ,,,}   ,,,mOrientation =,取向;   ,,}   @Override才能   public 才能;void  onDraw (Canvas  c, RecyclerView 父母),{   ,,,if  (mOrientation ==, VERTICAL_LIST), {   ,,,,,drawVertical (c,父);   ,,,},{else    ,,,,,drawHorizontal (c,父);   ,,,}   ,,}   public 才能;void  drawVertical (Canvas  c, RecyclerView 父母),{   ,,,final  int  left =, parent.getPaddingLeft ();   ,,,final  int  right =, parent.getWidth(),安康;parent.getPaddingRight ();   ,,,final  int  childCount =, parent.getChildCount ();   ,,,for  (int 小姐:=,0;,小姐:& lt;, childCount;,我+ +),{   ,,,,,final  View  child =, parent.getChildAt(我);   ,,,,,RecyclerView  v =, new  RecyclerView (   ,,,,,,,,,parent.getContext ());   ,,,,,final  RecyclerView.LayoutParams  params =, (RecyclerView.LayoutParams),孩子   ,,,,,,,,,.getLayoutParams ();   ,,,,,final  int  top =, child.getBottom (), +, params.bottomMargin;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android使用ItemTouchHelper实现侧滑删除和拖拽