レイアウトに水平スクロール ビューがあります。これには、ボタンを含む線形レイアウト (垂直方向) が含まれます。この領域では、水平方向にスクロールできます (ほとんどの携帯電話では、一度に 1 つの画面にボタンが収まりません)。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
は idlogin
を持つボタンであり、rightmost
はid を持つボタンですlogin4