2

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?

Tiago A.
  • 2,568
  • 1
  • 22
  • 27

2 Answers2

2

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);
}
}
Tiago A.
  • 2,568
  • 1
  • 22
  • 27
  • I had to drop this solution because it was causing an exception in the ViewPager sometimes when pinching. I think that the ViewPager was catching some weird "left-over" events. – Tiago A. Nov 27 '13 at 12:46
  • Did you find another working solution in the meantime? I myself just call `requestDisallowInterceptTouchEvent` whenever the `GraphicalView` is touched and I wanted to try to tune that solution in a way you did and then found your comment... – prom85 Mar 16 '14 at 14:45
  • 1
    No I didn't... I couldn't solve it because the exception was thrown deep in the SDK. After some time I just quit. If I recall correctly, the crashes only occurred because I was using both pan and pinch (zoom) gestures. If your graphic doesn't zoom, maybe this works. I can say though that what I wanted to implement was too confusing for the user anyway. It seemed a bit random that sometimes the graphical panned, other times the whole view panned. It is simpler to use the GraphicalView area just for one action: pan (or zoom) the graphics. – Tiago A. Mar 17 '14 at 09:47
1

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?

Community
  • 1
  • 1
Arash GM
  • 10,316
  • 6
  • 58
  • 76
  • Thanks! I upvoted because it would be a way to do it, but I wasn't happy with that solution. I am getting some nice results using requestDisallowInterceptTouchEvent. I'll post my solution when I finish. – Tiago A. Oct 14 '13 at 15:09
  • @TiagoA. : Thanks for Upvote and i will be happy to see your solution – Arash GM Oct 14 '13 at 16:12