android – CollapsingToolbarLayout问题与GridView

前端之家收集整理的这篇文章主要介绍了android – CollapsingToolbarLayout问题与GridView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
CollapsingToolbarLayout只能使用RecyclerView,但不能使用ListView和GridView.

下面是我的XML文件:@H_502_3@

  1. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:fitsSystemWindows="true">
  6.  
  7. <android.support.design.widget.AppBarLayout
  8. android:id="@+id/appbar"
  9. android:layout_width="match_parent"
  10. android:layout_height="192dp"
  11. android:fitsSystemWindows="true"
  12. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  13.  
  14. <android.support.design.widget.CollapsingToolbarLayout
  15. android:id="@+id/collapsing_toolbar"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent"
  18. android:fitsSystemWindows="true"
  19. app:contentScrim="?attr/colorPrimary"
  20. app:expandedTitleMarginBottom="32dp"
  21. app:expandedTitleMarginEnd="64dp"
  22. app:expandedTitleMarginStart="48dp"
  23. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  24.  
  25. <ImageView
  26. android:id="@+id/restaurant_image"
  27. android:layout_width="match_parent"
  28. android:layout_height="match_parent"
  29. android:src="@drawable/gradiant"
  30. app:layout_collapseMode="parallax" />
  31.  
  32. <android.support.v7.widget.Toolbar
  33. android:id="@+id/anim_toolbar"
  34. android:layout_width="match_parent"
  35. android:layout_height="?attr/actionBarSize"
  36. app:layout_collapseMode="pin"
  37. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
  38. </android.support.design.widget.CollapsingToolbarLayout>
  39. </android.support.design.widget.AppBarLayout>
  40.  
  41. <android.support.v4.widget.NestedScrollView
  42. android:layout_width="match_parent"
  43. android:layout_height="match_parent"
  44. android:animateLayoutChanges="true"
  45. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  46. android:fillViewport="true">
  47.  
  48.  
  49. <GridView
  50. android:id="@+id/restaurant_items"
  51. android:layout_width="match_parent"
  52. android:layout_height="match_parent"
  53. android:layout_marginLeft="20dp"
  54. android:layout_marginRight="20dp"
  55. android:layout_marginTop="5dp"
  56. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  57. android:gravity="center"
  58. android:numColumns="2"
  59. android:verticalSpacing="20dp" />
  60.  
  61. </android.support.v4.widget.NestedScrollView>
  62.  
  63. </android.support.design.widget.CoordinatorLayout>

这是我的Activity文件:@H_502_3@

  1. Toolbar toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
  2. setSupportActionBar(toolbar);
  3. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  4. CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
  5. collapsingToolbar.setTitle("Resturant Name");
  6. ImageView header = (ImageView) findViewById(R.id.restaurant_image);
  7.  
  8. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  9. ViewCompat.setNestedScrollingEnabled(mGrid,true);
  10. }
  11.  
  12. mGrid.setAdapter(new ResturantItemsAdapter(this,images,name));//images and name is array with size 10....

注意: – 滚动工作正常,但是在GridView列表滚动之后,它卡住了一些,而不是滚动更多,即使在gridView中有更多的行.仅滚动第八个gridView项目,第九个和第十个项目不显示…@H_502_3@

搜索了很多链接,人们说它只在上面和在Lollipop版本中工作.以下版本有一些问题.@H_502_3@

是否可以在棒棒糖版本下运行折叠收费工具?@H_502_3@

Thanx到所有….@H_502_3@

解决方法

CoordinatorLayout使用RecyclerView或NestedScrollView更好地工作.根据您的要求,您可以使用RecyclerView与GridLayoutManger.
  1. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:fitsSystemWindows="true">
  6.  
  7. <android.support.design.widget.AppBarLayout
  8. android:id="@+id/appbar"
  9. android:layout_width="match_parent"
  10. android:layout_height="192dp"
  11. android:fitsSystemWindows="true"
  12. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  13.  
  14. <android.support.design.widget.CollapsingToolbarLayout
  15. android:id="@+id/collapsing_toolbar"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent"
  18. android:fitsSystemWindows="true"
  19. app:contentScrim="?attr/colorPrimary"
  20. app:expandedTitleMarginBottom="32dp"
  21. app:expandedTitleMarginEnd="64dp"
  22. app:expandedTitleMarginStart="48dp"
  23. app:layout_scrollFlags="scroll|exitUntilCollapsed">
  24.  
  25. <ImageView
  26. android:id="@+id/restaurant_image"
  27. android:layout_width="match_parent"
  28. android:layout_height="match_parent"
  29. android:src="@drawable/gradiant"
  30. app:layout_collapseMode="parallax" />
  31.  
  32. <android.support.v7.widget.Toolbar
  33. android:id="@+id/anim_toolbar"
  34. android:layout_width="match_parent"
  35. android:layout_height="?attr/actionBarSize"
  36. app:layout_collapseMode="pin"
  37. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
  38. </android.support.design.widget.CollapsingToolbarLayout>
  39. </android.support.design.widget.AppBarLayout>
  40.  
  41. <android.support.v7.widget.RecyclerView
  42. android:id="@+id/my_recycler_view"
  43. android:layout_width="match_parent"
  44. android:layout_height="match_parent"
  45. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  46. android:scrollbars="vertical" />
  47.  
  48. </android.support.design.widget.CoordinatorLayout>

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

这是一个示例,演示了GridLayoutManger的用法
http://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/@H_502_3@

猜你在找的Android相关文章