我想做一个分页的 HorizontalScrollView。如果将屏幕向右移动,它将显示右侧的“页面”,如果将屏幕向左移动,它将显示左侧的“页面”。
我以前做过这件事。您可以通过自定义触摸监听器执行此操作:
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;
}
}
});
}
我认为非常不言自明。这假设您的页面宽度恒定且等于整个 ScrollView 的宽度。
我是一名优秀的程序员,十分优秀!