DialogFragmentビューから親アクティビティビューへのタッチイベントのディスパッチ
質問
これが私のレイアウトがどのように見えるかです:
カスタムビュー(onTouchイベントを単独で処理するview1)と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);
.....
}
}
レイアウトファイルは次のようになります:
<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.ダイアログフラグメントを閉じます 2.onTouchを親アクティビティに渡し、ユーザーがビューと対話できるようにします。
したがって、基本的に、View1の上に指をドラッグすると、ダイアログを閉じて、view1に対するドラッグ操作を続行したいと思います。
これを達成することは可能ですか?
ル:また、これは動作しません:
layMain.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
getActivity().dispatchTouchEvent(event);
return false;
}
});
解決
最後に、LUKSPROGが提案されているので、私は秘密のダイアログフレグメントで終わりました。私はアクティビティのFramelayoutでホストされている単純なフラグメントを使用してテストをしましたので、それが必要な場所のオーバーレイのように見えます。このようにして、私はまだ私が残りのビューに必要なすべての対話を得ます。
あなたのサポートのためにありがとうございました。
他のヒント
あなたがダイアログを隠したにもかかわらずこれは他の人に役立つかもしれません。
実際の装飾ビューを手に入れる必要があり、そこでタッチイベントを傍受する必要があります。
@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);
}
. 望ましい効果を得るために代わりに警告ダイアログを使用します。
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();
.
これが役に立つことを願っています:)
ダイアログの透過的なビューでboolean onInterceptTouchEvent(MotionEvent ev)
を上書きすることができます。
または表示クラスをオーバーライドしたくない場合は、ダイアログでboolean dispatchTouchEvent (MotionEvent ev)
を上書きし、 MotionEvent オブジェクト
あなたがすでに正常にあなたのタッチイベントをキャプチャしている場合 LinearLayout
, 、私は周囲の透明なビューであることを理解しています Dialog
次に、親に戻るインターフェイスでこれを達成できるはずです Activity
そして、dialogクラス内のpublicメソッドへの呼び出し。私は次の変更を行います:
まず、TransparentView内でインターフェイスを定義し、親アクティビティへのコールバックを確立するためのpublicメソッドを追加します。
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;
}
}
アイデアは、motionイベントをキャッチし、手動でホスティングアクティビティに戻すことです。Motionイベントをactivityに戻したら、ダイアログを閉じるだけです。 DialogFragment
すでに持っています メソッドを閉じる インスタンス変数を使用してダイアログへの参照を保持している限り、次のことができるように組み込まれています
@Override
public void onMotionEvent(MotionEvent e){
mDialog.dismiss();
}
それがうまくいかない場合は、dialogクラスにメソッドを追加して呼び出すだけです。あなたの例からわかるように、タッチが発生したときにダイアログを閉じるだけなので、透明なビューでキャプチャされたモーションイベントで何もする必要はありません。コールバックはタッチが発生したときにのみアクティブにする必要があるため、チェックするものは何もありません(ただし、後でこれを拡張して Handler
そして、タッチが指定された時間続いた場合にのみダイアログを閉じます)。
私はBottoSheetDialogに関して同じ問題がありました。私は私の問題を解決しました:
final BottomSheetDialog dialog = getDialog();
final View touchOutside = dialog.findViewById(R.id.touch_outside);
touchOutside.setOnTouchListener((v, event) -> {
getActivity().dispatchTouchEvent(event);
return false;
});
.
は、フィンの解決策のように見えます。