android 活动图片的dialog,android – 从DialogFragment视图到父活动视图的Dispatch Touch事件...

本文讨论如何在Android中处理DialogFragment的触摸事件,目标是当用户触摸DialogFragment的可见部分时,关闭对话框并将事件传递给父活动的自定义视图,以便用户能继续与视图进行交互。示例代码展示了如何尝试实现这一功能,但遇到了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是我的布局的样子:

我有一个父活动,它有一个自定义视图(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);

.....

}

}

并且布局文件如下所示:

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

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

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.关闭对话框片段

2.将onTouch传递给父活动,并允许用户与视图交互.

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

这有可能实现吗?

LE:这也行不通:

layMain.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

getActivity().dispatchTouchEvent(event);

return false;

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值