gpt4 book ai didi

android - 将 Touch 事件从 DialogFragment View 分派(dispatch)到父 Activity View

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:27 26 4
gpt4 key购买 nike

这是我的布局的样子:

enter image description here

我有一个父 Activity ,它有一个自定义 View (自己处理 onTouch 事件的 view1)和 2 个按钮(view2 和 view3)。 DialogFragment 显示了一个可见的布局,其余部分是透明的。我的对话 fragment 如下所示:

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

.....
}

}

布局文件如下所示:

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

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
}

}

当用户在 DialogFragment 的可见布局之外按下时,我想:1.关闭对话框 fragment 2. 将 onTouch 传递给父 Activity 并允许用户与 View 进行交互。

所以基本上,如果我将手指拖到 View1 上,我想关闭对话框并继续我对 view1 的拖动交互。

这有可能实现吗?

LE:这也行不通:

layMain.setOnTouchListener(new OnTouchListener() {

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

return false;
}
});

最佳答案

我在使用 BottomSheetDialog 时遇到了同样的问题。我解决了我的问题:

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

看起来 Finn 的解决方案应该可以正常工作。

关于android - 将 Touch 事件从 DialogFragment View 分派(dispatch)到父 Activity View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24024332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com