我有一个包含三个片段的ViewPager,其中一个是包含ScrollView的FrameLayout:
<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上滑动,它不起作用,只有上下滚动才有效。
编辑:我尝试重写ScrollView的OnTouchEvent:
@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没有收到此事件;