質問

I set an "OnTouchListener" on a Horizontal Scroll View. I can't set a onScrollListener. Is this not possible on a horizontal scroll view?

This not works:

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    // TODO Auto-generated method stub
    Log.i("Scrolling", "X from ["+oldl+"] to ["+l+"]");
    super.onScrollChanged(l, t, oldl, oldt);
}
役に立ちましたか?

解決

You current code snippet wouldn't work as it's not telling any other object that the scrolling has changed - it's just firing the super version of the onScrollChanged(). You should get the log entries though.

To listen for scroll changes you would need to extend the ScrollView with a listener interface of your own. Something akin to this:

public class MyScrollView extends ScrollView{
    public interface MyScrollViewListener{
        public abstract void onScrollChanged(int l, int t, int oldl, int oldt);
    }

    MyScrollViewListener mMyScrollViewListener; 

    public void setMyScrollViewListener(MyScrollViewListenerlistener){
        mMyScrollViewListener = listener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        if(mMyScrollViewListener != null){
            mMyScrollViewListener.onScrollChanged(l, t, oldl, oldt);
        }
    }
}

Then you can use this scrollview in place of the one you are currently. Make sure to implement the interface MyScrollViewListener on what ever you want to listen for the scroll change and set it using the setMyScrollViewListener function.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top