如何在使用horizontalscrollview滚动时实现中心锁定
我已经看到了这个http://krishnalalstha.wordpress.com/tag/horizontal-scrollview-with-auto-center-lock/,但它不是我需要的,我需要自动中心锁
我有一个包含3种不同布局的滚动视图(第1种:整个宽度,第2种:整个宽度,第3种:半屏),我也试过画廊,但我无法移除左侧和右侧的空间,
谢谢
几乎所有你想要的例子:http://www.velir.com/blog/index.php/2010/11/17/android-snapping-horizontal-scroll/
在我看来,它的解释非常好,所以你应该能够根据你的用例进行定制。
但是:常量SWIPE_THRESHOLD_VELOCITY设置得太高。 50在我的案件中工作。只需尝试一下,直到你满意为止。
编辑:或者,您可能想要使用ViewPager。它可能更容易,更适合“画廊”用例。
Java的:
horizontalScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
int horizontalWidth = v.getMeasuredWidth();
int horizontalHeight = v.getMeasuredHeight();
int centerX = v.getScrollX() + horizontalWidth / 2;
int centerY = horizontalHeight / 2;
Rect hitRect = new Rect();
for (int i = 0; i < horizontalContainer.getChildCount(); i++) {
View child = horizontalContainer.getChildAt(i);
child.getHitRect(hitRect);
if (hitRect.contains(centerX, centerY)) {
int x = (child.getLeft() - (horizontalWidth / 2)) + (child.getWidth() / 2);
horizontalScrollView.smoothScrollTo(x, 0);
break;
}
}
return true;
} else {
return false;
}
}
});
其中xml是:
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="22dp"
android:gravity="center_vertical">
<LinearLayout
android:id="@+id/horizontalContainer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
该容器可能包含儿童syubykh,任何尺寸