Android RecyclerView左滑侧滑显示删除按钮

创建一个Recyclerview列表item布局,自定义容器:
SlidButtonView.java

public class SlidButtonView extends HorizontalScrollView {

    private static final String TAG = "SlidButtonView";
    private TextView lTextView_Delete;//删除按钮
    private int lScrollWith;//横向滚动范围
    private boolean first = false; //标记第一次进入获取删除按钮控件

    public SlidButtonView(Context context) {
        this(context, null);
    }

    public SlidButtonView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlidButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setOverScrollMode(OVER_SCROLL_NEVER);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //第一次进入时获取删除控件
        if (!first) {
            lTextView_Delete = findViewById(R.id.tv_delete);
            first = true;//修改标记
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        //默认隐藏删除按钮
        if (changed) {
            this.scrollTo(0, 0);
            //获取水平滚动条可以滚动的范围,也就是右侧删除按钮的宽度
            lScrollWith = lTextView_Delete.getWidth();
        }
    }

    //手势判断
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                Log.d(TAG, "onTouchEvent: ");
                changeScrollx();
                return true;
        }
        return super.onTouchEvent(ev);
    }

    //根据滑动距离判断是否显示删除按钮
    private void changeScrollx() {
        //触摸滑动的距离大于删除按钮的一半时
        if (getScrollX() >= (lScrollWith / 2)) {
            //显示删除按钮
            this.smoothScrollTo(lScrollWith, 0);
        } else {
            //隐藏删除按钮
            this.smoothScrollTo(0, 0);
        }
    }
}

item布局:

<?xml version="1.0" encoding="utf-8"?>
<com.xiebin.myapplication.view.SlidButtonView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginBottom="1dp"
    android:background="@android:color/white">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--删除按钮-->
        <TextView
            android:id="@+id/tv_delete"
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:layout_toRightOf="@+id/layout_content"
            android:background="@drawable/btn_click_red_bg"
            android:gravity="center"
            android:text="删除"
            android:textColor="@android:color/white"/>

        <!--行的布局文件-->
        <RelativeLayout
            android:id="@+id/layout_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

            <!--图标-->
            <ImageView
                android:id="@+id/img"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/icon_1"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_toRightOf="@+id/img"
                android:gravity="center_vertical"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:text="123"
                    android:textColor="@android:color/black"
                    android:textSize="15sp"/>

                <TextView
                    android:id="@+id/info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="2dp"
                    android:singleLine="true"
                    android:text="123"
                    android:textColor="@android:color/black"
                    android:textSize="10sp"/>

            </LinearLayout>

        </RelativeLayout>

    </RelativeLayout>

</com.xiebin.myapplication.view.SlidButtonView>

Recycler适配器:

Adapter.java

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

    //图标数组
    private int[] icons = {
            R.drawable.icon_1, R.drawable.icon_2, R.drawable.icon_3,
            R.drawable.icon_4, R.drawable.icon_5, R.drawable.icon_6, R.drawable.icon_7,
            R.drawable.icon_8, R.drawable.icon_9};

    //名字数组
    private int[] names = {
            R.string.name1, R.string.name2, R.string.name3, R.string.name4, R.string.name5,
            R.string.name6, R.string.name7, R.string.name8, R.string.name9};

    //信息数组
    private int[] infos = {
            R.string.info1, R.string.info2, R.string.info3, R.string.info4, R.string.info5,
            R.string.info6, R.string.info7, R.string.info8, R.string.info9};

    private Context lContent;//定义上下文

    //集合
    private List<Integer> listIcon = new ArrayList<>();
    private List<Integer> listName = new ArrayList<>();
    private List<Integer> listInfo = new ArrayList<>();

    public Adapter(Context lContent) {
        this.lContent = lContent;
        //设置菜单行数与行内图标、名称、信息
        for (int i = 0; i < 11; i++) {
            listIcon.add(icons[I]);
            listName.add(names[I]);
            listInfo.add(infos[I]);
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取列表中每行的布局文件
        View view = LayoutInflater.from(lContent).inflate(R.layout.layout_item, parent, false);
        return new MyViewHolder(view);
    }

    //设置列表中行所显示的内容
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        //设置图标
        holder.img.setBackgroundResource(listIcon.get(position));
        //设置名称
        holder.name.setText(listName.get(position));
        //设置信息
        holder.info.setText(listInfo.get(position));
        //设置内容宽度为屏幕的宽度
        holder.layout_content.getLayoutParams().width = Utils.getScreenWidth(lContent);

        //删除按钮的方法
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int n = holder.getLayoutPosition();//获取要删除行的位置
                removeData(n);//删除列表中指定的行
            }
        });
    }

    //返回行的总数
    @Override
    public int getItemCount() {
        return listIcon.size();
    }

    //删除列表行中信息的方法
    public void removeData(int position){
        listIcon.remove(position);//删除图标
        listName.remove(position);//删除行中名字
        listInfo.remove(position);//删除信息
        notifyItemRemoved(position);//删除行
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView btn_delete;
        public TextView name, info;//名字与信息
        public ImageView img;//图标
        public ViewGroup layout_content;//图标与信息布局

        //获取控件
        public MyViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            info = itemView.findViewById(R.id.info);
            img = itemView.findViewById(R.id.img);
            layout_content = itemView.findViewById(R.id.layout_content);
            btn_delete = itemView.findViewById(R.id.tv_delete);
        }
    }
}

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RecyclerView侧滑菜单可以通过ItemTouchHelper类来实现。 首先,需要创建一个实现ItemTouchHelper.Callback的类,重写其中的方法,用于处理拖拽和侧滑等操作。在其中,我们需要实现onSwiped方法,用于处理RecyclerView中的侧滑操作。 可以通过实现onChildDraw方法来绘制侧滑菜单,例如使用Canvas绘制背景和图标等。然后在onSwiped方法中,通过ViewHolder.getAdapterPosition()获取当前侧滑的位置,然后再调用Adapter的remove方法来删除对应的数据。最后,需要在Adapter中实现onCreateViewHolder方法,在其中绑定侧滑菜单的布局和操作。 下面是一个简单的示例代码: ```java public class SwipeController extends ItemTouchHelper.Callback { private RecyclerView.Adapter adapter; public SwipeController(RecyclerView.Adapter adapter) { this.adapter = adapter; } @Override public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { return makeMovementFlags(0, ItemTouchHelper.LEFT); } @Override public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { return false; } @Override public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { int position = viewHolder.getAdapterPosition(); adapter.notifyItemRemoved(position); adapter.notifyItemRangeChanged(position, adapter.getItemCount()); } @Override public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { View itemView = viewHolder.itemView; Paint p = new Paint(); if (dX < 0) { p.setColor(Color.RED); RectF background = new RectF((float)itemView.getRight() + dX, (float)itemView.getTop(), (float)itemView.getRight(), (float)itemView.getBottom()); c.drawRect(background, p); Drawable icon = ContextCompat.getDrawable(adapter.getContext(), R.drawable.ic_delete); int iconWidth = icon.getIntrinsicWidth(); int iconHeight = icon.getIntrinsicHeight(); int left = itemView.getRight() - iconWidth - 32; int top = itemView.getTop() + (itemView.getHeight() - iconHeight) / 2; int right = itemView.getRight() - 32; int bottom = top + iconHeight; icon.setBounds(left, top, right, bottom); icon.draw(c); } super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } } ``` 在Activity或Fragment中,可以将SwipeController作为参数传递给ItemTouchHelper,然后调用ItemTouchHelper.attachToRecyclerView方法来绑定RecyclerView。 ```java RecyclerView recyclerView = findViewById(R.id.recycler_view); Adapter adapter = new Adapter(data); recyclerView.setAdapter(adapter); SwipeController swipeController = new SwipeController(adapter); ItemTouchHelper itemTouchHelper = new ItemTouchHelper(swipeController); itemTouchHelper.attachToRecyclerView(recyclerView); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值