怎样实现viewpager与scrollview的同步滑动
展开全部
需要重写ScrollView,下面是代码
package com.zp365.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
/**
* 解决ScrollView嵌套ViewPager出现的滑动冲突问题
*/
public class ScrollView1 extends ScrollView {
private boolean canScroll;
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;
public ScrollView1(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(new YScrollDetector());
canScroll = true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(ev.getAction() == MotionEvent.ACTION_UP)
canScroll = true;
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}
class YScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(canScroll)
if (Math.abs(distanceY) >= Math.abs(distanceX))
canScroll = true;
else
canScroll = false;
return canScroll;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询