CollapsingToolbarLayout只能使用RecyclerView,但不能使用ListView和GridView.
<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>
这是我的Activity文件:
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....
注意: – 滚动工作正常,但是在GridView列表滚动之后,它卡住了一些,而不是滚动更多,即使在gridView中有更多的行.仅滚动第八个gridView项目,第九个和第十个项目不显示…
我搜索了很多链接,人们说它只在上面和在Lollipop版本中工作.以下版本有一些问题.
是否可以在棒棒糖版本下运行折叠收费工具?
Thanx到所有….
解决方法
CoordinatorLayout使用RecyclerView或NestedScrollView更好地工作.根据您的要求,您可以使用RecyclerView与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);
这是一个示例,演示了GridLayoutManger的用法:
http://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/