Created
April 20, 2018 03:05
-
-
Save showsky/d0bf3e6eef7e14378b2033eeda59a785 to your computer and use it in GitHub Desktop.
SwiperRefreshLayout 對於水平滾動衝突
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
在 SwipeRefreshLayout 元件中,使用任何水平滾動元件容易造成水平和垂直滾動衝突,所以改寫 SwipeRefreshLayout 元件,增加水平滾動(Y軸) 的判定距離。