2016-09-28 4 views
2

Ich habe dieses Layout in der app_bar_main.xml:Wie kann Tablayout nur in einigen Fragmenten aktiviert werden?

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

wo ich aktivieren/die collapsingToolbarLayout mit Schnittstelle deaktivieren.

Das Problem ist, wenn ich das TabLayout in einem verschachtelten Fragment verwenden möchte. In der Tat, wenn ich versuche zu tun:

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); 
    } 

ich eine Nullpointer bekommen, weil in meinem Layout des verschachtelten Fragments habe ich nur die 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>

Wie kann ich TabLayout nur in einem verschachtelten Fragment aktivieren/deaktivieren? Ich muss es in das Layout von Fragmenten setzen oder ich könnte es in Aktivität tun?

Vielen Dank.

Antwort

2

tableLayout befindet sich im Aktivitätslayout, Sie suchen jedoch im Fragmentlayout, um es zu finden.
Fügen Sie diese innerhalb Fragmente onCreateView() und anderen Code entfernen:

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

Und tabLayout am meisten in activty

+0

jetzt öffentliche Aufgabe sein, es ist Arbeit, vielen Dank! – nani

+1

Codierung genießen !! –

Verwandte Themen