Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

listening to scroll events horizontalscrollview android

I am trying to listen to the event when the HorizontalScrollView is scrolled. Tried this but it does not print anything.

HorizontalScrollView headerScrollView = new HorizontalScrollView(this);

    headerScrollView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Log.i("hv1",event.toString());
            Log.i("hv1","HELLO");
            return false;
        }
    });

The actual problem is, I want to scroll two HorizontalScrollView at a time..ie; both of them need to scroll simultaneously when atleast one of them scrolled. any workaround?

I used the answer below and then tried implementing this but I am not sure how I need to use the methods in the class.

TestHorizontalScrollView headerScrollView = (TestHorizontalScrollView) findViewById(R.id.headerHv); 

Is this the way I need to point to the hsv element in the layout file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may want to try creating your own custom class that extends HorizontalScrollView and overriding the onScrollChanged() function as such

public class TestHorizontalScrollView extends HorizontalScrollView {

    public TestHorizontalScrollView(Context context) {
        super(context);
    }


    @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);
    }

}

This overriden function will catch all changes to the scroll position even when the view is not being touched. This should keep your scroll views in sync.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...