65.9K
CodeProject 正在变化。 阅读更多。
Home

Xamarin Android:ScrollView 的事件处理程序实现无尽支持

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (9投票s)

2016 年 3 月 2 日

CPOL

3分钟阅读

viewsIcon

22842

downloadIcon

467

滚动视图在 Android 布局中被广泛使用。现在,当用户滚动视图时,我们需要找到滚动视图的滚动事件。因此,本文全部是关于查找所有 Android API 版本的这些事件。

引言

滚动视图在 Android 布局中被广泛使用。如今,当用户向下滚动时,应用程序通常会隐藏标题,或者有时会在滚动时加载更多内容。因此,访问 ScrollChange 事件有多种用途。现在 Android API > 20 提供了 ScrollView 的滚动更改事件的实现,但这并不适用于所有设备。当我在发布的高潮中测试我的应用程序时,我遇到了这个问题,因为我在模拟器上测试了我的应用程序,而现有的实现工作正常。所以我们需要一个可靠的解决方案。让我们看看我们将如何做到这一点。

背景

本文的受众可以是 Xamarin/Android 开发的初学者。本文将包括一个非常简单的应用程序,包含一个布局和一个活动。对“Interfaces”有一点了解将是受众的加分项。我希望您已经设置了 Xamarin 开发环境。让我们从添加一个空白的 Android 应用程序开始。

Using the Code

请仔细阅读以下步骤,添加一个新的 xamrin 项目,或者只需按照步骤在您现有的项目中进行更改。

步骤 1:添加项目

添加具有以下文件的新项目

正如您所看到的,我添加了一个空白项目,只有一个布局,即我们的 Main.axml 布局。

步骤 2:布局

让我们向视图中添加一些内容,以便我们至少有一些内容使我们的屏幕可滚动。所以我添加了以下布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout_ScrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:textColor="#000000"
            android:id="@+id/textview1"
            android:gravity="center"
            android:layout_width="fill_parent"
            android:layout_height="500dp"
            android:background="#FFFFFF"
            android:text="1" />
        <TextView
            android:textColor="#000000"
            android:id="@+id/textview2"
            android:gravity="center"
            android:layout_width="fill_parent"
            android:layout_height="500dp"
            android:background="#C6C6C6"
            android:text="2" />
    </LinearLayout>
</ScrollView>

关于视图

正如您在上面看到的,我首先添加了 2 个 TextView,只是为了添加一些内容,我给它们的高度为“500dp”,即设置 android:layout_height="500dp"

现在,我的内容的高度大于我的屏幕分辨率高度。因此,如果我们添加一个 ScrollView,我们将有一个具有可滚动内容的窗口。所以,我添加了一个父布局作为 ScrollView,并给它 ID MainLayout_ScrollView1

步骤 3:活动

我们知道,我们所有的编程部分都进入了与特定布局关联的 activity 类。所以我们有这个非常基本的 activity 类,它实现了上面的 Main.xaml 布局。让我们看看内容。

 

步骤 4:检测 ScrollView 上的滚动更改事件

现在这里是我们文章中最重要的部分,添加事件来检测页面是否滚动,现在让我们首先将以下代码添加到我们主活动文件的 OnCreate 方法中。

 // Get the ScrollView and add event listner to it
 ScrollView MainLayout_ScrollView1 = (ScrollView)FindViewById(Resource.Id.MainLayout_ScrollView1);
 MainLayout_ScrollView1.ViewTreeObserver.AddOnScrollChangedListener(this);

正如您所看到的,我已将滚动更改侦听器添加到 scrollview 的树中,而不是 scrollview 本身。

但是这会抛出一个错误,因为这只是一个我们调用的 abstract 方法,仍然需要实现。

实现 TreeViewScrollChangeListener 的接口

让我们将类 ViewTreeObserver.IOnScrollChangedListener 继承到我们的主活动类,以便我们可以实现此接口的 OnScrollChange 方法。

    [Activity(Label = "Scroll View Events", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity, ViewTreeObserver.IOnScrollChangedListener

接口的定义

这个接口有的方法如下。您只需点击上面的 IScrollChangedListener 文本后按 F12 即可看到它。

[Register("android/view/ViewTreeObserver$OnScrollChangedListener", "", 
"Android.Views.ViewTreeObserver/IOnScrollChangedListenerInvoker")]
public interface IOnScrollChangedListener : IJavaObject, IDisposable
{
     [Register("onScrollChanged", "()V", 
	"GetOnScrollChangedHandler:Android.Views.ViewTreeObserver/IOnScrollChangedListenerInvoker, 
	Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")]
            void OnScrollChanged();
}

现在,我们只需要实现这个方法并编写代码来检测滚动位置。所以,只需添加以下代码。

public void OnScrollChanged()
{
   ScrollView MainLayout_ScrollView1 = 
   ((ScrollView)FindViewById(Resource.Id.MainLayout_ScrollView1));
   double scrollingSpace = 
   MainLayout_ScrollView1.GetChildAt(0).Height - MainLayout_ScrollView1.Height;

    if (scrollingSpace <= MainLayout_ScrollView1.ScrollY) // Touched bottom
    {
       // Do the things you want to do
       Toast.MakeText(this, "You have reached to the bottom!", ToastLength.Short);
    }
    else
    {
       //Do the load more like things
    }
}

步骤 5:构建并运行

只需构建应用程序并运行它。您将看到输出屏幕。当您滚动它时,到达屏幕底部时,它将显示一条 toast 消息。

Output

关注点

我们可以通过对 TreeViewObserver 的一个小技巧找到页面上的滚动事件,并且可以实现一个对用户更具交互性并且具有更好性能的应用程序。

© . All rights reserved.