Android 水平滚动视图的行为类似于 iPhone(分页) [英] Android horizontal scrollview behave like iPhone (paging)
问题描述
我在 HorizontalScrollView 中有一个 LinearLayout.内容只是一张图片.在滚动时,我需要实现与在 iPhone 等效的 HSW 上设置分页选项时获得的相同行为(滚动列表应在列表中的每个页面处停止,而不是继续移动).
I have a LinearLayout inside a HorizontalScrollView. The content is just a image. While scrolling, I need to achieve the same behavior you get when setting the paging option on a the iPhone equivalent of the HSW (scrolling the list should stop at every page on the list, not continue moving).
这是如何在 Android 中完成的?我应该自己实现这个功能还是有一个特定的属性要设置或 HSV 的子类要实现?
How is this done in Android? Should I implement this features by myself or there is a particular property to set or a subclass of HSV to implement?
推荐答案
所以,我的解决方案是:
So, my solution is:
- 拦截onTouch事件并计算页面是应该切换到下一个还是保持当前
- 从HorizontalScrollView继承并覆盖computeScroll方法
调用computeScroll方法来移动列表.默认情况下,我认为它已实现以一定的比率减速......由于我不想要这个动作,我只是覆盖它而不指定主体.
The method computeScroll the called to move the list. By default I suppose it's implemented to decelerate with a certain ratio... Since I don't want this motion, I just override it without specifing a body.
事件处理程序的代码是:
The code for the event handler is:
_scrollView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP)
{
float currentPosition = _scrollView.getScrollX();
float pagesCount = _horizontalBar.getChildCount();
float pageLengthInPx = _horizontalBar.getMeasuredWidth()/pagesCount;
float currentPage = currentPosition/pageLengthInPx;
Boolean isBehindHalfScreen = currentPage-(int)currentPage > 0.5;
float edgePosition = 0;
if(isBehindHalfScreen)
{
edgePosition = (int)(currentPage+1)*pageLengthInPx;
}
else
{
edgePosition = (int)currentPage*pageLengthInPx;
}
_scrollView.scrollTo((int)edgePosition, 0);
}
return false;
}
});
在我继承的 HorizontalScrollView
And in my inherited HorizontalScrollView
@Override
public void computeScroll (){
return;
}
这篇关于Android 水平滚动视图的行为类似于 iPhone(分页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!