将滚动视图放在向上和向下按钮下 [英] Put the Scroll View under the Up and Down Button

查看:23
本文介绍了将滚动视图放在向上和向下按钮下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在向上和向下按钮下显示滚动视图,如图所示.并且在点击向上和向下箭头滚动条后应该滚动,在长按向上和向下按钮后它应该继续滚动到终点.请告诉我我们如何实现这一点.

I want to show the scrollView under the Up and Down button as show in picture. And after taping on up and down arrow scroll bar should be scroll and after long press on the up and down button it should be scrolled continue till the end point. Please tell me how we can Implement this.

提前致谢.

推荐答案

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

Perform ScrollView .pageScroll() or ScrollView.smoothScrollBy() on the tap event of the button.

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

Use a Runnable to continues invoke ScrollView.scrollBy() when long press event happens. I wrote a demo for you.

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() 方法.

If you want to detect the scroll state of the ScrollView to enable or disable the buttons, you must write a CustomView to extends ScrollView and override the OnScrollChanged() method.

添加布局

<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





2





3




4



5

"
            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>

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

If you are using a ListView, you can use ListView.smoothScrollBy() to replace ScrollView.scrollBy(). There is not a pageScroll() method in ListView, write it by yourself, it's not very hard.

ListView 的 pageScroll

pageScroll for ListView

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

这篇关于将滚动视图放在向上和向下按钮下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆