android – 在NestedScrollView中使用Viewpager折叠toolBar布局

前端之家收集整理的这篇文章主要介绍了android – 在NestedScrollView中使用Viewpager折叠toolBar布局前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在嵌套的Scrollview中使用带有NestedScrollView和Viewpager的Collapsing ToolBar Layout.

我有3个标签,这些标签有3个片段.这些片段使用RecyclerView来设置数据.

现在使用nestedScrollView和viewpager,当我滚动RecyclerView内容时,折叠效果不起作用.

我还需要放置NestedScrollView,因为我需要在Tabs上面显示一些信息.

这是我的代码

  1. <android.support.design.widget.AppBarLayout
  2. android:id="@+id/appbar"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  6. android:fitsSystemWindows="true">
  7.  
  8. <android.support.design.widget.CollapsingToolbarLayout
  9. android:id="@+id/collapsing_toolbar"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. app:layout_scrollFlags="scroll|exitUntilCollapsed"
  13. android:fitsSystemWindows="true"
  14. app:contentScrim="?attr/colorPrimary"
  15. app:expandedTitleMarginEnd="64dp"
  16. app:expandedTitleMarginStart="48dp"
  17. app:expandedTitleTextAppearance="@style/TransparentText">
  18.  
  19. <FrameLayout
  20. android:id="@+id/carouselLayout"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. app:layout_scrollFlags="scroll|exitUntilCollapsed"
  24. android:fitsSystemWindows="true"
  25. app:layout_collapseMode="parallax">
  26.  
  27. <ImageView
  28. android:id="@+id/coverImage"
  29. android:layout_width="match_parent"
  30. android:layout_height="match_parent"
  31. android:src="@drawable/example"
  32. android:scaleType="centerCrop"/>
  33.  
  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:gravity="bottom"
  37. android:orientation="vertical"
  38. android:layout_gravity="bottom"
  39. android:padding="@dimen/profile_image_margin"
  40. android:background="@drawable/gradient_bg"
  41. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  42. android:layout_height="wrap_content">
  43.  
  44. <TextView
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:layout_marginLeft="@dimen/profile_image_margin"
  48. android:textSize="@dimen/text_size_xlarge"
  49. android:textStyle="bold"
  50. android:textColor="@color/white"
  51. android:text="Title"
  52. android:id="@+id/content_title"/>
  53.  
  54. </LinearLayout>
  55.  
  56. </FrameLayout>
  57.  
  58. <android.support.v7.widget.Toolbar
  59. android:id="@+id/toolbar"
  60. android:layout_width="match_parent"
  61. android:layout_height="?attr/actionBarSize"
  62. app:theme="@style/ActionBarThemeOverlay"
  63. app:popupTheme="@style/ActionBarPopupThemeOverlay"
  64. app:layout_scrollFlags="scroll|enterAlways"
  65. android:background="@drawable/gradient_bg" />
  66.  
  67. </android.support.design.widget.CollapsingToolbarLayout>
  68. </android.support.design.widget.AppBarLayout>
  69.  
  70. <android.support.v4.widget.NestedScrollView
  71. android:layout_width="match_parent"
  72. android:layout_height="match_parent"
  73. android:layout_gravity="fill"
  74. android:isScrollContainer="true"
  75. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  76.  
  77. <LinearLayout
  78. android:layout_width="match_parent"
  79. android:layout_height="wrap_content"
  80. android:orientation="vertical">
  81.  
  82. <TextView
  83. android:layout_width="match_parent"
  84. android:layout_height="wrap_content"
  85. android:text="sdjsfksdfsd"
  86. android:textColor="@color/red"/>
  87.  
  88. <TextView
  89. android:layout_width="match_parent"
  90. android:layout_height="wrap_content"
  91. android:text="csdffsfsdfsdfsdf"
  92. android:textColor="@color/red"/>
  93.  
  94. <android.support.design.widget.TabLayout
  95. android:id="@+id/slidingTabs"
  96. android:layout_width="match_parent"
  97. app:tabMode="scrollable"
  98. app:tabGravity="fill"
  99. android:layout_height="wrap_content"/>
  100.  
  101. <android.support.v4.view.ViewPager
  102. android:id="@+id/pager"
  103. android:layout_width="match_parent"
  104. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  105. android:layout_height="300dp">
  106.  
  107. </android.support.v4.view.ViewPager>
  108. </LinearLayout>
  109. </android.support.v4.widget.NestedScrollView>

如果你有任何想法如何在android中的NestedScrollview中实现Recyclerview,请帮助我,这样我就可以使用了.

解决方法

看来这已经在这里得到了回答:
https://stackoverflow.com/a/31561790/4931825

您还可以查看上述答案中提供的此示例
https://github.com/TheLittleNaruto/SupportDesignExample/

作者创建了一个自定义的NestedScrollView类,扩展了NestedScrollView,它覆盖了onInterceptTouchEvent方法,如下所示:

  1. @Override
  2. public boolean onInterceptTouchEvent(MotionEvent ev) {
  3. final float x = ev.getX();
  4. final float y = ev.getY();
  5. switch (ev.getAction()) {
  6. case MotionEvent.ACTION_DOWN:
  7. xDistance = yDistance = 0f;
  8. lastX = ev.getX();
  9. lastY = ev.getY();
  10. break;
  11. case MotionEvent.ACTION_MOVE:
  12. final float curX = ev.getX();
  13. final float curY = ev.getY();
  14. xDistance += Math.abs(curX - lastX);
  15. yDistance += Math.abs(curY - lastY);
  16. lastX = curX;
  17. lastY = curY;
  18. if (xDistance > yDistance)
  19. return false;
  20. }
  21.  
  22. return super.onInterceptTouchEvent(ev) || ev.getPointerCount() == 2;
  23. }

和一个扩展ViewPager的自定义ViewPager类,它覆盖了它的onMeasure方法,如下所示:

  1. @Override
  2. protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
  3. super.onMeasure(widthMeasureSpec,heightMeasureSpec);
  4.  
  5. int height = 0;
  6. for (int i = 0; i < getChildCount(); i++) {
  7. View child = getChildAt(i);
  8. child.measure(widthMeasureSpec,MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
  9. int h = child.getMeasuredHeight();
  10. if (h > height) height = h;
  11. }
  12. heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY);
  13. super.onMeasure(widthMeasureSpec,heightMeasureSpec);
  14. }

最后,他在CoordinatorLayout中使用了两个:

  1. <com.thelittlenaruto.supportdesignexample.customview.MyNestedScrollView
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  5.  
  6. <LinearLayout
  7. android:id="@+id/lin"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="vertical"
  11. android:visibility="visible">
  12.  
  13. <com.thelittlenaruto.supportdesignexample.customview.WrapContentHeightViewPager
  14. android:id="@+id/viewPager"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content" />
  17.  
  18. </LinearLayout>
  19.  
  20. </com.thelittlenaruto.supportdesignexample.customview.MyNestedScrollView>

猜你在找的Android相关文章