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);
        }
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章