c# - Scrollview 监听器在 Xamarin for Android 中不起作用?

标签 c# android xamarin xamarin.android scrollview

我在 Xamarin 中使用 C# 创建一个 Android 应用程序。我创建了 ScrollView 的扩展。这是我的代码

public class EndlessScroll : ScrollView
{ 
    public EndlessScroll (Context context) : base (context)
    {

    }

    public EndlessScroll(Context context, IAttributeSet attrs) : base(context, attrs)
    {



    }

    public EndlessScroll(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {

    }



    public interface OnScrollViewListener
    {
        void onScrollChanged(EndlessScroll v, int l, int t, int oldl, int oldt);
    }

    public  OnScrollViewListener scrollViewListener;

    public void setOnScrollViewListener(OnScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }


    protected   void onScrollChanged(int l, int t, int oldl, int oldt)
    {

        base.OnScrollChanged(l, t, oldl, oldt);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
          }

    }


}

我试图将它实现到另一个类中。我没有收到任何编译错误,但是当 ScrollView 触底时它不会注册。这是我这里的代码。

public class getFromParse:Activity, EndlessScroll.OnScrollViewListener {

    LinearLayout linear; 
    Button buttonSort; 
    Typeface font; 


protected override void OnCreate (Bundle bundle)
    {
        Window.RequestFeature(WindowFeatures.NoTitle);
        base.OnCreate (bundle); 
        SetContentView (Resource.Layout.debugLog);



   EndlessScroll  scroll = FindViewById <EndlessScroll> (Resource.Id.scrollView); 
        scroll.setOnScrollViewListener (this); 

这是 ScrollView 监听器应该在到达底部时检测到的位置。

public  void onScrollChanged(EndlessScroll scrollView, int x, int y, int oldx, int oldy) {
        // We take the last son in the scrollview
        View view = (View) scrollView.GetChildAt(scrollView.ChildCount  - 1);
        int diff = (view.Bottom  - (scrollView.Height + scrollView.ScrollY));

        // if diff is zero, then the bottom has been reached
        if (diff == 0) {
            Console.WriteLine ("Scroll changed"); 
        }
    }

如果有人可以帮助我,让我知道我做错了什么,那将是一个很大的帮助。似乎我做的一切都是对的,但我可能会遗漏一些东西。

最佳答案

我发现我做错了什么。如果将来有人需要在 Xamarin 中使用 c# 编写滚动监听器,我是这样做的。

public class EndlessScroll : ScrollView
{ 
    private ScrollViewListener scrollViewListener = null; 
    public EndlessScroll (Context context) : base (context) 
    {

    }
    public EndlessScroll(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {

    }

    public EndlessScroll(Context context, IAttributeSet attrs) : base(context, attrs)
    {



    }




    public interface ScrollViewListener
    {
        void OnScrollChanged(EndlessScroll v, int l, int t, int oldl, int oldt);
    }



    public void setOnScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }


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


        base.OnScrollChanged (l, t, oldl, oldt); 
        if (scrollViewListener != null) {
            scrollViewListener.OnScrollChanged (this, l, t, oldl, oldt);
        } 
    }


}

如您所见,我忘记覆盖 OnScrollChanged。

确保将接口(interface)实现到要使用它的 Activity 中。然后将此代码放入 oncreate 中。

 EndlessScroll  scroll = FindViewById <EndlessScroll> (Resource.Id.scrollView); 


        scroll.setOnScrollViewListener (this); 

一定要加上下面的方法,这样滚动条变化的时候才能注册。

public void  OnScrollChanged(EndlessScroll scrollView, int l, int t, int oldl, int oldt) {
        // We take the last son in the scrollview
        View view = (View) scrollView.GetChildAt(scrollView.ChildCount  - 1);
        int diff = (view.Bottom  - (scrollView.Height + scrollView.ScrollY));

        // if diff is zero, then the bottom has been reached
        if (diff <= 0 && myResources.isLoading == false) {
            myResources.isLoading = true; 
           //do stuff here 
        }


    }

如您所见,只要没有内容正在加载,上面的代码就允许它检测滚动何时到达底部。

关于c# - Scrollview 监听器在 Xamarin for Android 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26539281/

相关文章:

c# - 如何将字节数组转换为通用数组?

java - 在 Android 中反转十六进制颜色

java - 如何自动访问strings.xml? (安卓)

c# - 在 Recyclerview 中更改所选项目的背景颜色

.net - 为 Xamarin Studio(Gtk#) 安装 gstreamer-sharp

c# - Android(Xamarin) 如何通过蓝牙从手环获取数据(脉冲)

c# - 是否有任何C#方式[获取]/[提供声音频率信息的库?

android - 谷歌地图 V2 android 应用程序中的空数组错误

android - 如何在首选项 header 中使用 PreferenceFragmentCompat

c# - 我如何重构这两种方法?