HorizontalScrollView inside SwipeRefreshLayout
I solved it by extending SwipeRefreshLayout
and overriding its onInterceptTouchEvent
. Inside, I calculate if the X distance the user has wandered is bigger than the touch slop. If it does, it means the user is swiping horizontally, therefor I return false
which lets the child view (the HorizontalScrollView
in this case) to get the touch event.
public class CustomSwipeToRefresh extends SwipeRefreshLayout {
private int mTouchSlop;
private float mPrevX;
public CustomSwipeToRefresh(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPrevX = MotionEvent.obtain(event).getX();
break;
case MotionEvent.ACTION_MOVE:
final float eventX = event.getX();
float xDiff = Math.abs(eventX - mPrevX);
if (xDiff > mTouchSlop) {
return false;
}
}
return super.onInterceptTouchEvent(event);
}
}
SwipeRefreshLayout blocking horizontally scrolled RecyclerView
As per this discussion about SRL and HorizontalScrollView, I created the counterpart for the SwipeRefreshLayout
:
public class OnlyVerticalSwipeRefreshLayout extends SwipeRefreshLayout {
private int touchSlop;
private float prevX;
private boolean declined;
public OnlyVerticalSwipeRefreshLayout( Context context, AttributeSet attrs ) {
super( context, attrs );
touchSlop = ViewConfiguration.get( context ).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent( MotionEvent event ) {
switch( event.getAction() ){
case MotionEvent.ACTION_DOWN:
prevX = MotionEvent.obtain( event ).getX();
declined = false; // New action
break;
case MotionEvent.ACTION_MOVE:
final float eventX = event.getX();
float xDiff = Math.abs( eventX - prevX );
if( declined || xDiff > touchSlop ){
declined = true; // Memorize
return false;
}
break;
}
return super.onInterceptTouchEvent( event );
}
}
and usage in XML:
<com.commons.android.OnlyVerticalSwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<tags/>
</com.commons.android.OnlyVerticalSwipeRefreshLayout>
SwipeRefreshLayout + ViewPager, limit horizontal scroll only?
Solved very simply without extending anything
mPager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mLayout.setEnabled(false);
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
mLayout.setEnabled(true);
break;
}
return false;
}
});
work like a charm
Android SwipeRefreshLayout how to implement canChildScrollUp if child is not a ListView or ScrollView
thanks to @Twibit i found a way to make this work. the trick is to detect the position of the scrollview if the position is 0 make set the SwipeRefreshLayout to be enabled else disabled.
scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
@Override
public void onScrollChanged() {
int scrollY = scrollView.getScrollY();
if(scrollY == 0) swipeLayout.setEnabled(true);
else swipeLayout.setEnabled(false);
}
});
Related Topics
How to Set Dropdown Arrow in Spinner
Work Around Canvas.Clippath() That Is Not Supported in Android Any More
How to Get the Fragment Instance from the Fragmentactivity
Android Music Seekbar Is Not Working (Idle)
What Do Gc_For_Malloc, Gc_Explicit, and Other Gc_* Mean in Android Logcat
How to Set Cursor Position in Edittext
Detect Viewpager Tab Change Inside Fragment
Auto-Scrolling Textview in Android to Bring Text into View
Kotlin: Whats Does "Return@" Mean
Finish Parent and Current Activity in Android
How to Run Countdowntimer in a Service in Android
How to Handle Outofmemoryerror
Simple Mediaplayer Play Mp3 from File Path
Build Unsigned APK File with Android Studio
How to Set Text Size of Textview Dynamically for Different Screens
How to Set "Android:Layout_Below" at Runtime Programmatically