短視訊app製作,實現訊息列表的左滑刪除或長按刪除

zhibo系統開發發表於2022-06-20

短視訊app製作,實現訊息列表的左滑刪除或長按刪除

首先佈局就是一個RecyclerView

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


介面卡什麼的不多說,正常寫就行

mRecyclerView = findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
myApr = new MyApr();
mRecyclerView.setAdapter(myApr);
mDatas = new ArrayList<>();//這裡是資料 自己放我這裡就不放了
//新增長按刪除監聽
myApr.setOnremoveListnner(new MyApr.OnremoveListnner() {
    @Override
    public void ondelect(final int i) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage("確定刪除?");
        builder.setTitle("提示");
        builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mDatas.remove(i);
                //更新列表
                myApr.notifyDataSetChanged();
                Toast.makeText(MainActivity.this, "刪除成功", Toast.LENGTH_LONG).show();
            }
        });
        //新增AlertDialog.Builder物件的setNegativeButton()方法
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.create().show();
    }
});
public static class MyApr extends RecyclerView.Adapter<VH> {
    //建立刪除監聽介面
    interface OnremoveListnner {
        void ondelect(int i);
    }
    private OnremoveListnner onremoveListnner;
    public void setOnremoveListnner(OnremoveListnner onremoveListnner) {
        this.onremoveListnner = onremoveListnner;
    }
    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleview_item, parent, false));
    }
    @Override
    public void onBindViewHolder(final VH holder, final int position) {
    //show=展示的view  click=刪除view 監聽也和recyclerView的不一樣
        holder.show.setText(mDatas.get(position));
        //恢復狀態
        holder.recyclerViewItem.apply();
        holder.click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDatas.remove(position);
                //更新列表
                notifyDataSetChanged();
                Toast.makeText(holder.itemView.getContext(), "刪除成功", Toast.LENGTH_LONG).show();
            }
        });
        //長按監聽
        holder.show.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onremoveListnner != null) {
                    //刪除
                    onremoveListnner.ondelect(position);
                }
                return true;
            }
        });
    }
    @Override
    public int getItemCount() {
        return null == mDatas ? 0 : mDatas.size();
    }
}


左劃刪除我這裡跟RecyclerView是沒有關係的,佈局最關鍵,跟item有關,父容器需要自定義HorizontalScrollView,自己建立一個類就行,屬性可以自己修改

public class MyRecyclerViewItem extends HorizontalScrollView {
    public MyRecyclerViewItem(Context context) {
        super(context);
        init(context,null);
    }
    public MyRecyclerViewItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }
    public MyRecyclerViewItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
    public static final String TAG=MyRecyclerViewItem.class.getSimpleName();
    private boolean isLeft = true;//預設左邊
    private int rightLayoutWidth;
    private int leftLayoutWidth;
    private int range;
    public void setRightLayoutWidth(int rightLayoutWidth) {
        this.rightLayoutWidth = rightLayoutWidth;
    }
    public void setLeftLayoutWidth(int leftLayoutWidth) {
        this.leftLayoutWidth = leftLayoutWidth;
    }
    public void setRange(int range) {
        this.range = range;
    }
    private void init(Context context, AttributeSet attrs) {
        leftLayoutWidth = getScreenSize(getContext()).widthPixels;// recyclerview 寬度
        rightLayoutWidth = dp2px(getContext(),200);// 右邊佈局的寬度
        range = dp2px(getContext(), 30);// 移動多少開始切換閾值
        if (attrs!=null){
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRecyclerViewItem);
            int indexCount = typedArray.getIndexCount();
            for (int i = 0; i < indexCount; i++) {
                int index = typedArray.getIndex(i);
                if (index==R.styleable.MyRecyclerViewItem_left_width){
                    leftLayoutWidth = typedArray.getInteger(index, 0)==0? leftLayoutWidth : dp2px(context, typedArray.getInteger(index, 0));
                }
                if (index==R.styleable.MyRecyclerViewItem_right_width){
                    rightLayoutWidth = typedArray.getInteger(index, 0)==0? rightLayoutWidth : dp2px(context, typedArray.getInteger(index, 0));
                }
                if (index==R.styleable.MyRecyclerViewItem_move_range){
                    range = typedArray.getInteger(index, 0)==0? range : dp2px(context, typedArray.getInteger(index, 0));
                }
            }
            typedArray.recycle();
        }
    }
    //介面卡 bind 方法中呼叫
    public void apply() {
        isLeft = true;
        changeLayout();
        scrollTo(0, 0);
    }
    private void changeLayout() {
        try {
            ViewGroup mainLayout= (ViewGroup) getChildAt(0);
            ViewGroup left= (ViewGroup) mainLayout.getChildAt(0);
            ViewGroup right= (ViewGroup) mainLayout.getChildAt(1);
            if (left.getMeasuredWidth()== leftLayoutWidth && right.getMeasuredWidth()==rightLayoutWidth){
                Log.i(TAG, "changeLayout: no change");
                return;
            }
            ViewGroup.LayoutParams layoutParams = left.getLayoutParams();
            layoutParams.width = leftLayoutWidth;
            left.setLayoutParams(layoutParams);
            ViewGroup.LayoutParams layoutParams2 = right.getLayoutParams();
            layoutParams2.width = rightLayoutWidth;
            left.setLayoutParams(layoutParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static DisplayMetrics getScreenSize(Context context){
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        return dm;
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            Log.i(getClass().getSimpleName(), "down");
            return true;
        }
        if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
            Log.i(getClass().getSimpleName(), "up");
            if (isLeft) {
                if (getScrollX() > range) {
                    isLeft = false;
                    smoothScrollTo(rightLayoutWidth, 0);
                } else {
                    smoothScrollTo(0, 0);
                }
            } else {
                if (getScrollX() < (rightLayoutWidth - range)) {
                    isLeft = true;
                    smoothScrollTo(0, 0);
                } else {
                    smoothScrollTo(rightLayoutWidth, 0);
                }
            }
            return true;
        }
        Log.i(getClass().getSimpleName(), "end");
        return super.onTouchEvent(ev);
    }
    public static int dp2px(Context context,float dpValue) {
        DisplayMetrics scale = context.getResources().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, scale);
    }
}


以上就是 短視訊app製作,實現訊息列表的左滑刪除或長按刪除,更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2901632/,如需轉載,請註明出處,否則將追究法律責任。

相關文章