1. <legend id='aUBkI'><style id='aUBkI'><dir id='aUBkI'><q id='aUBkI'></q></dir></style></legend>
      <i id='aUBkI'><tr id='aUBkI'><dt id='aUBkI'><q id='aUBkI'><span id='aUBkI'><b id='aUBkI'><form id='aUBkI'><ins id='aUBkI'></ins><ul id='aUBkI'></ul><sub id='aUBkI'></sub></form><legend id='aUBkI'></legend><bdo id='aUBkI'><pre id='aUBkI'><center id='aUBkI'></center></pre></bdo></b><th id='aUBkI'></th></span></q></dt></tr></i><div id='aUBkI'><tfoot id='aUBkI'></tfoot><dl id='aUBkI'><fieldset id='aUBkI'></fieldset></dl></div>
      • <bdo id='aUBkI'></bdo><ul id='aUBkI'></ul>

      1. <small id='aUBkI'></small><noframes id='aUBkI'>

      2. <tfoot id='aUBkI'></tfoot>

        Android CoordinatorLayout + AppbarLayout + Viewpager 总是滚动

        时间:2023-10-04

            <bdo id='ejOUq'></bdo><ul id='ejOUq'></ul>
          • <tfoot id='ejOUq'></tfoot>

            <small id='ejOUq'></small><noframes id='ejOUq'>

              • <i id='ejOUq'><tr id='ejOUq'><dt id='ejOUq'><q id='ejOUq'><span id='ejOUq'><b id='ejOUq'><form id='ejOUq'><ins id='ejOUq'></ins><ul id='ejOUq'></ul><sub id='ejOUq'></sub></form><legend id='ejOUq'></legend><bdo id='ejOUq'><pre id='ejOUq'><center id='ejOUq'></center></pre></bdo></b><th id='ejOUq'></th></span></q></dt></tr></i><div id='ejOUq'><tfoot id='ejOUq'></tfoot><dl id='ejOUq'><fieldset id='ejOUq'></fieldset></dl></div>
              • <legend id='ejOUq'><style id='ejOUq'><dir id='ejOUq'><q id='ejOUq'></q></dir></style></legend>
                    <tbody id='ejOUq'></tbody>
                  本文介绍了Android CoordinatorLayout + AppbarLayout + Viewpager 总是滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个经典的布局,上面有一个 ToolBar,下面有一个 TabLayout,还有一个 ViewPager 从 TabLayout 切换选项卡.当 ViewPager 中的内容可滚动时,ToolBar 应该滚动到视线之外,并且 TabLayout 应该跟随并在到达顶部时粘住.

                  I have a classic layout with a ToolBar on the top, a TabLayout below it, and a ViewPager switching tabs from the TabLayout. When content in the ViewPager is scrollable, the ToolBar should scroll out of sight, and the TabLayout should follow and stick when it reaches the top.

                  在我当前的代码中,这一切都很好,除了 ToolBar 始终是可滚动的,无论 ViewPager 内容的大小如何.请参阅下面的代码.关于如何解决这个问题有什么绝妙的想法吗?

                  All this is good in my current code, except, the ToolBar is always scrollable, regardless of the size of the ViewPager's content. See my code below. Any brilliant ideas on how to fix this?

                  <?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:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="vertical">
                  
                      <android.support.design.widget.AppBarLayout
                          android:id="@+id/app_bar_layout"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@color/primary"
                          android:orientation="vertical">
                  
                          <android.support.v7.widget.Toolbar
                              android:id="@+id/toolbar"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:theme="@style/AppTheme.ToolBar"
                              app:layout_scrollFlags="scroll|enterAlways" />
                  
                          <android.support.design.widget.TabLayout
                              android:id="@+id/tabs"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:background="?attr/colorPrimary"
                              android:scrollbars="horizontal"
                              app:tabIndicatorColor="@color/black_text" />
                  
                      </android.support.design.widget.AppBarLayout>
                  
                      <android.support.v4.view.ViewPager
                          android:id="@+id/tabs_activity_view_pager"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          app:layout_behavior="@string/appbar_scrolling_view_behavior" />
                  
                  </android.support.design.widget.CoordinatorLayout>
                  

                  我可以看到 viewPager 的高度与整个根视图的高度相同.这可能是有意的,因为 appbar_scrolling_view_behavior 似乎确实添加了顶部和底部偏移量.然而,它确实看起来很奇怪,因为它会导致总是滚动工具栏和标签栏.

                  I can see that the viewPager's height is the same as the height for the entire root view. This might be intentded, as the appbar_scrolling_view_behavior does seem to add a top and bottom offset. It does however seem weird, since it will result in always scrolling the toolbar and tabbar.

                  推荐答案

                  基于其他示例,我自己的代码,以及appbar_scrolling_view_behavior的(有点乱)源代码:

                  Based on other samples, my own code, and the (somewhat messy) source code of the appbar_scrolling_view_behavior:

                          public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
                              View dependency) {
                          final CoordinatorLayout.Behavior behavior =
                                  ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior();
                          if (behavior instanceof Behavior) {
                              // Offset the child so that it is below the app-bar (with any overlap)
                  
                              final int appBarOffset = ((Behavior) behavior)
                                      .getTopBottomOffsetForScrollingSibling();
                              final int expandedMax = dependency.getHeight() - mOverlayTop;
                              final int collapsedMin = parent.getHeight() - child.getHeight();
                  
                              if (mOverlayTop != 0 && dependency instanceof AppBarLayout) {
                                  // If we have an overlap top, and the dependency is an AppBarLayout, we control
                                  // the offset ourselves based on the appbar's scroll progress. This is so that
                                  // the scroll happens sequentially rather than linearly
                                  final int scrollRange = ((AppBarLayout) dependency).getTotalScrollRange();
                                  setTopAndBottomOffset(AnimationUtils.lerp(expandedMax, collapsedMin,
                                          Math.abs(appBarOffset) / (float) scrollRange));
                              } else {
                                  setTopAndBottomOffset(dependency.getHeight() - mOverlayTop + appBarOffset);
                              }
                          }
                          return false;
                      }
                  

                  我正在阅读本文以解释问题,因为这是此代码的预期行为.

                  I'm reading this in a way explaining the problem, as this is expected behavior with this code.

                  我认为我们需要编写自己的滚动行为,专门针对 RecyclerView,

                  I think we need to write our own scroll behavior, specially for the RecyclerView,

                  这篇关于Android CoordinatorLayout + AppbarLayout + Viewpager 总是滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ViewPager + SurfaceView = 导航回活动时的长时间延迟 下一篇:ViewPager 和 FragmentPagerAdapter 中 Fragments 的 Android 生命周期管

                  相关文章

                  <i id='MXwj4'><tr id='MXwj4'><dt id='MXwj4'><q id='MXwj4'><span id='MXwj4'><b id='MXwj4'><form id='MXwj4'><ins id='MXwj4'></ins><ul id='MXwj4'></ul><sub id='MXwj4'></sub></form><legend id='MXwj4'></legend><bdo id='MXwj4'><pre id='MXwj4'><center id='MXwj4'></center></pre></bdo></b><th id='MXwj4'></th></span></q></dt></tr></i><div id='MXwj4'><tfoot id='MXwj4'></tfoot><dl id='MXwj4'><fieldset id='MXwj4'></fieldset></dl></div>
                  <legend id='MXwj4'><style id='MXwj4'><dir id='MXwj4'><q id='MXwj4'></q></dir></style></legend>
                  1. <small id='MXwj4'></small><noframes id='MXwj4'>

                    • <bdo id='MXwj4'></bdo><ul id='MXwj4'></ul>
                    <tfoot id='MXwj4'></tfoot>