7
我將浮動箭頭添加到我的HorizontalScrollView
,這將讓用戶知道當前視圖外有更多項目。獲取最大HorizontalScrollView滾動量
我需要的是如何判斷視圖是否滾動到最大。你可能會認爲方法getMaxScrollAmount()
會給你這個 - 它不,在我的代碼中,它給了我視圖的寬度。去計算爲什麼。這裏是我的代碼 - 優雅而簡單:從上面的調試
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;
}
});
輸出:
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是Max在這種情況下)
給出「0」爲它的寬度在onCreateView的XML已經充氣之後。即使做一個'findById'給出0的寬度 - 絕對不知道爲什麼。好主意,但! – Graeme
oncreate在視圖實際繪製在屏幕上之前被觸發。繪製佈局後,只能得到正確的寬度。我更新了我的unanswer。 – blessenm
很好的答案,解釋和方法來獲得視圖的實際寬度後,它的佈局! – Graeme