Android设计库AppBar滚动行为enterAlways

标签 android android-toolbar android-design-library android-collapsingtoolbarlayout android-appbarlayout

我使用的是最新版本的设计支持库 (23.1.1),当我使用带有 enterAlways 滚动标志的 CollapsingToolbarLayout 时遇到问题。基本上,当您向上滚动时, View 会出现,但如果还会在顶部留下一个空白区域。

普通 View :

Normal Image view

向下滚动然后返回(注意状态栏下方的空白): enter image description here

MainActivity.java

public class MainActivity extends AppCompatActivity {

    AppBarLayout appBar;
    View expandedView;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("");

        initViews();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    private void initViews() {
        appBar = (AppBarLayout) findViewById(R.id.appBar);
        appBar.addOnOffsetChangedListener(appBarOffsetChangedListener);

        expandedView = findViewById(R.id.expandedView);

        RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new DummyAdapter());
    }

    private AppBarLayout.OnOffsetChangedListener appBarOffsetChangedListener = new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            int maxOffset = appBar.getTotalScrollRange();
            verticalOffset = Math.abs(verticalOffset);
            if(verticalOffset > maxOffset)
                return;
            float percentage = verticalOffset / (float) maxOffset;

            if(expandedView!=null)
                expandedView.setAlpha(1 - percentage);
        }
    };
}

activity_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.media2359.fragmenttoolbarchange.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:background="@color/colorPrimaryDark"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin">

            </android.support.v7.widget.Toolbar>

            <RelativeLayout
                android:id="@+id/expandedView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:paddingLeft="@dimen/toolbar_text_margin_left"
                android:paddingTop="@dimen/toolbar_text_margin_top"
                tools:background="@color/colorPrimaryDark">

                <TextView
                    android:id="@+id/tvName"
                    style="@style/TextAppearance.AppCompat.Headline"
                    android:layout_width="@dimen/toolbar_text_width"
                    android:layout_height="wrap_content"
                    android:text="Hello" />

                <TextView
                    android:id="@+id/tvTime"
                    style="@style/TextAppearance.AppCompat.Body1"
                    android:layout_width="@dimen/toolbar_text_width"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/tvName"
                    android:layout_marginTop="7dp"
                    android:text="04 Feb, Tuesday evening" />

            </RelativeLayout>

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/item_dummy" />
</android.support.design.widget.CoordinatorLayout>

使用 enterAlwaysCollapsedenterAlways 可以避免这个问题,但我希望返回完整 View ,因为在实际应用中,展开的部分要小得多。

我注意到的另一件事是,空白的高度等于工具栏的高度。

编辑 1

我用 snap 替换了 exitUntilCollapsed 然后没有任何空白,但工具栏没有固定和滚动

编辑 2

看起来这是设计库的问题:CollapsingToolbarLayout enterAlways not supported

临时解决方法:Cheesesquare: enterAlways produces wrong layout

最佳答案

可能是因为:

enterAlways

哪个codepath/android_guides说:

enterAlways: The view will become visible when scrolling up. This flag is useful in cases when scrolling from the bottom of a list and wanting to expose the Toolbar as soon as scrolling up takes place.

也许你想试试这个:(标准方式)

app:layout_scrollFlags="scroll|exitUntilCollapsed"

老实说,在我的整个开发生涯中,我没有看到有人在 CollapsingToolbarLayout 中使用 enterAlways。特别是,使用这两个标志:

app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways"

否则,这可能是一个错误,需要 Google 的工作人员对此进行解答。

关于Android设计库AppBar滚动行为enterAlways,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35204283/

相关文章:

android - 当我的 View 滚动出可见区域时如何收听

Android,如何获取当前正在显示的 Activity 信息(在前台)?

Android v7 工具栏按钮对齐

android - 如何替换旧 API<21 中的 NestedScrollView?

android - 如何自定义 FloatingActionButton 的大小

android - 如何将支持库 snackbar 文本颜色设置为 android :textColor? 以外的其他内容

java - 请求位置的运行时权限时

android - 如何使工具栏上的操作菜单居中

android - TabLayout 上的样式文本

Android ffmpeg只是将图像文件转换为视频文件