将滚动视图放在向上和向下按钮下

时间:2012-12-06 05:53:31

标签: android button scrollview updown

我想在“向上”和“向下”按钮下显示scrollView,如图所示。在点击向上和向下箭头后,滚动条应滚动,长按上下按钮后,应滚动继续直到结束点。 请告诉我如何实现这一点。

先谢谢。

enter image description here

2 个答案:

答案 0 :(得分:3)

在按钮的点击事件上执行ScrollView .pageScroll()ScrollView.smoothScrollBy()

当长按事件发生时,使用Runnable继续调用ScrollView.scrollBy()。我为你写了一个演示。

public class TestAndroidActivity extends Activity implements OnTouchListener,
        OnGestureListener {
    private static final String TAG = "TestAndroid";
    private Handler mHandler = new Handler();
    private ScrollView mScrollView;
    private Button mBtnUp;
    private Button mBtnDown;
    private View mCurBtn;
    private GestureDetector mDetecor;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mScrollView = (ScrollView) findViewById(R.id.scroll);
        mBtnUp = (Button) findViewById(R.id.btn_up);
        mBtnDown = (Button) findViewById(R.id.btn_down);
        mBtnUp.setOnTouchListener(this);
        mBtnDown.setOnTouchListener(this);
        mDetecor = new GestureDetector(this, this);
    }

    private long mStartTime = 0;
    private float mSpeed = 0.5f;
    private Runnable mScrollRunnable = new Runnable() {

        @Override
        public void run() {
            int dy = (int) ((SystemClock.uptimeMillis() - mStartTime) * mSpeed);
            mStartTime = SystemClock.uptimeMillis();
            int direction = getCurrentBtnDirection();
            if (direction == View.FOCUS_UP)
                dy *= -1;
            mScrollView.scrollBy(0, dy);
            mHandler.post(this);
        }
    };

    private int getCurrentBtnDirection() {
        if (mCurBtn == mBtnUp) {
            return View.FOCUS_UP;
        } else if (mCurBtn == mBtnDown) {
            return View.FOCUS_DOWN;
        } else
            return 0;
    }

    private void perfromPageScroll() {
        int direction = getCurrentBtnDirection();
        mScrollView.pageScroll(direction);
    }

    private void stopContinuousScroll() {
        mStartTime = 0;
        mHandler.removeCallbacks(mScrollRunnable);
    }

    private void startContinuousScroll() {
        mStartTime = SystemClock.uptimeMillis();
        mHandler.post(mScrollRunnable);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mCurBtn = v;
        mDetecor.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_UP) {
            stopContinuousScroll();
        }
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        perfromPageScroll();
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        startContinuousScroll();
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        return false;
    }

}

如果要检测ScrollView的滚动状态以启用或禁用按钮,则必须编写CustomView以扩展ScrollView并覆盖OnScrollChanged()方法。

编辑:添加布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="1\n\n\n\n\n\n2\n\n\n\n\n\n3\n\n\n\n\n4\n\n\n\n5\n\n"
            android:textSize="32sp" />
    </ScrollView>

    <Button
        android:id="@+id/btn_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="up" />

    <Button
        android:id="@+id/btn_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="down" />

</RelativeLayout>

EDIT2 :如果您使用的是ListView,则可以使用ListView.smoothScrollBy()替换ScrollView.scrollBy()。 ListView中没有pageScroll()方法,自己编写,这不是很难。

EDIT3: PageScroll for ListView

private void pageScroll(ListView l) {
    l.smoothScrollBy(l.getHeight(), 300);
}

答案 1 :(得分:0)

要转到顶部,您可以使用方法

ScrollView.fullScroll(ScrollView.FOCUS_UP)

对于滚动部分..基本上你可以使用函数

ScrollView.smoothScrollTo (int x, int y)

您可以按X, YgetScrollX()获取当前getScrollY()个位置,然后添加一些金额以进一步滚动。