首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当TabLayout包含额外的非回收视图组件时,如何隐藏ViewPager

当TabLayout包含额外的非回收视图组件时,如何隐藏ViewPager
EN

Stack Overflow用户
提问于 2016-03-16 16:14:52
回答 3查看 1.7K关注 0票数 0

TabLayout包含额外的非RecylerView组件时,我面临一个隐藏RecylerView的问题。

目前,我有以下布局。

在能够隐藏TabLayout之前

XML文件如下所示

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="?attr/portfolioTabIndicatorColor" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

</LinearLayout>

以下组件属于ViewPager中的三个子片段中的一个。

  1. RecylerView --一个包含白卡的
  2. LinearLayout页脚--包含“利润”和"RM-5,936.88“的页脚
  3. LinearLayout状态栏-包含“每日利润: RM-172.00”的状态栏

这个ViewPager的子片段看起来像(更多的细节可以在https://gist.github.com/yccheok/da69b317e48af132f54c上找到)

ViewPager子片段布局

代码语言:javascript
复制
<LinearLayout>
    <android.support.v7.widget.RecyclerView/>
    <LinearLayout /> <!-- Footer -->
    <LinearLayout /> <!-- Status bar -->
</LinearLayout>

目前,只有RecylerView可以滚动。

其他组件,如ToolbarTabLayoutLinearLayout Footer & LinearLayout Status Bar位置和大小都是静态的。

现在,我希望在滚动TabLayout时隐藏RecylerView。我指的是将TabLayout隐藏在内容卷轴上而不是ToolBar上

在能够隐藏TabLayout之后

代码语言:javascript
复制
<?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:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways">

            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabIndicatorColor="?attr/portfolioTabIndicatorColor" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

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

结果不是我想要的,因为"LinearLayout页脚“和"LinearLayout状态栏”正在按下

但是滚动以隐藏TabLayout的一些工作原理。请参考下面的两个截图。

有可能吗?

  1. 继续使LinearLayout页脚和LinearLayout状态栏成为ViewPager子片段布局的一部分。
  2. 使LinearLayout页脚和LinearLayout状态栏的位置保持静态。
  3. 能够在执行TabLayout滚动时隐藏RecylerView
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-03-24 06:58:53

通过以下教程,我能够实现我想要的

https://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/

http://mzgreen.github.io/2015/02/28/How-to-hideshow-Toolbar-when-list-is-scrolling%28part2%29/

关键的想法是

  1. 不要使用CoordinatorLayout
  2. TabLayoutRecyclerView放置在FrameLayout中,这样TabLayout将覆盖在RecyclerView的顶部。
  3. RecyclerView上添加顶部填充。拥有android:clipToPadding="false"也很重要。
  4. 若要在滚动期间隐藏/显示TabLayout,请将HidingScrollListener附加到RecyclerView

该解决方案的缺点是,由于顶部填充,requiresFadingEdge将不再在RecyclerView上工作。

票数 0
EN

Stack Overflow用户

发布于 2016-03-16 16:48:11

根据我的理解,要实现这一点,您必须在活动xml中定义布局,而不是在视图寻呼机中定义片段。并使用可以更改LinearLayout和StatusBarLayout数据的接口将数据从片段传递到活动,您可以为LinearLayout和StatusBarLayout容器使用android:layout_gravity="bottom"将它们静态地放置在协调器布局的底部。

票数 0
EN

Stack Overflow用户

发布于 2016-03-17 01:50:48

在不使用循环视图的情况下。尝试使用支持库中可用的嵌套滚动视图包装视图。这将启用协调器布局的滚动行为。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36041197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档