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
625 views
in Technique[技术] by (71.8m points)

android - Get maximum HorizontalScrollView scroll amount

I'm adding floating arrows to my HorizontalScrollView which will let the user know that there are more item's outside of the current view.

What I need is how to tell if the View has been scrolled to it's maximum. You would have thought the method getMaxScrollAmount() would give you this - it doesn't, in my code it gives me the width of the View. Go figure why. Here's my code - nice and simple:

    final ImageView leftArrow = (ImageView) toReturn.findViewById(R.id.leftArrow);
    final ImageView rightArrow = (ImageView) toReturn.findViewById(R.id.rightArrow);

    final HorizontalScrollView scrollView = (HorizontalScrollView) toReturn.findViewById(R.id.actionBarHoriztonalScroll);


    final GestureDetector gd = new GestureDetector(new SimpleOnGestureListener() {
        @Override
        public boolean onScroll(MotionEvent e1, 
                                MotionEvent e2, 
                                float distanceX, 
                                float distanceY) {
            if(scrollView.getScrollX() == 0) {
                leftArrow.setVisibility(View.GONE);
            } else {
                leftArrow.setVisibility(View.VISIBLE);
            }
            if(scrollView.getScrollX() == scrollView.getMaxScrollAmount() ||
                scrollView.getMaxScrollAmount() == 0) {
                rightArrow.setVisibility(View.GONE);
            } else {
                rightArrow.setVisibility(View.VISIBLE);
            }

            Log.v(ClientDetailsFragment.class.getSimpleName(), "max: " + scrollView.getMaxScrollAmount() +
                                                            "current: " + scrollView.getScrollX());
            return super.onScroll(e1, e2, distanceX, distanceY);
        }
    });

    scrollView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent ev) {
            gd.onTouchEvent(ev);
            return false;
        }
    });

Output from the above debugging:

10-03 14:36:16.343: VERBOSE/(20508): max: 240 current: 126
10-03 14:36:16.363: VERBOSE/(20508): max: 240 current: 127
10-03 14:36:16.386: VERBOSE/(20508): max: 240 current: 132
10-03 14:36:16.398: VERBOSE/(20508): max: 240 current: 143
10-03 14:36:16.417: VERBOSE/(20508): max: 240 current: 149
10-03 14:36:16.433: VERBOSE/(20508): max: 240 current: 152
10-03 14:36:16.449: VERBOSE/(20508): max: 240 current: 152
10-03 14:36:16.468: VERBOSE/(20508): max: 240 current: 152

(152 is the Max in this case)

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

The child of the horizontal scrollview should have the correct width. Try this

Log.e("ScrollWidth",Integer.toString(horizontalScrollview.getChildAt(0).getMeasuredWidth()-
        getWindowManager().getDefaultDisplay().getWidth()));

This should be the max scroll amount.

Put the following code in your onCreate method

ViewTreeObserver vto = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        Log.e("ScrollWidth",Integer.toString(horizontalScrollview.getChildAt(0)
                .getMeasuredWidth()-getWindowManager().getDefaultDisplay().getWidth()));

    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

57.0k users

...