Skip to content

Instantly share code, notes, and snippets.

@showsky
Created April 20, 2018 03:05
Show Gist options
  • Select an option

  • Save showsky/d0bf3e6eef7e14378b2033eeda59a785 to your computer and use it in GitHub Desktop.

Select an option

Save showsky/d0bf3e6eef7e14378b2033eeda59a785 to your computer and use it in GitHub Desktop.
SwiperRefreshLayout 對於水平滾動衝突
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
/**
* Created by showsky on 2018/4/18.
*/
public class CustomSwipeRefreshLayout extends SwipeRefreshLayout {
private int mTouchSlop;
private float mPrevX;
public CustomSwipeRefreshLayout(@NonNull Context context) {
super(context);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public CustomSwipeRefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPrevX = event.getX();
break;
case MotionEvent.ACTION_MOVE:
final float eventX = event.getX();
float xDiff = Math.abs(eventX - mPrevX);
if (xDiff > mTouchSlop + 60) {
return false;
}
break;
}
return super.onInterceptTouchEvent(event);
}
}
@showsky
Copy link
Copy Markdown
Author

showsky commented Apr 20, 2018

在 SwipeRefreshLayout 元件中,使用任何水平滾動元件容易造成水平和垂直滾動衝突,所以改寫 SwipeRefreshLayout 元件,增加水平滾動(Y軸) 的判定距離。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment