ViewPager中的ScrollView:滑动不起作用 [英] ScrollView inside ViewPager: swipe not working
问题描述
我有带三个片段的ViewPager,其中一个是内部带有ScrollView的FrameLayout:
I have ViewPager with three fragments and one of them is FrameLayout with ScrollView inside:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</ScrollView>
<Button
android:id="@+id/buttonRemove"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_button_red"
android:layout_gravity="bottom"
android:layout_margin="4dp"
android:text="@string/button_remove_last_round"
android:textColor="#ffffff"
android:textSize="24sp"/>
</FrameLayout>
,如果我在按钮上滑动,它将起作用.但是,如果我在ScrollView上滑动,那是行不通的,只能向上/向下滚动
and if I swipe over button, it works. But if I swipe over the ScrollView, it doesn't work, only scroll up/down works
我试图覆盖Scrollview的OnTouchEvent:
I tried to override OnTouchEvent of Scrollview:
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
touchX = ev.getX();
touchY = ev.getY();
return super.onTouchEvent(ev);//if I change it to 'break', then only swipe works, but no scroll
case MotionEvent.ACTION_MOVE:
if(Math.abs(touchX-ev.getX())<40){
return super.onTouchEvent(ev);//Scroll works perfectly
}else{
return false;//Scroll disabled, but swipe still not working
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
touchX=0;
touchY=0;
break;
}
return false;
}
现在我可以禁用滚动,但是滑动仍然不起作用,如果X点之间的差异超过40,我会将事件传递给viewpager,但是viewpager的onTouchListener不会收到此事件;
Now I can disable scroll, but swipe still not working, if the difference between X points more than 40, I pass the event to the viewpager, but the viewpager's onTouchListener doesn't get this event;
推荐答案
好吧,我在@yedidyak的帮助下找到了解决方案.我写了我的自定义ScrollView:
Ok, I found the solution with help of @yedidyak. I wrote my custom ScrollView:
public class CustomScrollView extends ScrollView {
float touchX = 0;
float touchY = 0;
ViewPager parentPager;
public void setParentPager(ViewPager parentPager) {
this.parentPager = parentPager;
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()){
case MotionEvent.ACTION_DOWN:
touchX = ev.getX();
touchY = ev.getY();
return super.onTouchEvent(ev);
case MotionEvent.ACTION_MOVE:
if(Math.abs(touchX-ev.getX())<40){
return super.onTouchEvent(ev);
}else{
if (parentPager==null) {
return false;
} else {
return parentPager.onTouchEvent(ev);
}
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
touchX=0;
touchY=0;
break;
}
return super.onTouchEvent(ev);
}
}
然后在片段中,我将viewpager置于此视图中,并且效果很好
then in the fragment I put the viewpager to this view and it works perfectly
这篇关于ViewPager中的ScrollView:滑动不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!