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

这篇文章主要为大家详细介绍了Android使用ItemTouchHelper实现侧滑删除和拖拽,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

1. 定义一个简单bean类:


public class ImgText {
  public int resId;
  public String des;
}

2. 实现一个RecyclerView.Adapter


public class SwipeRecyclerAdapter extends RecyclerView.Adapter<SwipeRecyclerAdapter.MyHolder> {

  private List<ImgText> mList;

  public SwipeRecyclerAdapter(List<ImgText> list) {
    mList = list;
  }

  @Override
  public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_swipe_recycler, parent, false);
    return new MyHolder(view);
  }

  @Override
  public void onBindViewHolder(MyHolder holder, int position) {
    ImgText imgText = mList.get(position);
    holder.img.setImageResource(imgText.resId);
    holder.des.setText(imgText.des);
  }

  @Override
  public int getItemCount() {
    return mList.size();
  }

  public class MyHolder extends RecyclerView.ViewHolder {

    private ImageView img;
    private TextView des;

    public MyHolder(View itemView) {
      super(itemView);
      img = (ImageView) itemView.findViewById(R.id.img);
      des = (TextView) itemView.findViewById(R.id.text);
    }
  }
}

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 context, int orientation) {
    final TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);
    a.recycle();
    setOrientation(orientation);
  }
  public void setOrientation(int orientation) {
    if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
      throw new IllegalArgumentException("invalid orientation");
    }
    mOrientation = orientation;
  }
  @Override
  public void onDraw(Canvas c, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
      drawVertical(c, parent);
    } else {
      drawHorizontal(c, parent);
    }
  }
  public void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
      final View child = parent.getChildAt(i);
      RecyclerView v = new RecyclerView(
          parent.getContext());
      final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
          .getLayoutParams();
      final int top = child.getBottom() + params.bottomMargin;
      final int bottom = top + mDivider.getIntrinsicHeight();
      mDivider.setBounds(left, top, right, bottom);
      mDivider.draw(c);
    }
  }
  public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
      final View child = parent.getChildAt(i);
      final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
          .getLayoutParams();
      final int left = child.getRight() + params.rightMargin;
      final int right = left + mDivider.getIntrinsicHeight();
      mDivider.setBounds(left, top, right, bottom);
      mDivider.draw(c);
    }
  }
  @Override
  public void getItemOffsets(Rect outRect, int itemPosition,
                RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
      outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
      outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
  }
}

4. 实现ItemTouchHelper.Callback接口:


public class SwipeCallback<T> extends ItemTouchHelper.Callback {

  private RecyclerView.Adapter<? extends RecyclerView.ViewHolder> mAdapter;
  private List<T> mList;

  public SwipeCallback(RecyclerView.Adapter adapter, List<T> list) {
    mAdapter = adapter;
    mList = list;
  }

  @Override
  public boolean isLongPressDragEnabled() {
    return true;
  }

  @Override
  public boolean isItemViewSwipeEnabled() {
    return true;
  }


  @Override
  public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    int dragFlag=ItemTouchHelper.UP|ItemTouchHelper.DOWN;
    int swipeFlags=ItemTouchHelper.END|ItemTouchHelper.START;

    return makeMovementFlags(dragFlag,swipeFlags);
  }

  //用于移动item的位置
  @Override
  public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    Collections.swap(mList, viewHolder.getAdapterPosition(), target.getAdapterPosition());
    mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());

    return true;
  }

  //一般用于滑动删除
  @Override
  public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    mList.remove(viewHolder.getAdapterPosition());
    mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
  }
}

5.编写侧滑删除和拖拽Activity


public class SwipeRecyclerViewActivity extends AppCompatActivity {
  private RecyclerView mRecyclerView;

  private List<ImgText> mList;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_swipe_recycler);
    mRecyclerView= (RecyclerView) findViewById(R.id.recycler);
    mList=new ArrayList<>();

    for (int i = 0; i < 20; i++) {
      ImgText it= new ImgText();
      it.resId=R.mipmap.ic_launcher;
      it.des=i+"侧滑删除文字";
      mList.add(it);
    }

    final SwipeRecyclerAdapter adapter= new SwipeRecyclerAdapter(mList);
    RecyclerView.LayoutManager manager= new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    mRecyclerView.setLayoutManager(manager);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    mRecyclerView.setAdapter(adapter);
    ItemTouchHelper swipeItemHelper= new ItemTouchHelper(new SwipeCallback<>(adapter,mList));
    swipeItemHelper.attachToRecyclerView(mRecyclerView);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

这篇文章主要为大家详细介绍了Android LinearLayout实现自动换行,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了Android如何实现给EditText添加下划线,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了Android使用BottomNavigationBar实现导航栏功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
adb全名android debug bridge 安卓调试桥,下面这篇文章主要给大家介绍了关于Android中ADB命令用法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
很多朋友都遇到过打开别人的项目一直处于Building‘XXX’Gradle project info的情况。下面小编给大家带来了Android Studio一直处于Building的解决方法,感兴趣的朋友一起看看吧
android 中activity的启动模式分为四种,(standard、singleTop、singTask、singleInstance),本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友参考下吧