3

enter image description here

refer to the image, I want to create a parallax scrolling effect within a viewpager. the ImageView and the webview is the layout from the view pager fragment while the toolbar is on the activity that hold the viewpager.

I had my parallax scrolling effect working but not the show and hide toolbar below is my code for both activity and fragment.

main.xml

<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/htab_maincontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/htab_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:minHeight="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>

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

fragment.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:fitsSystemWindows="true"
android:id="@+id/htab_maincontent"
android:layout_height="match_parent"
android:layout_width="match_parent">

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

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

        <ImageView
            android:id="@+id/htab_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:src="@drawable/tes"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />
    </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"
    android:layout_gravity="fill_vertical"
    android:clipToPadding="false"
    android:isScrollContainer="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!--<TextView
        android:id="@+id/pagename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />-->

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></WebView>
    </LinearLayout>


</android.support.v4.widget.NestedScrollView>

so my question is how to make toolbar in activity get the scrolling behaviour in the fragment and show or hide when user scroll up and down

MinFu
  • 343
  • 1
  • 13

2 Answers2

0

Try this

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

Hope this will helps you

saeed
  • 1,855
  • 1
  • 15
  • 31
  • you tried? it worked for you? I had tried, it not working for me – MinFu Mar 09 '16 at 10:05
  • in your edited answer mean i had to wrap my viewpager with another nestedscrollview? I tried all my viewpager content missing covered by the nestedscrollview i can't see the viewpager content – MinFu Mar 09 '16 at 10:14
0

you will have to do it the following way

    <CoordinatorLayout>
       <AppBarLayout>
         <Toolbar >
           <ImageView      
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:minHeight="100dp"/>
        </Toolbar>
      </AppBarLayout>
    </CoordinatorLayout>

also refer this link https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

Mightian
  • 6,853
  • 3
  • 37
  • 50
  • my imageview is located inside fragment and i will have different image for each page in my view pager. any way to keep the imageview within the fragment and have the effect i wanted? – MinFu Mar 10 '16 at 01:57
  • You can have a frame layout with the parallax flag to which you can set the fragment and in code dynamically create the imageview – Mightian Mar 10 '16 at 02:10
  • sorry not really get what you mean. why i need to have a frame layout with parallax flag in fragment and dynamically create the imageview? My parallax scrolling effect for my imageview in fragment was working fine. my problem was i can't hide the toolbar in my activity so that i can giv a full screen view of my content to the user when the are scrolling through the content – MinFu Mar 10 '16 at 02:22