Item长按事件实现

MyAdapter中定义接口回调

public interface OnremoveListenser{
        void onDelete(int i);
    }
    private OnremoveListenser onremoveListenser;
    public void setOnremoveListenser(OnremoveListenser onremoveListenser){
        this.onremoveListenser=onremoveListenser;
    }

onBindViewHolder中

@Override
    public void onBindViewHolder(@NonNull final MyAdapter.ViewHolder holder, final int position) {
        holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onremoveListenser!=null){
                    onremoveListenser.onDelete(position);
                }
                return true;
            }
        });
    }

在HomeFragment中调用

private MyAdapter mMyAdapter_in;
    //实例化
mMyAdapter_in = new MyAdapter(in_total_list);
mMyAdapter_in.setOnremoveListenser(new MyAdapter.OnremoveListenser() {
            @Override
            public void onDelete(int i) {
                showBottomDialog(i,"in");
            }
        });

底部dialog的实现

bottom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="200dp"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:background="@drawable/button_round"
        android:textColor="#FF0000"
        android:text="删除" />

    <Button
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:id="@+id/btn_change"
        android:layout_width="200dp"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:background="@drawable/button_round"
        android:textColor="#FF0000"
        android:text="修改" />
</LinearLayout>

HomeFragment.java中

private void showBottomDialog(final int i, final String s){
        final BottomSheetDialog bottomSheetDialog=new BottomSheetDialog(Objects.requireNonNull(getContext()));
        final View dialog_view=LayoutInflater.from(getContext()).inflate(R.layout.bottom_dialog,null);
        Button delete_bt=dialog_view.findViewById(R.id.btn_delete);
        delete_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (s){
                    case "in":{
                        bottomSheetDialog.dismiss();
                        date_in_list.remove(i);
                        in_tag_list.remove(i);
                        in_money_list.remove(i);
                        in_total_list.remove(i);
                        Save_List(date_in_list,"date_in");
                        Save_List(in_money_list,"in_money");
                        Save_List(in_tag_list,"in_tag");
                        Save_List(in_total_list,"in_total");
                        mMyAdapter_in.notifyDataSetChanged();
                    }break;
                    case "out":{
                        bottomSheetDialog.dismiss();
                        date_out_list.remove(i);
                        out_tag_list.remove(i);
                        out_money_list.remove(i);
                        out_total_list.remove(i);
                        Save_List(date_out_list,"date_out");
                        Save_List(out_money_list,"out_money");
                        Save_List(out_tag_list,"out_tag");
                        Save_List(out_total_list,"out_total");
                        myAdapter_out.notifyDataSetChanged();
                    }
                }

            }
        });
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.setContentView(dialog_view);
        bottomSheetDialog.show();
    }

效果:

对话框的简单定制

由上图可以看到底部对话框是方形的,而且UI比较单一,所以我将它改为圆角,并且让它和四周有一些间隔,并添加分割线(直接用imageView)

bottom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dialog_round"
    android:orientation="vertical">

    <Button
        android:layout_marginTop="10dp"
        android:id="@+id/btn_delete"
        android:layout_width="200dp"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:background="@drawable/button_round"
        android:textColor="#FF0000"
        android:foreground="?android:attr/selectableItemBackground"
        android:text="删除" />

    <ImageView
        android:layout_gravity="center"
        android:layout_margin="6dp"
        android:layout_width="200dp"
        android:layout_height="1dp"
        android:background="@drawable/img_round"/>

    <Button
        android:layout_marginBottom="5dp"
        android:id="@+id/btn_change"
        android:layout_width="200dp"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:background="@drawable/button_round"
        android:foreground="?android:attr/selectableItemBackground"
        android:textColor="#000000"
        android:text="修改" />
</LinearLayout>

dialog_round.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp"/>
    <!-- 填充颜色 -->
    <solid android:color="#ffffffff" />

</shape>

styles.xml中添加

    <!--对话框透明背景-->
    <style name="Translucent" parent="android:style/Theme.Dialog">

        <!--边框-->
        <item name="android:windowFrame">@null</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsFloating">true</item>
        <!--半透明-->
        <item name="android:windowIsTranslucent">false</item>
        <!--无标题-->
        <item name="android:windowNoTitle">true</item>
        <!--提示框背景(透明)-->
        <item name="android:windowBackground">@color/transparent</item>
        <!--模糊-->
        <item name="android:backgroundDimEnabled">true</item>

    </style>
    <color name="transparent">#00000000</color>

HomeFragment中

final BottomSheetDialog bottomSheetDialog=new BottomSheetDialog(getContext(),R.style.Translucent);
        final View dialog_view=LayoutInflater.from(getContext()).inflate(R.layout.bottom_dialog,null);
        //设置不充满底部
        Window window=bottomSheetDialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.getDecorView().setPadding(150, 0, 150, 20);

最终效果:

Last modification:July 21st, 2020 at 10:48 pm