防止谷歌地图触发刷卡到刷新

时间:2016-06-15 16:07:18

标签: android google-maps listview swipe swiperefreshlayout

我有当前的布局(这里显然简化):

<SwipeRefreshLayout>

  <MapFragment/>

  <ListView/>

</SwipeRefreshLayout>

要解决向下滑动触发刷新的问题,我尝试执行以下操作:

  mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            mSwipeRefreshLayout.setEnabled(false);
        }
    });

然后在列表视图中启用:

    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
             mSwipeRefreshLayout.setEnabled(true);
        }
        // Prevents the downward swipe from triggering refresh unless the 
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            int topRowVerticalPosition = (listView == null || listView.getChildCount() == 0) ? 0 : listView.getChildAt(0).getTop();
            mSwipeRefreshLayout.setEnabled((topRowVerticalPosition >= 0));
        }
    });

但是,出于某种原因,这只适用于我在列表视图上滑动两次,这会破坏用户体验。

我不希望将地图片段移出SwipeRefreshLayout视图,因为这会将动画移出原位。

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

我找到了这个问题的解决方案。扩展 SwipeRefreshLayout 并覆盖 onInterceptTouchEvent。

public class OverviewSwipeRefreshLayout extends SwipeRefreshLayout {

    public OverviewSwipeRefreshLayout(Context context) {
        super(context);
    }

    public OverviewSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                super.onTouchEvent(ev);
                break;
            case MotionEvent.ACTION_MOVE:
                return false;
            case MotionEvent.ACTION_CANCEL:
                super.onTouchEvent(ev);
                break;
            case MotionEvent.ACTION_UP:
                return false;
            default:
                break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        return true;
    }
}