我想做一个分页的HorizontalScrollView。如果你向右移动屏幕,那么它将显示右侧的“页面”,如果您将屏幕移动到左侧,那么它将显示左侧的“页面”。
发布于 2011-03-23 19:37:58
我以前也做过这样的事情。你可以通过一个自定义的触摸监听器来实现:
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL ){
int scrollX = getScrollX();
int itemWidth = getMeasuredWidth();
int activeItem = ((scrollX + itemWidth / 2) / itemWidth);
int scrollTo = activeItem * itemWidth;
smoothScrollTo(scrollTo, 0);
return true;
} else {
return false;
}
}
});
}我认为这很不言自明。这假设页面的宽度是恒定的,并且等于整个滚动视图的宽度。
https://stackoverflow.com/questions/5404547
复制相似问题