Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How change Activity AppbarLayout in Fragment

I want to create an app designed like Play Store. So there is a Navigation Drawer. when We click Store home Navigation Item or (first fragment after launching app) there is seeming tablayout into AppBarLayout. The code is looking like this in the Activity

<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.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

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

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

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

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

when we click a App in one of the category The App Details Fragment view Seems a CollapsingToolbarLayout in AppbarLayout. The code like into this....

<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:fitsSystemWindows="true">

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

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

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax" />

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

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        ...........
    </android.support.v4.widget.NestedScrollView>

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

so How i Implement Different AppbarLayout In Different Fragment with Containing A Drawer. Please Help me

like image 860
ashraful Avatar asked Oct 31 '22 17:10

ashraful


1 Answers

The way I handled it was by putting it all in one layout:

<android.support.v4.widget.DrawerLayout xmlns: android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true" tools:context=".MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:fitsSystemWindows="true">

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

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

            <android.support.design.widget.TabLayout android:id="@+id/tabbar"
                android:layout_width="match_parent" android:layout_height="wrap_content"
                android:visibility="gone" app:tabIndicatorColor="?attr/colorAccent"/>

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

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

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

As you can see, I set the TabLayout visibility to gone. I also used a FrameLayout instead of the NestedScrollView (I found it caused more problems than it solved). I would change out each Fragment from the navigation drawer by calling:

getSupportFragmentManager().beginTransaction()
        .replace(R.id.fragment_holder, new_fragment).commit()

Then in my MainActivity I would have a method that returns the TabLayout:

public TabLayout getTabLayout() {
    return (TabLayout) findViewById(R.id.tabbar);
}

Then in each Fragment I would call that method in onResume, and set the TabLayout to visible:

@Override
public void onResume() {
    super.onResume();
    if(mTabLayout != null) {
        mTabLayout.setVisibility(View.VISIBLE);
    }
}

And in onPause I would remove all of the tabs and set the visibility back to gone:

@Override
public void onPause() {
    super.onPause();
    mTabLayout.removeAllTabs();
    mTabLayout.setVisibility(View.GONE);
}

And finally I would set the TabLayout in onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.view_pager);
    mTabLayout = ((MainActivity) getActivity()).getTabLayout();

    // TODO: set up TabLayout and ViewPager
}

As a side note, I ran into a few problems when I tried to add a Fragment to the back stack:

getSupportFragmentManager().beginTransaction()
        .add(R.id.fragment_holder, new_fragment).addToBackStack(null).commit();

I ended up extending android.support.v4.app.Fragment, and adding a method to call during onBackStackChanged() in the MainActivity which implements FragmentManager.OnBackStackChangedListener:

@Override
public void onBackStackChanged() {
    ((StackFragment) getSupportFragmentManager()
        .findFragmentById(R.id.fragment_holder)).onTopOfStack();
}

I would use onTopOfStack() instead of onResume() to make UI changes to my MainActivity.

like image 64
Bryan Avatar answered Nov 15 '22 05:11

Bryan