【问题标题】:How to fix footer properly which is inside CoordinatorLayout如何正确修复 CoordinatorLayout 内部的页脚
【发布时间】:2016-07-06 00:46:33
【问题描述】:

我倾向于从这里进一步阐述

Android - footer scrolls off screen when used in CoordinatorLayout

https://code.google.com/p/android/issues/detail?id=177195

我希望在滚动 RecyclerView 时隐藏 TabLayout。这就是为什么我有以下布局。

<CoordinatorLayout>
    <CollapsingToolbarLayout>
        <TabLayout>
    <ViewPager>
        <RecyclerView>
        <Footer>

对于我的情况,我有一个ViewPager,其中包含多个片段。

大部分片段,包含RecyclerView 和页脚。它们如下所示

<LinearLayout>
    <RecyclerView />
    <LinearLayout id="@+id/footer" />
</LinearLayout>

不幸的是,页脚在滚动时是可移动的,尽管我希望它是静态的。

请注意,将app:layout_behavior 放置在ViewPager 而不是RecyclerView 中很重要。如果没有,TabLayout 将不会出现。

我的实现如下

my_fragment.xml

<?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|snap">

            <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="org.yccheok.xxx.CustomScrollingViewBehavior"
        />

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

非常关键的类是org.yccheok.xxx.CustomScrollingViewBehavior,从https://stackoverflow.com/a/33396965/72437复制粘贴

org.yccheok.xxx.CustomScrollingViewBehavior 是迄今为止我能找到的最佳解决方案。但是,它远非完美,因为它会产生以下行为。

当您向上滚动一点并松开手指时,它会导致闪烁。请参考以下视频。

https://youtu.be/8RvCZJeQvS0

我想知道,根据https://stackoverflow.com/a/33396965/72437 提出的解决方案,我是否可以对CustomScrollingViewBehavior 类进行进一步改进以避免闪烁效果?

【问题讨论】:

    标签: android


    【解决方案1】:

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

    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

    【讨论】:

      猜你喜欢
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 2021-01-02
      • 1970-01-01
      • 2011-03-27
      • 2021-06-22
      相关资源
      最近更新 更多