Android Swipe Gesture表示父Activity无法拦截

时间:2018-04-27 04:47:13

标签: java android android-layout swipe gesture

我在Android滑动手势上工作,我在回收器视图中的物品上应用了滑动手势,回收者视图在片段内,然后片段在内部活动,活动有Scroll View,问题是父活动是拦截物品的滑动手势child recycler view,经过这么多努力我找到了解决方案并且这段代码工作正常,还有其他更好的解决方案吗?如果是,那么将其发布在答案中否则投票我的代码,以便我可以在我的应用程序中使用它,而不用担心失败。这是我的代码

      listingView.setOnTouchListener(new OnSwipeTouchListener(myContext)
        {


        public void onClick() {
            ViewListing();
        }
        //           @Override
        public boolean onTouch(View v, MotionEvent event) {


            switch (event.getActionMasked())
            {
                case MotionEvent.ACTION_DOWN:
                    x1 = event.getX();
                    break;
                case MotionEvent.ACTION_UP:
                    LockableScrollView.setScrollingEnabled(true);
                    break;
                case MotionEvent.ACTION_MOVE:
                    x2 = event.getX();
                    float deltaX = x2 - x1;

                    if (Math.abs(deltaX) > 20)
                    {
                        LockableScrollView.setScrollingEnabled(false);
                        // Left to Right swipe action
                        if (x2 > x1)
                        {
                            hideSidePanel();
                            LockableScrollView.setScrollingEnabled(true);

                        }

                        else
                        //MH: Right to Left Swipe
                        {

                            showSidePanel();
                            LockableScrollView.setScrollingEnabled(true);
                        }

                    }
                    break;

            }
            return super.onTouch(v, event);
        }


    });

可锁定滚动查看代码

             public class LockableScrollView extends ScrollView {

   @Override
   public void setClipChildren(boolean clipChildren) {
    clipChildren=false;
    super.setClipChildren(clipChildren);
  }

  @Override
  public void setFocusable(int focusable) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        focusable=View.NOT_FOCUSABLE;
      }
       super.setFocusable(focusable);
 }

 // true if we can scroll (not locked)
 // false if we cannot scroll (locked)
 private static boolean mScrollable = true;

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

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

   public LockableScrollView(Context context, AttributeSet attrs, int     defStyleAttr)    {
    super(context, attrs, defStyleAttr);
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  public LockableScrollView(Context context, AttributeSet attrs, int    defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

 public static void setScrollingEnabled(boolean enabled) {
    mScrollable = enabled;
  }

public static boolean isScrollable() {
    return  mScrollable;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            //MH: if we can scroll pass the event to the superclass
            if (mScrollable) return super.onTouchEvent(ev);
            //MH: only continue to handle the touch event if scrolling enabled
            return mScrollable; // mScrollable is always false at this point
        default:
            return super.onTouchEvent(ev);
    }
  }

@Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    //MH: Don't do anything with intercepted touch events if
    //MH: we are not scrollable
    if (!mScrollable) return false;
    else return super.onInterceptTouchEvent(ev);
    }

   }

0 个答案:

没有答案