我有三个片段的 ViewPager,其中之一是 FrameLayout,里面有 ScrollView:
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:滑动不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!