首页 > 解决方案 > setOnLongClickListener 中对话框自动关闭的问题

问题描述

我对 setOnLongClickListener 有疑问。当我使用此方法时,我希望将打开的对话框在我将手指从屏幕上移开时自动关闭(该方法的 return false 应该这样做)。用这种方法可以做到这一点,还是有其他合适的方法?

创建对话框

protected void showDialogLongClick(final int position){

        final Dialog dialog = new Dialog(activity);
        dialog.setCancelable(true);

        View v  = activity.getLayoutInflater().inflate(R.layout.onlongclick_card_mygift,null);
        dialog.setContentView(v);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        nameLong = v.findViewById(R.id.name_longclick);
        descriptionLong = v.findViewById(R.id.gift_description);
        addressLong = v.findViewById(R.id.gift_address);
        imageCategory = v.findViewById(R.id.small_category);

        nameLong.setText(myGift.get(position).getName());
        descriptionLong.setText(myGift.get(position).getDescription());
        addressLong.setText(myGift.get(position).getAddress());
        String cat = myGift.get(position).getCategory();
        changeCategoryImage(cat, imageCategory);

        dialog.show();

    }

听众

holder.card.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                showDialogLongClick(position);

                return false;
            }
        });

XML 卡片对话框

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"
    app:cardCornerRadius="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_card_mygift"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/name_longclick"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="#FFFFFF"
                android:textSize="25sp"
                android:layout_weight="1"
                android:layout_marginEnd="10dp"/>

            <ImageView
                android:id="@+id/small_category"
                android:layout_weight="0"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center_vertical"/>

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#F44336"
            android:textSize="22sp"/>

        <TextView
            android:id="@+id/gift_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#FFFFFF"
            android:textSize="20sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#F44336"
            android:textSize="22sp"/>

        <TextView
            android:id="@+id/gift_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#FFFFFF"
            android:textSize="20sp"/>


    </LinearLayout>



</androidx.cardview.widget.CardView>

标签: androidandroid-studioandroid-recyclerviewdialoggesture

解决方案


view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                //Dismiss the dialog
            }
            return false;
        }
    });

view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            //Create the dialog
            return true;
        }
    });

使用 setOnLongClickListener 创建对话框并使用 setOnTouchListener 关闭对话框。

因此,您应该将 setOnTouchListener 添加到视图中。当 MotionEvent 动作为 MotionEvent 时调用 dialog.dismiss()。ACTION_UP(用户抬起手指)。

如果视图在滚动视图中,则不会调用 onTouchListener 视图。一个简单的解决方案是在滚动视图中添加一个 onTouchListener 并在用户抬起手指时关闭对话框。

scrollView.setOnTouchListener( new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                //Dismiss the dialog
                return true;
            }
            return false;
        }
    });

推荐阅读