到达 NestedScrollView 的末尾时显示另一个背景

Showing another background when reaching the end of the NestedScrollView

我已经在许多网站上检查过这个概念,并想在我的应用程序中实现它。这个想法很简单,有一个静态背景,例如应用程序中的颜色作为背景,当滚动到尽头时,它会在底部显示一些树。

这就像将第二个背景放在Scrollview 的底部。我试过只放置一个背景,但它显示在应用程序的底部,而不是在滚动视图中。

有没有办法做到这一点?因为我已经搜索过但没有找到任何可靠的来源。

如果我没理解错的话,实现所需行为的最简单方法是向回收商的适配器添加一个额外的 ViewType,并将此项目添加到项目列表的末尾。它将用作 Recycler View 的页脚,因此当用户滚动到底部时,他们将看到您的树或任何东西。

如果我理解正确,您可以使用此 XML 布局(或以编程方式构建相同的结构)来实现您正在寻找的内容。

您只需要在 NestedScrollView 内的 ViewGroup 底部放置一个带有可绘制对象的 ImageView 作为 "src"(因为 ScrollView 只能有一个子视图)。你会得到一个 ScrollView,里面有一个 ViewGroup,你的内容后面是你的图像。

当用户滚动到内容下方的底部时,图像将从底部显示。在下面的代码中,我向您展示了 XML 以及您可以在何处设置应用程序背景、滚动视图底部以及放置内容的位置。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_background">
    <!-- YOU CAN SET YOUR APP BACKGROUND COLOR OR DRAWABLE UP HERE -->

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <!-- YOUR CONTENT GOES HERE -->
                <TextView android:text="Your content here instead of this TextView"
                          android:layout_width="match_parent" 
                          android:layout_height="1500dp" />


                <!-- YOUR FOOTER IMAGE HERE -->
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/trees" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

如果您希望底部的图像显示在 ScrollView 内容的后面而不是下面,您需要移除 ImageView XML,而不是 将背景可绘制对象 设置为 NestedScrollView 内的 LinearLayout,并使用以下 XML 资源可绘制对象作为背景:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/trees"
    android:gravity="bottom|center_horizontal" />

此 XML 将创建一个新的可绘制对象,当设置为背景时该可绘制对象与视图底部对齐。只需将该代码保存在可绘制文件夹中名为 trees_background.xml 的 xml 文件中,然后将 LinearLayout 更改为

android:background="@drawable/trees_background.xml"

您可以通过以下方式检测您是否到达了 NestedScrollView 的底部,然后可以应用您自己的逻辑相应地更改背景。

  NestedScrollView nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);

        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                if (scrollY < oldScrollY) {
                    // Scroll UP
                } else if (scrollY > oldScrollY) {
                    //Scroll down

                } else if (scrollY == 0) {
                    // TOP SCROLL
                } else if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                    //You have reached  the bottom of nested scrollview
                    //Add you logic here to change background
                }
            }
        });

希望我答对了你的问题。