avin
avin

Reputation: 469

Android: Preventing scrollview from scrolling automatically when hiding/showing views within it

I'm having a scrollview in my layout with a lot of views.

Within scrollview I'm having this layout for one particular item

<scrollview>
...
<FrameLayout>
  <View1/>
  <View2 hidden/>
</FrameLayout>
...
</scrollview>

____________  ____________
|          |  |          |
|          |  |  View 2  | 
|  View 1  |  |          |
|          |  |          |
|          |  ------------
------------  

I'm animating the views like this:

My problem is: On doing these actions, the scrollview is scrolling automatically (which is not looking good).

Probably the cause is : changing the visibility of the views.

I tried two things:

  1. Disabling vertical scrolling but that didn't help
  2. Remembering the scrollY in onAnimationStart and restoring this scroll position in onAnimationEnd - problem with this approach is, its visible clearly that the scrollview moved (although it moved really fast).

  3. This idea came to my mind, but i didn't know how to implement this: Keeping the upper views (wrt the framelayout) as it is and shifting the lower views to fill up the space, so that vertical scrollY value remains the same.

Is there a way I can implement this? If not, is there any other way i can achieve this?

Is there a way to partially redraw the scrollview?

Upvotes: 1

Views: 654

Answers (1)

Andrew
Andrew

Reputation: 37969

Try this:

yourView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

If yourView already has an onTouchListener, make it to return true in any case.

Upvotes: 2

Related Questions