2

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>
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
  • 1
    I was able to achieve this by attaching a layout listener to the pager. Here's an example: http://stackoverflow.com/a/17833392/5482166 – davehenry Feb 10 '16 at 22:06
  • 1
    @davehenry thank you for your suggestion, but I was wondering if this could be done only using XML. I know how to do it using this listener. – Georgi Koemdzhiev Feb 10 '16 at 22:33
  • 1
    Gotcha, I think you could achieve it more cleanly with a custom layout behavior. Not sure if its possible with just xml – davehenry Feb 10 '16 at 22:47

2 Answers2

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.

ariochdivij666
  • 396
  • 3
  • 8
  • thank you for your suggestion, but I was wondering if this could be done only using XML. I know how to do it using this listener. – Georgi Koemdzhiev Feb 10 '16 at 22:34
  • in my experience which is quite limited, I have not handled states like this in the xml file. So unless there is some way to specify what the viewpager does in the xml file based on the contents which i do not think it can the listener is probably ur best bet. – ariochdivij666 Feb 11 '16 at 01:44
  • Yes, I guess I have to stick with the onPageChangeListener approach. I spend quite a bit of time researching how to do it using XML and CoordinatorLayout. Thanks! – Georgi Koemdzhiev Feb 11 '16 at 08:20
1

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);
}
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126