Android 从另一个视图滚动视图

Android 从另一个视图滚动视图,android,Android,我的布局中有一个水平滚动视图。它包含一个线性布局(方向垂直),该布局又包含按钮。在这个区域中,我可以水平滚动(在大多数手机中,按钮不能一次放在一个屏幕上)。安卓给了我这种安慰。现在在这个水平滚动视图下面,有一个相对的布局现在我想要的是,当我在相对布局中水平滑动时,我希望水平滚动视图中的按钮滚动。 我试图通过重写onTouchEvent()来实现这一点。问题是,按钮会无限滚动(它们会从屏幕中消失)。我无法设定限制。我试图设定一个限制。但有些人认为它超出了限制1并停止。那我就不能滚动了。 这是我的布

我的布局中有一个水平滚动视图。它包含一个线性布局(方向垂直),该布局又包含按钮。在这个区域中,我可以水平滚动(在大多数手机中,按钮不能一次放在一个屏幕上)。安卓给了我这种安慰。现在在这个水平滚动视图下面,有一个相对的布局
现在我想要的是,当我在相对布局中水平滑动时,我希望水平滚动视图中的按钮滚动。
我试图通过重写
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; 
 }

.
.
.

这就是我尝试过的:

<?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; 
 }
@覆盖
公共布尔onTouchEvent(运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:{
currentX=(int)event.getRawX();
currentY=(int)event.getRawY();
打破
}
case MotionEvent.ACTION\u移动:{
int x2=(int)event.getRawX();
int y2=(int)event.getRawY();
如果(位置1[0]=rightconst)
{
llt.scrollBy(currentX-x2,0);
}
电流x=x2;
电流y=y2;
打破
}   
case MotionEvent.ACTION\u UP:{
打破
}
}
返回true;
}


leftconst
righconst
分别等于3和
screenwidth
。但当滚动它时,两端都会停止。然后在那之后滚动就不可能了
最左边的
是带有id的按钮
login
最右边的
是带有id的按钮
login4

,而不是
scrollBy
使用
scrollTo(x2)
,这样可以更准确地滚动。在代码中使用scrollBy很容易混淆

scrollTo
的参数可以这样计算

final float ratio = horizontalscrollView_width/relativelayout_width;
然后在onTouch

scrollTo( x2 * ratio, 0);

成功完成滚动后,您可以使用
VelocityTracker
实现fling功能。

在深入分析我自己的代码后,我发现限制使滚动永远停止(如果用户以另一种方式滚动,则不应停止)。因此,我在if语句中添加了另一个检查来检查滚动方向(通过存储上一个位置)



hscrv
是包含线性布局和按钮的水平滚动视图。

这与准确性无关。我已经编辑了问题的最后一部分。问题是,一旦到达终点,滚动就会停止,因为if语句中指定了限制。为什么需要指定限制?否则它将继续滚动,按钮将从屏幕上消失(滚动无限)。感谢您的回答。我找到了答案。请看我的答案。如果有办法的话很好。。但是试着在不使用if语句的情况下这样做。。将变得更快。。