将Touch事件从DialogFragment视图调度到父活动视图

Ali*_*lin 5 android ontouchlistener touch-event android-fragments android-dialogfragment

这是我的布局的样子:

在此处输入图片说明

我有一个父活动,它具有一个自定义视图(view1本身可处理onTouch事件)和2个按钮(view2和view3)。DialogFragment具有显示的可见布局,其余均为透明。我的对话框片段如下所示:

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

        .....
        }

}
Run Code Online (Sandbox Code Playgroud)

布局文件如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

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
    }

}
Run Code Online (Sandbox Code Playgroud)

当用户在DialogFragment的可见布局之外按下时,我要:1.关闭对话框片段2.将onTouch传递给父活动,并允许用户与视图进行交互。

因此,基本上,如果我在View1上拖动手指,我想关闭该对话框并继续针对View1的拖动交互。

这有可能实现吗?

LE:这也不起作用:

layMain.setOnTouchListener(new OnTouchListener() {

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

            return false;
        }
    });
Run Code Online (Sandbox Code Playgroud)

Ali*_*lin 3

最后,正如 Luksprog 所建议的,我最终放弃了 DialogFragment。我使用简单的 Fragment 进行了测试,该 Fragment 托管在活动的 FrameLayout 中,因此它看起来就像我需要它的地方的叠加层。通过这种方式,我仍然可以获得与其余视图所需的所有交互。

感谢大家的支持。