Sending a Touch Event from the Dialog Box to the Parent Activity View

This is what my layout looks like:

enter image description here

I have a parent activity that has a user view (view1, which itself handles onTouch events) and 2 buttons (view2 and view3). The dialog box has a visible layout, and the rest are transparent. My dialog box fragments look like this:

public class FragmentText extends DialogFragment{

  public static FragmentText newInstance() {
        FragmentText frag = new FragmentText();

        frag.setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Translucent_NoTitleBar);
        return frag;
    }

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        // instantiate the custom layout
        final View layout = inflater.inflate(R.layout.fragment_layout, null);

        .....
        }

}

and the layout file is as follows:

<com.TransparentView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="70dp"
    android:background="@color/transparent"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="70dp" >

    <LinearLayout
        android:id="@+id/layContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="5dp"
        android:background="@drawable/bk_text_edit"
        android:orientation="vertical"
        android:padding="@dimen/margin" >

          all my layouts and buttons
    </LinearLayout>
</com.TransparentView>

and

public class TransparentView extends LinearLayout {

    @SuppressLint("NewApi")
    public TransparentView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public TransparentView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TransparentView(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false; // event get propagated
    }

}

When the user clicks on the appearance of the DialogFragment dialog box, I want to: 1. release the dialog fragment 2. pass onTouch to the parent activity and allow the user to interact with the views.

So basically, if I drag my finger over View1, I want to reject the dialog and continue my drag-and-drop interaction with view1.

?

LE: :

layMain.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            getActivity().dispatchTouchEvent(event);

            return false;
        }
    });
+5
6

, Luksprog, DialogFragment. , , FrameLayout, , , . , .

.

+2

DialogFragment, .

:

 @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);


    getDialog().getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            getActivity().dispatchTouchEvent(event);
            return false;
        }
    });

    return super.onCreateView(inflater, container, savedInstanceState);
}
+1

Alert, :

AlertDialog.Builder Dialog = new AlertDialog.Builder(this);
        Dialog.setMessage("Text you wish to enter in the dialog"));
//Use this if you would like to include a button at the bottom of the alert dialog. Otherwise just leave it blank.
        Dialog.setNeutralButton("Back to App", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        Dialog.show();

, :)

0

boolean onInterceptTouchEvent(MotionEvent ev) , , , , false ( , ).

, View, boolean dispatchTouchEvent (MotionEvent ev) , , MotionEvent.

0

LinearLayout, , , , Dialog, Activity . :

- TransparentView .

public class TransparentView extends LinearLayout {

   public interface TouchCallback{
       public void onMotionEvent(MotionEvent e);
   }

   private TouchCallback mCallback;

   @SuppressLint("NewApi")
   public TransparentView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
   }

    public TransparentView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TransparentView(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        callback.onMotionEvent(ev);
        return false; // event get propagated
    }

    public setCallback(TouchCallback callback){
        mCallback = callback;
    }

}

, . , , , , . DialogFragment , , ,

@Override
public void onMotionEvent(MotionEvent e){
    mDialog.dismiss();
}

, . , , , , , , , . , , ( , Handler , ).

0

BottomSheetDialog. :

final BottomSheetDialog dialog = getDialog();
final View touchOutside = dialog.findViewById(R.id.touch_outside);
touchOutside.setOnTouchListener((v, event) -> {
    getActivity().dispatchTouchEvent(event);
    return false;
});

, .

0

Source: https://habr.com/ru/post/1543126/


All Articles