Samoji
Samoji

Reputation: 315

How to use over scroll mode in Android

Problem

I need to give some priority for scrolling events in my Activity.

I'm using aiCharts (charts library) and I need to zoom, pan, etc. on my areas. Without any ScrollViews it works fine, but, if I use the mentioned Layout, these features works bad. I think because of priority of views.

Possible solution

I tried to use setOverScrollMode(View.OVER_SCROLL_ALWAYS); on views that need to be on "top" of ScrollView and HorizontalScrollView but doesn't work properly.

Layout

 <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:id="@+id/screen_relative_layout"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >
            </RelativeLayout>        
        </HorizontalScrollView>
    </ScrollView>

All my views are added programmatically by adding to RelativeLayout.

Upvotes: 4

Views: 20318

Answers (1)

Dyna
Dyna

Reputation: 2305

Change your RelativeLayout so that the android:layout_height="wrap_content" Also do your own custom scrollview so that it intercepts the movement and nothing else:

public class VerticalScrollView extends ScrollView {
private float xDistance, yDistance, lastX, lastY;

public VerticalScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if(xDistance > yDistance)
                return false;
    }

    return super.onInterceptTouchEvent(ev);
}
}  

source

Let me know how that worked!

Upvotes: 0

Related Questions