【问题标题】:Prevent google map from triggering swipe-to-refresh防止谷歌地图触发滑动刷新
【发布时间】:2016-06-15 16:07:18
【问题描述】:

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

<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 视图,因为这会将动画移出位置。

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

【问题讨论】:

标签: android google-maps listview swipe swiperefreshlayout


【解决方案1】:

我找到了解决这个问题的方法。扩展 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;
    }
}  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2012-02-13
    • 2012-12-25
    相关资源
    最近更新 更多