Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollapsingToolbarLayout Issue with GridView

CollapsingToolbarLayout only working with RecyclerView but not working with ListView and GridView.

Below one is my XML file:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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="192dp"
        android:fitsSystemWindows="true"
        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="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginBottom="32dp"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/restaurant_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/gradiant"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_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.AppBarLayout>

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


    <GridView
        android:id="@+id/restaurant_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:gravity="center"
        android:numColumns="2"
        android:verticalSpacing="20dp" />

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

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

and this one is my Activity file:

        Toolbar toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbar.setTitle("Resturant Name");
        ImageView header = (ImageView) findViewById(R.id.restaurant_image);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ViewCompat.setNestedScrollingEnabled(mGrid,true);
        }

        mGrid.setAdapter(new ResturantItemsAdapter(this, images, name));//images and name is array with size 10....

Note:-Scrolling is working fine but after some of Grid View list scrolling it is getting stuck and not scrolling more, even there is more rows in Grid View. It scrolling only for the 8th item of Grid View and 9th and 10th item is not showing ...

I searched many links, there people saying it only work above and in Lollipop version. Below version have some problem.

Is it possible to run Collapse Toolbar work below the lollipop version ?

Thanks to all ....

like image 327
sushildlh Avatar asked Sep 09 '16 12:09

sushildlh


1 Answers

CoordinatorLayout works better with RecyclerView or NestedScrollView. For your requirement, you can use RecyclerView with GridLayoutManger.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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="192dp"
        android:fitsSystemWindows="true"
        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="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginBottom="32dp"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/restaurant_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/gradiant"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/anim_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.AppBarLayout>

  <android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:scrollbars="vertical" />

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

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2); recyclerView.setLayoutManager(mLayoutManager);

Here is a sample which demonstrates the usage of GridLayoutManger: http://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/

like image 169
Abhishek V Avatar answered Oct 07 '22 21:10

Abhishek V