Capture swipe to dismiss listview gestures in View

2020-07-30 03:07发布

I have a ListView that captures swipe gestures to delete a row. This ListView lives in a Fragment. On phones this is in it's own Activity and works great. On tablets this Fragment is in a ViewPager and the ListView is never able to capture any of the the swipe events since they always go to the ViewPager.

How would I go about making sure swipe gestures are captured by the ListView to consume before they are passed to the ViewPager?

P.S. I am using this library for my swipe to dismiss gestures in my ListView

1条回答
淡お忘
2楼-- · 2020-07-30 03:18

How would I go about making sure swipe gestures are captured by the ListView to consume before they are passed to the ViewPager?

Create a subclass of ViewPager and override canScroll(). If the supplied View is the ListView, return true to have the touch events be given to the ListView.

For example, here is a class I use in some samples for hosting maps in 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));
  }
}

Now, it is entirely possible that there may be more to getting your swipe-to-dismiss to work inside the ViewPager, but I would start here.

查看更多
登录 后发表回答