欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-07-18 08:26:55|栏目:Android代码|点击:

本文实例为大家分享了如何使用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控件实现水滴效果

栏    目:Android代码

下一篇:Android中EditText的drawableRight属性设置点击事件

本文标题:Android使用ItemTouchHelper实现侧滑删除和拖拽

本文地址:http://www.codeinn.net/misctech/158870.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有