2016-09-28 34 views
2

我有這樣的佈局在app_bar_main.xml:如何在只有一些片段中啓用tablayout?

<?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>

,我啓用/帶接口禁用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>

所以我怎麼能啓用/禁用tabLayout只有一些嵌套片段?我必須把它放在碎片的佈局中,否則我可以在活動中使用它?

非常感謝。

回答

2

tableLayout位於活動佈局中,但您在片段佈局中進行搜索以找到它。
添加這裏面的片段onCreateView()和刪除其他代碼:

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

而且tabLayout大多數是公開的對象activty

+0

現在它的工作,非常感謝你! – nani

+1

享受編碼! –

相關問題