我的布局中有一个水平滚动视图。它包含一个线性布局(垂直方向),后者又包含按钮。在这个区域我可以水平滚动(大多数手机中的按钮一次不能放在一个屏幕上)。 Android为我提供了这种舒适感。现在在此水平滚动视图下方,有一个相对布局。
现在我想要的是,当我在相对布局中水平滑动时,我希望水平滚动视图中的按钮滚动。
我试图通过覆盖onTouchEvent()
来实现这一点。这个问题是,按钮无限滚动(它们离开屏幕)。我无法限制。我试图限制。但有些人如何超过限制1并停止。然后我无法滚动。
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusableInTouchMode="true" >
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/llt"
>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/button1"
style="@style/ButtonText"
android:text="Groups" />
<Button
android:id="@+id/login1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/button1"
style="@style/ButtonText"
android:text="QandA"/>
<Button
android:id="@+id/login2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/button1"
style="@style/ButtonText"
android:text="Pending Requests"/>
<Button
android:id="@+id/login3"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/button1"
style="@style/ButtonText"
android:text="Settings"
></Button>
<Button
android:id="@+id/login4"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/button1"
style="@style/ButtonText"
android:text="Help"/>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
.
.
.
这就是我的尝试:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
currentX = (int) event.getRawX();
currentY = (int) event.getRawY();
break;
}
case MotionEvent.ACTION_MOVE: {
int x2 = (int) event.getRawX();
int y2 = (int) event.getRawY();
if(location1[0]<=leftconst&&(location2[0]+rightmost.getWidth())>=rightconst)
{
llt.scrollBy(currentX - x2 ,0);
}
currentX = x2;
currentY = y2;
break;
}
case MotionEvent.ACTION_UP: {
break;
}
}
return true;
}
leftconst
和rightconst
分别等于3和screenwidth
。但滚动时它会停止两端。之后滚动是不可能的。 leftmost
是标识为login
的按钮,rightmost
是标识为login4
的按钮
答案 0 :(得分:1)
而不是scrollBy
使用scrollTo(x2)
,这将更准确地滚动。使用scrollBy会让您的代码感到困惑。
scrollTo
的参数可以像这样计算
final float ratio = horizontalscrollView_width/relativelayout_width;
然后在onTouch
scrollTo( x2 * ratio, 0);
成功完成滚动后,您可以使用VelocityTracker
来实现fling功能..
答案 1 :(得分:1)
在更深入地分析我自己的代码之后,我发现限制使滚动永远停止(如果用户正在滚动另一个方向则不应该停止)。所以我在if语句中添加了另一个检查来检查滚动方向(通过存储前一个位置)。
case MotionEvent.ACTION_MOVE: {
int x2 = (int) event.getRawX();
int y2 = (int) event.getRawY();
int location1[] = new int[2];
int location2[]=new int[2];
leftmost.getLocationOnScreen(location1);
rightmost.getLocationOnScreen(location2);
Toast.LENGTH_SHORT).show();
if((x2-currentX<0||location1[0]+1<leftconst)&&((location2[0]+rightmost.getWidth())+1>rightconst||x2-currentX>0))
{
llt.scrollBy(currentX - x2 ,0);
}
currentX = x2;
currentY = y2;
break;
}
的修改
没有if语句本身的更好,更简单的解决方案。而不是滚动LinearLayout
(llt),只需滚动**HorizontalScrollView**
本身!由于HorizontalScrollView
负责处理,因此无需指定任何限制。
case MotionEvent.ACTION_MOVE: {
int x2 = (int) event.getRawX();
int y2 = (int) event.getRawY();
hscrv.scrollBy(currentX - x2 ,0);
currentX = x2;
currentY = y2;
break;
}
hscrv
是包含线性布局和按钮的水平滚动视图。