android - 主细节中的变量 mTwoPane 没有改变

android - the variable mTwoPane in master detail doesn't change

我正在尝试使用 masterDetail 布局变化尽可能小,试图了解它们的默认结构。

我正在我的三星 S5 上试用应用程序。

应用程序正常运行,但在横向模式下不会在布局的右侧添加片段。

我认为问题是变量 mTwoPane 没有改变值,仍然是 false,这是因为 findViewById(R.id.pokemon_detail_container) != null 显然永远是 false。

这是代码:

MainActivity 中 onCreate 中的代码块:

        if (findViewById(R.id.detail_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-w900dp).
        // If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;
    }

评论是默认的。

activity_detail.xml中的detail_conter:

<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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.soissy.masterdetail.DetailActivity"
tools:ignore="MergeRootFrame">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/detail_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

这是代码点,如果 mTwoPane 为真,我将添加片段。此方法位于 MainActivity 的内部 class 内:

        public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.mIdView.setImageResource(mValues.get(position).id);
        holder.mNameView.setText(mValues.get(position).name);


        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mTwoPane) Log.d("mTwoPane", "is true");
                else Log.d("mTwoPane", "is false");
                if (mTwoPane) {
                    Bundle arguments = new Bundle();
                    arguments.putInt(DetailFragment.ARG_ITEM_ID, holder.mItem.id);
                    DetailFragment fragment = new DetailFragment();
                    fragment.setArguments(arguments);
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.detail_container, fragment)
                            .commit();
                } else {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, DetailActivity.class);
                    intent.putExtra(DetailFragment.ARG_ITEM_ID, holder.mItem.id);

                    context.startActivity(intent);
                }
            }
        });
    }

我不明白我要做什么才能添加片段(mTwoPane=true)

您可能没有选择您认为的布局。请确保您正在遵循有关如何 Support Multiple Screens 的文档。测试 Android 是否选择了您期望的布局的一种简单方法是更改​​该布局文件中的某些内容,并且仅更改该文件,这在显示时对您来说是显而易见的 - 一种颜色或文本,因为例子。如果您没有看到变化,那么您看到的不是您认为的布局。

我找到问题了。当您使用 Android Studio 创建一个 MasterDetail "activity" 开头时,它会自动为两个窗格模式创建布局。但是,我认为仅适用于平板电脑,因为此布局的具体选项是 "w900dp"。所以我为横向模式创建了另一个布局。然后从布局 "with w900dp" 复制并粘贴到我的横向新布局,现在一切正常。

我遇到了布尔值始终为 false 的相同问题,但就我而言,我将适配器作为单独的 java 文件,因此在我的列表片段中实现了一个实例 callbck,然后我移动了 this

if findViewById R.id.detail_container !=null mTwoPane = true;

在 If(mTwopane) 之前进入我的 onclick 回调。现在它起作用了,我可以横向查看 Twopane,当我翻转到纵向时。细节活动开始了,所以这个检查视图是否为 != null 的条件不是 运行 ,这就是为什么布尔值保持 false 我希望这对某些人有帮助