2

我在android.support.v4.view.ViewPager.

当用户在其上拖动手指时,我希望我的图表平移,但现在该事件在一个小的拖动移动后被 ViewPager 捕获。

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

有任何想法吗?

4

2 回答 2

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);
}
}
于 2013-10-15T14:32:13.647 回答
1

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

也许这个链接可以帮助你:如何在触摸特定视图时禁用 viewpager 适配器?

于 2013-10-14T12:01:37.480 回答