ArteFact
ArteFact

Reputation: 537

How to make ScrollView scroll while button is pressed?

my problem is quite simple actually, I couldn't find a way to do what's mentionned in my question. I tried with AsyncTask, which didn't work at all, also with the OnFocusChange event of the button... I'm a bit desperate at now.

Hope you guys could help ! :)

AsyncTask Version :

private void creationVoid(){
    (...)
    btScrollDown.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN)
                downPressed = true;
            if(event.getAction() == MotionEvent.ACTION_UP)
                downPressed = false;
            new ScrollDownTask().execute();
            return true;
        }
    });
(...)
}


private void scrollDown(){
    btScrollUp.setVisibility(View.VISIBLE);
    scrollView.scrollTo(scrollView.getScrollX(), scrollView.getScrollY() + 15);
    if(scrollView.getScrollY() == scrollView.getChildAt(scrollView.getChildCount()-1).getBottom() - 763) {
        btScrollDown.setVisibility(View.GONE);
    }
}

private class ScrollDownTask extends AsyncTask<String, Void, String>{
    @Override
    protected String doInBackground(String... params) {return null;}

    @Override
    protected void onPostExecute(String s) {
        while(downPressed)
            scrollDown();
    }
}

OnFocusChange Version :

private void creationVoid(){
    (...)
    btScrollDown.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            while(hasFocus)
                scrollDown();
        }
    });
    (...)
}

Upvotes: 1

Views: 492

Answers (2)

Ruchira Randana
Ruchira Randana

Reputation: 4179

Here's my approach.

  • Start a timer when you press the button and cancel the timer when you lift the touch from the button
  • On the timer fired method, scroll by a certain value (y).

Fine tune the timer interval and the scrollBy value y to suite your requirement.

Upvotes: 1

Anirudh R Kashyap
Anirudh R Kashyap

Reputation: 114

When the button is pressed, you can call the smoothScrollBy or the smoothScrollTo method. You have to, however,based on your requirement calculate how much to scroll the view. Hope this helps.

https://developer.android.com/reference/android/widget/ScrollView.html#smoothScrollBy(int,%20int)

Upvotes: 1

Related Questions