achartengine在viewpager内平移

时间:2013-10-14 11:46:29

标签: android android-viewpager achartengine

我在android.support.v4.view.ViewPager内有一个achartengine GraphicalView图表。

我希望我的图表在用户拖动手指时平移,但现在在小拖动后,ViewPager会捕捉到该事件。

最好,我希望能够让用户平移到图表的末尾,然后让ViewPager切换页面。 作为替代方案,我想至少阻止ViewPager捕捉图表中的拖动动作。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

这是我的解决方案。

它允许用户在其中有数据的同时拖动图形。之后,ViewPager会捕获拖动事件。

就像我说的,关键是这个解决方案是requestDisallowInterceptTouchEvent函数。

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);
}
}

答案 1 :(得分:1)

我认为你应该实现自己的ViewPager并亲自处理Touch事件。

也许此链接可以帮助您:how to disable viewpager adapter on touching specific views?