Tiago A.
Tiago A.

Reputation: 2586

achartengine with panning inside a viewpager

I have an achartengine GraphicalView chart inside a android.support.v4.view.ViewPager.

I want my chart to pan when the user drags her finger on it, but now the event is being caught by the ViewPager after a small drag movement.

Preferably, I would like to be able to let the user pan to the end of the chart and then let the ViewPager switch pages. As an alternative, I would like to at least stop the ViewPager from catching the drag movement inside the chart.

Any ideas?

Upvotes: 3

Views: 813

Answers (2)

Tiago A.
Tiago A.

Reputation: 2586

Here goes my solution.

It allows the user to drag the graph around while there is data in it. After that, the drag events are caught by the ViewPager.

Like I said, the key is this solution is the requestDisallowInterceptTouchEvent function.

public class ParameterGraphicalView extends org.achartengine.GraphicalView {

// stores the data model size
private int mDataSize = 0;
// stores the first X position in the dataset
private long mDataStartX = 0;
// stores the last X position in the dataset
private long mDataEndX = 0;

// the ViewPager
private ViewParent mViewPager;
//(...)
@Override
public boolean onTouchEvent(MotionEvent event) {

    // save the position of the first touch so we can determine whether the user is dragging
    // left or right
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mFirstTouchX = event.getX();
    }

    // when mViewPager.requestDisallowInterceptTouchEvent(true), the viewpager does not
    // intercept the events, and the drag events (pan, pinch) are caught by the GraphicalView

    // we want to keep the ViewPager from intercepting the event if:
    // 1- there are 2 or more touches, i.e. the pinch gesture
    // 2- the user is dragging to the left but there is no data to show to the right
    // 3- the user is dragging to the right but there is no data to show to the left
    if (event.getPointerCount() > 1
            || (event.getX() < mFirstTouchX && mDataSize > 0 && mRenderer.getXAxisMax() < mDataEndX)
            || (event.getX() > mFirstTouchX && mData.size() > 0 && mRenderer.getXAxisMin() > mDataStartX)) {
        mViewPager.requestDisallowInterceptTouchEvent(true);
    }
    else {
        mViewPager.requestDisallowInterceptTouchEvent(false);
    }

    return super.onTouchEvent(event);
}
}

Upvotes: 2

Arash GM
Arash GM

Reputation: 10395

I think you should Implement your own ViewPager and handle Touch event yourself.

Maybe this Link helps you : how to disable viewpager adapter on touching specific views?

Upvotes: 1

Related Questions