java - AppBarLayout 自动完成当用户停止滚动时展开/折叠

标签 java android xml android-studio

我想在用户停止滚动时自动完成滚动到顶部/底部。像这样image . 我已经测试了 OnOffsetChangedListener 但总是有问题。 请有任何想法。

此操作没有使用 java 代码。

这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:background="?attr/backLayout"
android:animateLayoutChanges="true"
android:fitsSystemWindows="true"
>

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:fitsSystemWindows="true"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <!--app:layout_scrollFlags="scroll|enterAlwaysCollapsed" -->
    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
        app:contentScrim="?attr/colorPrimary">

        ...no toolbar

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/collLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7"
            >

            ...content
        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/nestedScroll"
    android:fitsSystemWindows="true"
    android:clipToPadding="false"
    app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        ...content
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

最佳答案

这里有一些标志来检查滚动:

int TO_UP = -1;
int TO_DOWN = +1;
boolean AutoScroll = false;

检查触摸是否用户释放并检查标志以自动滚动到顶部:

nsv_scroll.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP){

  if(AutoScroll){
    gotoTop();
    return true;
  }
}
return false;
}
});

检查用户是否滚动到顶部也能够滚动到顶部并将标志设置为 true ,否则为 false 并且没有自动滚动:

    nsv_scroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        Log.e("Scroll", scrollY + "");
        if (scrollY > 0) {
            if (nsv_scroll.canScrollVertically(TO_UP))
                AutoScroll = true;
        } else {
            AutoScroll = false;
        }
    }
});

简单地回到顶部:

void gotoTop(){
    nsv_scroll.fling(0);
    nsv_scroll.smoothScrollTo(0, 0);
}

关于java - AppBarLayout 自动完成当用户停止滚动时展开/折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58884825/

相关文章:

java - 从设备读取数据?

java - Swift 在 Kotlin(或 Java)中的结构?同一类的多个实例,改变其变量

android - 如何在 onPostExecute() 中从 doInBackground() 添加数据到 BaseAdapter?

android - singleTask 的多个 Activity

c# - close var XDocument.Load 方法/方式

javascript - 在 JavaScript 中将字符串转换为 XML 文档

java - 如何使用 XMLEventReader 解析来自 XML 的注释?

java - 更改 JRadioButton MenuItem 的选定点颜色

python - XML 数据的图形可视化

java - 使用 @ConditionalOnProperty 时没有限定类型的 bean