nani nani - 2 months ago 19
Android Question

How to enable tablayout in only some fragments?

I have this layout in the 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>





where I enable/disable the collapsingToolbarLayout with interface.

The problem is when I want to use the TabLayout in a nested fragment.
In fact, if I try to do is:

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


I get a NullPointerException, because in my layout of the nested fragment I have only the 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>





so how could I enable/disable the tabLayout in only some nested fragment? I must put it in the layout of fragments or I could ability it in activity?

Thank you so much.

Answer

tableLayout is in the activity layout but you search in fragment layout to find it.
add this inside fragments onCreateView() and remove other code :

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

And tabLayout most be public object in activty

Comments