1

嗨,我有一个简单的程序,其中包含一个简单的滚动视图,如 android 默认计算器。
当用户滚动超过偏移值时,我想滚动它以完成向右或向左。
但这行不通。
当我使用 myh.scrollBy(max, 0); 没有什么真正改变。

主要活动

public class MainActivity extends Activity {
int pos=0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final HorizontalScrollView myh= (HorizontalScrollView) findViewById(R.id.myhview);
    myh.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            int xpos= myh.getScrollX();
            int max=640;
            if((xpos>50  && pos==0) || (pos==640 && xpos>590)){
                pos=640;
                myh.scrollBy(max, 0);
                }
            if((xpos<590 && pos==640) || ( pos==0 && xpos<50) ){
                pos=0;
                myh.scrollBy(0, 0);
            }
            return false;
        }
    });
    }
}

而xml是

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:id="@+id/myhview"

     >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a01_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a00_text" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/a02_text" />
    </LinearLayout>
</HorizontalScrollView>

</LinearLayout>
4

1 回答 1

1

每当您想向左或向右滚动时,请尝试以下操作:

horizontalScrollView.post(new Runnable() {
    public void run() {
        horizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT); //Can also be FOCUS_LEFT
    }
});
于 2013-08-02T18:29:11.907 回答