如何仅在某些片段中启用 tablayout?

How to enable tablayout in only some fragments?

我在 app_bar_main.xml 中有这个布局:

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/collapse"
                android:layout_width="match_parent"
                android:layout_height="256dp"
                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/ThemeOverlay.AppCompat.Light" />

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

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

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

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />



    <include layout="@layout/content_main" />

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

我 enable/disable 带界面的 collapsingToolbarLayout。

问题是当我想在嵌套片段中使用 TabLayout 时。 事实上,如果我尝试做的是:

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View inflatedView = inflater.inflate(R.layout.fragment_add_parking, container,false);

    tabLayout = (TabLayout)inflatedView.findViewById(R.id.tabs);
    tabLayout.setVisibility(View.VISIBLE);
    }

我得到一个 NullPointerException,因为在我的嵌套片段布局中我只有 viewPager:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">


    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="436dp"
        android:layout_gravity="center"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/tabs"
        android:layout_alignParentBottom="true">

    </android.support.v4.view.ViewPager>



</RelativeLayout>

所以我怎么能 enable/disable tabLayout 只在一些嵌套片段中?我必须把它放在片段的布局中,否则我可以在 activity?

中使用它

非常感谢。

tableLayout 在 activity 布局中,但您在片段布局中搜索才能找到它。
将其添加到片段 onCreateView() 中并删除其他代码:

((YourActivity)getActivity()).tabLayout.setVisibility(View.Gone|View.Visible);

而tabLayout在activty

中大部分是public对象