1

我有一个ListView捕获滑动手势以删除一行的功能。这ListView住在一个Fragment. 在手机上,这是它自己的Activity,而且效果很好。在平板电脑上,这Fragment是在 a 中ViewPager,并且ListView永远无法捕获任何滑动事件,因为它们总是转到ViewPager.

ListView在将滑动手势传递给之前,我将如何确保它们被捕获到消费ViewPager

PS我正在使用这个库进行滑动以消除我的手势ListView

4

1 回答 1

6

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

创建一个子类ViewPager并覆盖canScroll()。如果提供View的是ListView,则返回true以将触摸事件提供给ListView

例如,这是我在一些示例中使用的类,用于在 a 中托管地图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,但我会从这里开始。

于 2013-11-07T15:48:32.903 回答