将在上下键滚动视图 [英] Put the Scroll View under the Up and Down Button

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

问题描述

我要显示的滚动视图下的向上和向下按钮显示在图片。而在录音向上和向下箭头滚动条应该是滚动并经过长时间preSS的上下按钮,它应该被滚动后继续,直到终点。 请告诉我如何才能实现这一点。

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.

在此先感谢。

推荐答案

执行滚动型.pageScroll() ScrollView.smoothScrollBy()按钮的点击事件。

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

使用一个Runnable来继续调用 ScrollView.scrollBy()时长preSS事件发生。我写了一个demo给你。

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;
    }

}

如果您要检测的滚动型的滚动状态来启用或禁用按钮,你必须写一个CustomView以滚动型延伸并覆盖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\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()来代替滚动型。 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.

EDIT3: pageScroll的ListView控件

pageScroll for ListView

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

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

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