获取最大HorizontalScrollView滚动量 [英] Get maximum HorizontalScrollView scroll amount
问题描述
我添加的浮动箭头来我 HorizontalScrollView
,这将让用户知道有当前视图的多个项目的外面。
我需要的是如何判断的查看已滚动到它的最大值。你会想到的方法 getMaxScrollAmount()
会给你这一点 - 这不,在我的code它给我的视图的宽度。去图为什么。这是我的code - 优雅而简单:
最后ImageView的LEFTARROW =(ImageView的)toReturn.findViewById(R.id.leftArrow);
最后ImageView的向右键=(ImageView的)toReturn.findViewById(R.id.rightArrow);
最后HorizontalScrollView滚动视图=(HorizontalScrollView)toReturn.findViewById(R.id.actionBarHoriztonalScroll);
最后GestureDetector GD =新GestureDetector(新SimpleOnGestureListener(){
@覆盖
公共布尔onScroll(MotionEvent E1,
MotionEvent E2,
浮distanceX,
浮动distanceY){
如果(scrollView.getScrollX()== 0){
leftArrow.setVisibility(View.GONE);
} 其他 {
leftArrow.setVisibility(View.VISIBLE);
}
如果(scrollView.getScrollX()== scrollView.getMaxScrollAmount()||
scrollView.getMaxScrollAmount()== 0){
rightArrow.setVisibility(View.GONE);
} 其他 {
rightArrow.setVisibility(View.VISIBLE);
}
Log.v(ClientDetailsFragment.class.getSimpleName(),最大:+ scrollView.getMaxScrollAmount()+
电流:+ scrollView.getScrollX());
返回super.onScroll(E1,E2,distanceX,distanceY);
}
});
scrollView.setOnTouchListener(新OnTouchListener(){
@覆盖
公共布尔onTouch(查看为arg0,MotionEvent EV){
gd.onTouchEvent(EV);
返回false;
}
});
从上面的调试输出:
10-03 14:36:16.343:VERBOSE /(20508):最大:240电流:126
10-03 14:36:16.363:VERBOSE /(20508):最大:240电流:127
10-03 14:36:16.386:VERBOSE /(20508):最大:240电流:132
10-03 14:36:16.398:VERBOSE /(20508):最大:240电流:143
10-03 14:36:16.417:VERBOSE /(20508):最大:240电流:149
10-03 14:36:16.433:VERBOSE /(20508):最大:240电流:152
10-03 14:36:16.449:VERBOSE /(20508):最大:240电流:152
10-03 14:36:16.468:VERBOSE /(20508):最大:240电流:152
(152是Max在这种情况下)
横向滚动型的孩子应该有正确的宽度。试试这个
<$p$p><$c$c>Log.e("ScrollWidth",Integer.toString(horizontalScrollview.getChildAt(0).getMeasuredWidth()- 。getWindowManager()getDefaultDisplay()的getWidth()))。这应该是最大滚动量。
将下面的code。在您的onCreate方法
ViewTreeObserver VTO = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
@覆盖
公共无效onGlobalLayout(){
。scrollView.getViewTreeObserver()removeGlobalOnLayoutListener(本);
Log.e(ScrollWidth,Integer.toString(horizontalScrollview.getChildAt(0)
.getMeasuredWidth() - getWindowManager()getDefaultDisplay()的getWidth()))。。;
}
});
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)
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()));
}
});
这篇关于获取最大HorizontalScrollView滚动量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!