Xamarin android C# ScrollView OnScrollChanged 事件 [英] Xamarin android C# ScrollView OnScrollChanged event

查看:65
本文介绍了Xamarin android C# ScrollView OnScrollChanged 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Xamarin Android 中,我们如何扩展 ScrollView 以使用受保护的 OnScrollChanged 事件?

In Xamarin Android how can we extend the ScrollView in order to use the protected OnScrollChanged event?

具体来说,我们如何扩展 ScrollView 以允许将 EventHandler 注册到 OnScrollChanged 事件?ScrollView还有哪些方法需要在一个继承ScrollView的类中实现?

Specifically, How do we extend the ScrollView to allow an EventHandler to be registered to the OnScrollChanged event? What other methods of ScrollView need to be implemented in a class that extends ScrollView?

原因:

Android ScrollView 没有办法监听滚动事件.关于如何在原生 Android Java 中扩展 ScrollView 存在各种问题,但是,没有解决如何将其应用于 Xamarin 的问答.

The Android ScrollView does not have a way to listen for a scroll event. There have been various questions regarding how to extend ScrollView in native Android Java, however, there is not a question and answer addressing how to apply this to Xamarin.

推荐答案

为了以这种方式扩展 ScrollView 我们应该实现 3 个构造函数

In order to extend ScrollView in this way we should implement 3 constructors

public UsefulScrollView(Context context)
public UsefulScrollView(Context context, IAttributeSet attrs)
public UsefulScrollView(Context context, IAttributeSet attrs, int defStyle)

我们还需要重写 OnDraw 方法

We also need to override the OnDraw method

protected override void OnDraw(Android.Graphics.Canvas canvas)

为了实现我们可以在用户滚动时响应的事件的功能,我们需要重写 OnScrollChanged 方法.

To achieve the functionality of an event that we can respond to when the user scrolls we need to override the OnScrollChanged method.

protected override void OnScrollChanged(int l, int t, int oldl, int oldt)

允许事件侦听和处理的方法有多种,但为了与 Xamarin 保持一致,我们可以向类中添加公共 EventHandler 属性.

There are multiple ways to allow event listening and handling, but in order to be consistent with Xamarin we can add a public EventHandler property to our class.

public EventHandler<T> ScrollEventHandler { get; set; }

我们希望将值从 OnScrollChanged 传递给 EventHandler,所以让我们扩展 EventArgs

We will want to pass along the values from OnScrollChanged to the EventHandler, so let's extend EventArgs

public class UsefulScrollEventArgs : EventArgs{
    public int l { get; set; }
    public int t { get; set; }
    public int oldl { get; set; }
    public int oldt { get; set; }
}

最后,不要忘记在我们的每个构造函数中初始化我们的处理程序

Finally, don't forget to initialize our handler in each of our constructors

ScrollEventHandler = (object sender, UsefulScrollEventArgs e) => {};

把它们放在一起,它可能看起来像这样

Put it all together and it might look something like this

扩展的 EventArgs 类

Extended EventArgs class

public class UsefulScrollEventArgs : EventArgs{
    public int l { get; set; }
    public int t { get; set; }
    public int oldl { get; set; }
    public int oldt { get; set; }
}

扩展的 ScrollView 类

Extended ScrollView class

public class UsefulScrollView : ScrollView
{
    public EventHandler<UsefulScrollEventArgs> ScrollEventHandler { get; set; }
    public UsefulScrollView (Context context)
    : base(context)
    {
        ScrollEventHandler = (object sender, UsefulScrollEventArgs e) => {};
    }
    public UsefulScrollView (Context context, IAttributeSet attrs)
    : base(context, attrs) {
        ScrollEventHandler = (object sender, UsefulScrollEventArgs e) => {};

    }
    public UsefulScrollView(Context context, IAttributeSet attrs, int defStyle)
    : base(context, attrs, defStyle) {
        ScrollEventHandler = (object sender, UsefulScrollEventArgs e) => {};

    }
    protected override void OnScrollChanged(int l, int t, int oldl, int oldt)
    {
        ScrollEventHandler (this, 
                   new UsefulScrollEventArgs ()
                   {l=l,t=t,oldl=oldl,oldt=oldt});
        base.OnScrollChanged (l, t, oldl, oldt);
    }
    protected override void OnDraw(Android.Graphics.Canvas canvas)
    {

    }
}

这个问答有助于解决这个问题:滚动视图侦听器在 Xamarin for Android 中不起作用?

This Q&A was helpful in figuring this problem out: Scrollview listener is not working in Xamarin for Android?

这篇关于Xamarin android C# ScrollView OnScrollChanged 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆