4

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.

Thanks in Advance.

enter image description here

A K Jha
  • 89
  • 1
  • 10

2 Answers2

3

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

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

}

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.

EDIT: add the layout

<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: 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 for ListView

private void pageScroll(ListView l) {
    l.smoothScrollBy(l.getHeight(), 300);
}
faylon
  • 7,360
  • 1
  • 30
  • 28
  • 1
    I cannot believe such a detailed sample doesn't deserve any feedback. – faylon Dec 07 '12 at 04:01
  • I didn't try this, Bcz am trying to implement all those things which I asked. – A K Jha Dec 07 '12 at 05:37
  • 1
    How do you know that i did not implement all your requirements even without trying the demo. – faylon Dec 07 '12 at 09:00
  • I implement the scroll bar in listview between both up and down arrow buttons. Which is not implement in your code. – A K Jha Dec 07 '12 at 10:33
  • You said that 'show the scrollView under the Up and Down', it's just a RelativeLayout with a ScrollView and two Buttons. I thought you have already wrote the layout. – faylon Dec 07 '12 at 11:29
  • But Scrollview is related with listview – A K Jha Dec 07 '12 at 11:45
  • You described two layouts. 1. Show the scrollView under the Up and Down, this is just a RelativeLayout. 2. Show the Buttons inside the listview, but the scrollBar should be shown between the two buttons. This is really hard and a different question. The code to draw ScrolBar is in View.class and is not easy to modify. Maybe you should draw the scrollBar by yourself to solve the problem. – faylon Dec 10 '12 at 06:23
  • Okay, I just trying this. Making a Listview and show the scrollBar of that listview under the Up and Down Button. – A K Jha Dec 10 '12 at 07:36
  • If you are just trying to acieve 'under' but not 'between', what's the problem with my layout that using a RelativeLayout. – faylon Dec 10 '12 at 07:50
  • Okay, Please don't misunderstood. I want to the Listview with scroll bar which view is just as the given image...!!! – A K Jha Dec 10 '12 at 08:02
  • Orz... you totally defeat me. – faylon Dec 10 '12 at 08:17
  • Please tell me how I can write own pageScroll() method for listview? – A K Jha Jan 03 '13 at 10:29
  • pageScroll for listview is added. – faylon Jan 04 '13 at 03:36
0

For going to Top you can use the method

ScrollView.fullScroll(ScrollView.FOCUS_UP)

For scrolling portions.. basically you can use the function

ScrollView.smoothScrollTo (int x, int y)

You can get the current X, Y positions by getScrollX() and getScrollY() and then add some amount to scroll further.

Umair
  • 1,206
  • 1
  • 13
  • 28