Search code examples
nativescriptnativescript-angular

How to access the native android scrollview listener from nativescript angular?


Iam working android and ios application using nativescript application,I was created the customised scrollview with extending android.widget.ScrollView in nativescript angular and i had listeners inside the scrollview.I want to set my customised scrollview listeners and its override methods to the nativescript angular scrollview.How can i set this android customised listeners to scrollview like setting ios delegate methods?

My Customised scrollView java class is:

import android.content.Context;

public class AndScroll extends org.nativescript.widgets.VerticalScrollView
{
    public interface OnEndScrollListener {
        public void onEndScroll(int x,int y);
    }
    private boolean mIsFling;
    private OnEndScrollListener mOnEndScrollListener;

    public AndScroll(Context context)
    {
        super(context)
    }
    @Override
    public void fling(int velocityY) {
        super.fling(velocityY);
        mIsFling = true;
    }
    @Override
    protected void onScrollChanged(int x, int y, int oldX, int oldY) {
        super.onScrollChanged(x, y, oldX, oldY);
        if (mIsFling) {
            if (Math.abs(y - oldY) < 2 || y >= getMeasuredHeight() || y == 0) {
                if (mOnEndScrollListener != null) {
                    mOnEndScrollListener.onEndScroll(x,y);
                }
                mIsFling = false;
            }
        }
    }
    public OnEndScrollListener getOnEndScrollListener() {
        return mOnEndScrollListener;
    }

    public void setOnEndScrollListener(OnEndScrollListener mOnEndScrollListener) {
        this.mOnEndScrollListener = mOnEndScrollListener;
    }


}

Iam want to access my customised class inside my FoodCourt Scroll ts class:

public createNativeView() {
    return this.orientation === "horizontal" ? new org.nativescript.widgets.HorizontalScrollView(this._context) : new org.nativescript.widgets.VerticalScrollView(this._context);
}

How to access My AndScroll java class in my foodcourt class like

new org.nativescript.widgets.VerticalScrollView

Solution

  • You should use org.nativescript.widgets.VerticalScrollView if you like to preserve the features exposed by ScrollView component while modifying it.

    If your aim is to just intercept OnScrollChangedListener then you just have to do it like it's done here in the original source code.

    But I don't think you would actually need any of these, scroll event itself gives you the x & y points constantly if that's all you are looking for within OnScrollChangedListener.