1

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


leftconstrightconstは、それぞれ 3 と に等しいscreenwidth。しかし、スクロールすると両端が止まります。それ以降はスクロールできません。leftmostは idloginを持つボタンであり、rightmostはid を持つボタンですlogin4

4

2 に答える 2

1

scrollByを使用する代わりにscrollTo(x2)、より正確にスクロールします。scrollBy を使用すると、コードが混乱します。

パラメータ to は次のscrollToように計算できます

final float ratio = horizontalscrollView_width/relativelayout_width;

次にonTouchで

scrollTo( x2 * ratio, 0);

VelocityTrackerスクロールが正常に完了したら、フリング機能を実装するために使用できます..

于 2012-09-22T14:40:45.343 に答える
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線形レイアウトとボタンを含む水平スクロール ビューです。

于 2012-09-22T16:21:54.517 に答える