Android两个窗格布局,找不到视图

时间:2016-04-18 21:15:02

标签: android android-layout android-fragments

现在我的应用程序在常规屏幕上正常工作。 我很难搞清楚如何管理当前用户界面来处理两个面板布局。

现在我的DetailActivity有一些要加载的UI元素,3个片段在不同的选项卡上,控制每个FAB的功能,FAB动画和用于选项卡布局的片段页面适配器。

如何在单独的类中以及在两个窗格布局的主要活动中管理所有这些元素和逻辑?

这是我的主要活动

public class MainActivity extends AppCompatActivity {

public static boolean TWO_PANE;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (findViewById(R.id.movie_detail_container) != null) {
        // The detail container view will be present only in the large-screen layouts
        // (res/layout-sw600dp). If this view is present, then the activity should be
        // in two-pane mode.
        TWO_PANE = true;
        if (savedInstanceState == null) {
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
        }
    }else{
        TWO_PANE = false;
    }
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, new FragmentMain())
            .commit();
}

Fragment Main显示电影网格布局,点击时如果常规手机打开另一个活动,否则电影细节应显示在右上角。

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Defines the xml file for the fragment
    View rootView =  inflater.inflate(R.layout.fragment_main, container, false);
    imageAdapter = new ImageAdapter(getContext());
    gridView = (GridView) rootView.findViewById(R.id.gridView);
    gridView.setAdapter(imageAdapter);
    //Start DetailActivity with the movie details.
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            if (MainActivity.TWO_PANE){
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constants.BUNDLE_CONSTANT, mMovieElements.get(position));
                FragmentDetail fragment = new FragmentDetail();
                fragment.setArguments(bundle);
                getChildFragmentManager()
                        .beginTransaction()
                        .replace(R.id.movie_detail_container, fragment)
                        .addToBackStack(null)
                        .commit();
            }else{
                //Starting detail activity
                Intent intent = new Intent(getContext(), DetailActivity.class);
                intent.putExtra(Intent.EXTRA_TEXT, mMovieElements.get(position));
                startActivity(intent);
            }
        }
    });

    return rootView;
}

土地\活动-主要

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
tools:context=".ui.ui.MainActivity">

<!--
This layout is activity_main two-pane layout for the Items master/detail flow.
-->
<FrameLayout
    android:id="@+id/container"
    class = "com.joaonogueira.nmovies.ui.ui.FragmentMain"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    tools:layout="@android:layout/list_content" />

<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:name="com.joaonogueira.nmovies.ui.ui.FragmentDetail"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:layout_weight="4"
    android:id="@+id/movie_detail_container"
    tools:context="com.joaonogueira.nmovies.ui.ui.DetailActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <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:expandedTitleGravity="center"
            >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="@dimen/app_bar_height"
                android:id="@+id/posterImage"
                android:background="@drawable/nopicture"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>


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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"/>


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

    <android.support.v4.view.ViewPager
        android:id="@+id/MovieContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/btn_star_big_off"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:backgroundTint="@color/colorPrimaryDark" />

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

My Fragment Detail无法启动,我收到以下错误:

  

找不到ID为0x7f0c0070(com.joaonogueira.nmovies:id / movie_detail_container)的片段FragmentDetail {1b71bc14#0 id = 0x7f0c0070 bundle}

1 个答案:

答案 0 :(得分:0)

您正在尝试从片段本身替换片段

getChildFragmentManager()
                        .beginTransaction()
                        .replace(R.id.movie_detail_container, fragment)
                        .addToBackStack(null)
                        .commit();

但由于movie_detail_container不存在于片段视图中,而是活动布局的一部分,因此您将收到该错误。

您需要做的是在您的活动中实现一个侦听器界面,并让您的片段听取它。最后,您调用该侦听器以使您的活动替换新片段。 如果您不知道该怎么做,请阅读this