十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本文实例为大家分享了如何使用ItemTouchHelper实现侧滑删除和拖拽的具体代码,供大家参考,具体内容如下
创新互联公司-成都网站建设公司,专注成都网站设计、成都网站制作、网站营销推广,域名注册,网页空间,绵阳服务器托管有关企业网站制作方案、改版、费用等问题,请联系创新互联公司。
1. 定义一个简单bean类:
public class ImgText { public int resId; public String des; }
2. 实现一个RecyclerView.Adapter
public class SwipeRecyclerAdapter extends RecyclerView.Adapter{ private List mList; public SwipeRecyclerAdapter(List 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 SwipeCallbackextends ItemTouchHelper.Callback { private RecyclerView.Adapter extends RecyclerView.ViewHolder> mAdapter; private List mList; public SwipeCallback(RecyclerView.Adapter adapter, List 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 ListmList; @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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。