Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 236
Tiago A. Avatar asked Jan 31 '26 02:01

Tiago A.


2 Answers

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);
}
}
like image 111
Tiago A. Avatar answered Feb 01 '26 17:02

Tiago A.


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?

like image 30
Arash GM Avatar answered Feb 01 '26 17:02

Arash GM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!