DialogFragment背后的活动仍然接受触动

时间:2016-11-04 15:48:38

标签: java android android-fragments

我有以下DialogFragment

public class DetailItemFragment extends AppCompatDialogFragment {


@BindView(R.id.task_detail_name)
AppCompatTextView taskDetailNameText;
@BindView(R.id.task_detail_description)
AppCompatTextView taskDetailDescriptionText;
@BindView(R.id.task_detail_priority)
AppCompatTextView taskDetailPriorityText;
@BindView(R.id.toolbar)
Toolbar toolbar;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCanceledOnTouchOutside(true); //doesn't work
    dialog.setCancelable(true); //doesn't work
    return dialog;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_detail_view, container, false);
    ButterKnife.bind(this, view);

    //Get the Json back and parse it as a Task
    Bundle bundle = getArguments();
    final String taskAsJson = bundle.getString("task");
    Task task = new Gson().fromJson(taskAsJson, Task.class);

    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.close);
    }
    setHasOptionsMenu(false);

    taskDetailNameText.setText(task.getName());
    taskDetailDescriptionText.setText(task.getDescription());
    taskDetailPriorityText.setText(task.getPriority().toString());

    return view;
}

我执行像这样的片段交易

//Put the clicked item into a Bundle for the next fragment to consume
DialogFragment detailItemFragment = new DetailItemFragment();
Bundle args = new Bundle();
args.putString("task", new Gson().toJson(mTaskListAdapter.getItem(position)));
detailItemFragment.setArguments(args);

getActivity().getSupportFragmentManager().beginTransaction()
             .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
             .add(R.id.task_detail_fragment_container, detailItemFragment)
             .addToBackStack(null)
             .commit();

我正在尝试完成以下操作: *在平板电脑/大屏幕上,它显示为对话框弹出窗口 *在手机上,它会显示一个全屏对话框(几乎试图复制Google日历处理打开活动详情的方式)

这就是我xlarge/activity_main.xml的样子

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/task_detail_fragment_container"
        android:layout_width="800dp"
        android:layout_height="400dp"
        android:layout_centerInParent="true"
        android:elevation="16dp" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/ToolbarStyle"
            android:background="@color/colorPrimary"
            app:title="@string/app_name"
            app:titleTextColor="@color/white" />
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/task_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add"
        android:layout_width="@dimen/fab_normal_size"
        android:layout_height="@dimen/fab_normal_size"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="@dimen/fab_padding"
        android:layout_marginEnd="@dimen/fab_padding"
        android:src="@drawable/add_black"
        app:elevation="@dimen/fab_elevation"
        app:fabSize="normal"
        app:layout_anchorGravity="bottom|right|end"
        app:pressedTranslationZ="@dimen/fab_pressed_elevation" />
</RelativeLayout>

您会注意到我的屏幕中心有FrameLayout,用于加载DetailItemFragment。也许这不是正确的方法 - 但我想提出它,以防万一成为问题。

这就是fragment_detail_view.xml布局的样子

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/task_detail_background"
    android:clickable="true"
    android:elevation="8dp"
    tools:context="io.havoc.todo.view.fragments.DetailItemFragment">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_tall_height"
            android:background="?attr/colorPrimary">

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/task_detail_name"
                style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start|bottom"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:gravity="start|bottom"
                android:padding="8dp"
                android:text="Task detail"
                android:textColor="@color/white"
                android:textSize="24sp" />
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginStart="86dp"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.AppCompatTextView
            style="@style/TextAppearance.AppCompat.Body2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/spacing_margin"
            android:text="@string/hint_task_description"
            android:textColor="@color/colorAccent" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/task_detail_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Description text"
            android:textColor="@color/text_primary"
            android:textSize="18sp" />

        <android.support.v7.widget.AppCompatTextView
            style="@style/TextAppearance.AppCompat.Body2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/spacing_margin"
            android:text="@string/hint_task_priority"
            android:textColor="@color/colorAccent" />

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/task_detail_priority"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Priority"
            android:textColor="@color/text_primary"
            android:textSize="18sp" />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

问题

在平板电脑上打开DialogFragment时,后台中的Activity仍会接受触摸(即我可以与Activity进行互动,就好像对话框不存在一样) ;此外,我不知道在触摸它之外时如何让DialogFragment解雇。我尝试了以下内容:

  • dialog.setCanceledOnTouchOutside(true);(在onCreateDialog()方法中)
  • dialog.setCancelable(true);(在onCreateDialog()方法中)
  • dialog.setCanceledOnTouchOutside(false);(在onCreateDialog()方法中)
  • getDialog().setCanceledOnTouchOutside(true); setCancelable(true);(导致NullPointerExceptions - 在onCreateView()中为DialogFragment执行了

我已经经历了所有其他问题,我可以找到并且没有任何对我有用 - 所以显然我做错了。

我感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

您添加FrameLayout TO的task_detail_fragment_containerDetailItemFragment)的大小与父级不同,这意味着它不会覆盖整个布局:

...
        android:layout_width="800dp"
        android:layout_height="400dp"
...

要解决此问题,请将task_detail_fragment_container设置为与父布局(match_parent)相同的大小,并在container布局后引入另一个布局,以限制对话框布局中对话框的大小:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" 
    android:clickable="true"
    android:elevation="8dp"
    tools:context="io.havoc.todo.view.fragments.DetailItemFragment">

     <FrameLayout
         andorid:width="800dp"
         android:height="400dp"
         android:layout_centerInParent="true">

         <android.support.design.widget.AppBarLayout ...
         //rest of your xlm here
    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:0)

更改为.dialog.setCancelable(false);

如果是带有“确定”和“取消”按钮的对话框,则可以使用:

.setCancelable(closeActivity == true ? false : true)

答案 2 :(得分:0)

更改此

dialog.setCanceledOnTouchOutside(true);

dialog.setCanceledOnTouchOutside(false);