【问题标题】:Android Toolbar small movements cause bouncingAndroid 工具栏小动作导致弹跳
【发布时间】:2026-01-01 00:20:03
【问题描述】:

当视图向上滚动时,我的工具栏应该被隐藏,向下滚动时它会返回。小动作会产生奇怪的效果;如果我做一个小动作,工具栏会弹跳,如果我想移动视图并且不导致弹跳,我有一个最小的空间可以做。

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/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/Friends"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.00001px"
            android:background="@android:color/transparent"
            android:visibility="invisible" />

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

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

输入示例(白色圆圈):

【问题讨论】:

  • 当我从 22.2.0 升级到 22.2.1 时,我也开始看到这一点——您使用的是哪个版本的设计支持库?
  • @ChristopherOrr 22.2.0

标签: android scroll toolbar android-recyclerview


【解决方案1】:

试试这个

mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
if(Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD || Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1){
    mListView.setOnScrollListener(new OnScrollListener(){
        private boolean flinging = false;
        private boolean reachedEnd = false;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            flinging = (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING);
            reachedEnd = false;
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if(reachedEnd && flinging && (firstVisibleItem + visibleItemCount < totalItemCount)){
                mListView.setSelection(mAdapter.getCount() - 1);
            }else if(firstVisibleItem + visibleItemCount == totalItemCount){
                reachedEnd = true;
            }else{
                reachedEnd = false;
            }

        }

    });

【讨论】: