android中RecyclerView+CardView实现左滑删除和交换功能

语言无法具体表达做出来的效果,这里请读者先看一下演示

左滑删除功能演示

第一步,布局文件代码




    

        


        
            
            
            
            
            
            
            

第二步,自定义一个Listener接口

public interface ItemTouchHelperListener {
    //移动item
    public void onItemMove(int fromPosition,int toPosition);
    //删除item
    public void onItemDelete(int position);
}

第三步,在自定义adapter类中实现该接口

public class AllBookAdapter extends RecyclerView.Adapter implements ItemTouchHelperListener {

    private Context context;
    private List books;


    public AllBookAdapter(Context context, List books) {
        this.context = context;
        this.books = books;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_all_book, parent, false);
        return new MyViewHolder(view);
    }
//重写ItemTouchHelperListener接口中的方法
    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        //交换位置
        Collections.swap(books,fromPosition,toPosition);
        notifyItemMoved(fromPosition,toPosition);
    }
//重写ItemTouchHelperListener接口中的方法
    @Override
    public void onItemDelete(final int position) {
        AlertDialog alertDialog = new AlertDialog.Builder(context)
                .setTitle("确认删除")
                .setMessage("是否确认删除")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        DbUtil.deleteBookByNumber(books.get(position).getNumber(),context);
                        DbUtil.deleteBookStar(books.get(position).getNumber(),context);
                        DbUtil.deleteBookRead(books.get(position).getNumber(),context);
                        books.remove(position);
                        notifyItemRemoved(position);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        return;
                    }
                }).create();
        alertDialog.show();
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final BookBean bookBean = books.get(position);
        if(holder instanceof MyViewHolder){
            ((MyViewHolder) holder).tvName.setText("书籍名:"+bookBean.getName());
            ((MyViewHolder) holder).tvSex.setText("价格:"+bookBean.getPrice());
            ((MyViewHolder) holder).tvTel.setText("数量:"+bookBean.getCount());
            ((MyViewHolder) holder).tvMajor.setText("类型:"+bookBean.getType());
            ((MyViewHolder) holder).tvClass.setText("浏览量:"+bookBean.getLook_number());
            ((MyViewHolder) holder).tvNumbver.setText("编号:"+bookBean.getNumber());
            if(bookBean.getCover() != null)
            {
                ContentResolver cr = context.getContentResolver();
                Uri uri = Uri.parse(bookBean.getCover());
                try {
                    Bitmap bitmap = null;
                    bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
                    // 取得想要缩放的matrix参数
                    Matrix matrix = new Matrix();
                    matrix.postScale(0.25f, 0.25f);
                    Bitmap bitmap1 = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
                    ((MyViewHolder) holder).imageView.setImageBitmap(bitmap1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            ((MyViewHolder) holder).btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, UpdateBookActivity.class);
                    intent.putExtra("book",bookBean);
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        System.out.println("0------------size"+books.size());
        return books.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tvName;
        TextView tvSex;
        TextView tvMajor;
        TextView tvTel;
        TextView tvClass;
        TextView tvNumbver;
        ImageView imageView;
        Button btn;


        public MyViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvSex = itemView.findViewById(R.id.tv_sex);
            tvClass = itemView.findViewById(R.id.tv_class);
            tvMajor = itemView.findViewById(R.id.tv_major);
            tvTel = itemView.findViewById(R.id.tv_tel);
            tvNumbver = itemView.findViewById(R.id.tv_number);
            btn = itemView.findViewById(R.id.btn_bianji);
            imageView = itemView.findViewById(R.id.item_img);
        }
    }
}

第四步,创建一个 myItemTouchHelperCallBack类,该类继承自ItemTouchHelper.Callback

 public class myItemTouchHelperCallBack extends ItemTouchHelper.Callback{
        private ItemTouchHelperListener itemTouchHelperListener;

        public myItemTouchHelperCallBack(ItemTouchHelperListener itemTouchHelperListener) {
            this.itemTouchHelperListener = itemTouchHelperListener;
        }

        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            //允许上下拖动
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            //允许从右向左滑动
            int swipeFlags = ItemTouchHelper.LEFT;
            return makeMovementFlags(dragFlags,swipeFlags);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            //onItemMove接口里的方法
            itemTouchHelperListener.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
            return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            //onItemDelete接口里的方法
            itemTouchHelperListener.onItemDelete(viewHolder.getAdapterPosition());
        }

        @Override
        public boolean isLongPressDragEnabled() {
            //该方法返回值为true时,表示支持长按ItemView拖动
            return true;
        }

        @Override
        public boolean isItemViewSwipeEnabled() {
            //该方法返回true时,表示如果用户触摸并且左滑了view,那么可以执行滑动删除操作,就是可以调用onSwiped()方法
            return true;
        }
    }

最后一步,在activity的onCreate里添加如下代码

@BindView(R.id.rv_list)
 RecyclerView mRecyclerView;
AllBookAdapter adapter;
private LinearLayoutManager layoutManager;

mRecyclerView.setLayoutManager(layoutManager);
 mRecyclerView.setAdapter(adapter);
 ItemTouchHelper.Callback callback = new myItemTouchHelperCallBack(adapter);
 ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
 touchHelper.attachToRecyclerView(mRecyclerView);

activity的布局文件代码



    
    
            
    

你可能感兴趣的:(android语言)