捕获滑动以解除ViewPager中的listview手势

时间:2013-11-07 15:38:25

标签: android android-listview android-fragments android-viewpager swipe-gesture

我有ListView抓取滑动手势以删除行。此ListView位于Fragment。在手机上,这是它自己的Activity并且效果很好。在平板电脑上,此Fragment位于ViewPager,而ListView永远无法捕获任何滑动事件,因为它们始终转到ViewPager

如何确保ListView在将ViewPager传递到ListView之前捕获滑动手势?

P.S。 我使用this library进行滑动以解除我{{1}}

中的手势

1 个答案:

答案 0 :(得分:6)

  

如何确保ListView在传递给ViewPager之前捕获滑动手势以进行消费?

创建ViewPager的子类并覆盖canScroll()。如果提供的ViewListView,请返回true以将触摸事件发送给ListView

例如,以下是我在某些示例中使用的类,用于在ViewPager中托管地图:

/***
  Copyright (c) 2013 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.mapsv2.pager;

import android.content.Context;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.view.View;

public class MapAwarePager extends ViewPager {
  public MapAwarePager(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  protected boolean canScroll(View v, boolean checkV, int dx, int x,
                              int y) {
    if (v instanceof SurfaceView || v instanceof PagerTabStrip) {
      return(true);
    }

    return(super.canScroll(v, checkV, dx, x, y));
  }
}

现在,完全有可能让你的轻扫到解雇在ViewPager内工作,但我会从这里开始。