Show toolbar when view pager is swiped. [Coordinat

2019-02-25 23:59发布

问题:

in my app I am using a viewpager with 3 fragments. In two of those I have recuclerviews. I took advatngage of the new Coordinator layout and made my toolbar hides/shows when scrolling on a recyclerview. My problem is the following Say the user is scrolling on a recyclerview list in fragment A and thus the toolbar is hidden. After that, the user performs a swipe and goes to fragment B which does not have a recycle view to scroll so the toolbar can appear again. Is there a way I can alter the layout_behaviout so that when the user swipes on the view pager the toolbar to be shown?

NOTE: IF it is possible, I want to achieve that only using XML.

This is my main_layput xml code:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.studentsins.lust.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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"
        app:layout_scrollFlags="scroll|enterAlways" />

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


</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"/>


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

<com.getbase.floatingactionbutton.FloatingActionsMenu
    android:id="@+id/floatingActionMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    fab:fab_addButtonColorNormal="@color/blood_orange"
    fab:fab_addButtonColorPressed="@color/dirtyWhite"
    fab:fab_addButtonPlusIconColor="@color/dirtyWhite"
    fab:fab_addButtonSize = "normal"
    fab:fab_labelStyle="@style/menu_labels_style"
    fab:fab_labelsPosition="left"
    app:layout_anchor="@id/viewpager"
    app:layout_anchorGravity="bottom|end">

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/createPlanBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fab:fab_colorNormal="@color/blood_orange"
        fab:fab_title="Create a plan"
        fab:fab_size="normal"
        app:fab_icon="@drawable/ic_event_white_48dp"
        fab:fab_colorPressed="@color/dirtyWhite"/>

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/changeStatusBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fab:fab_colorNormal="@color/blood_orange"
        fab:fab_size="normal"
        app:fab_icon="@drawable/ic_textsms_white_48dp"
        fab:fab_title="Change status"
        fab:fab_colorPressed="@color/dirtyWhite"/>

</com.getbase.floatingactionbutton.FloatingActionsMenu>

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

回答1:

You should be able to do this using the ViewPager.OnPageChangedListener and based on the page u are on which u can get from the onPageSelected(int position) method that is part of the listener. Hope this is what u are looking for.



回答2:

I was able to achieve this by referencing the AppBarLayout and calling the "setExtended" method like that:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //Make sure that the fab is visible when scrolling the pages...
            MainActivity.mFloatingActionsMenu.animate()
                    .setDuration(150)
                    .translationY(0);
            //show the toolbar
            expandToolbar();

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

my expandToolbar method:

public void expandToolbar(){
    //setExpanded(boolean expanded, boolean animate)
    AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBarLayouy);
    appBarLayout.setExpanded(true, true);
}