解决ScrollView嵌套ViewPager,导致ViewPager不能滑动的问题

import java.lang.reflect.Field;

import java.lang.reflect.Method;

/**

 * 解决ScrollView嵌套ViewPager,导致ViewPager不能滑动的问题

 */

public class CustomScrollView extends ScrollView {

private GestureDetector mGestureDetector;

private int Scroll_height = 0;

private int view_height = 0;

protected Field scrollView_mScroller;

private static final String TAG = "CustomScrollView";


public CustomScrollView(Context context, AttributeSet attrs) {

super(context, attrs);

mGestureDetector = new GestureDetector(context, new YScrollDetector());

setFadingEdgeLength(0);

}


@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

if (ev.getAction() == MotionEvent.ACTION_DOWN) {

stopAnim();

}

boolean ret = super.onInterceptTouchEvent(ev);

boolean ret2 = mGestureDetector.onTouchEvent(ev);

return ret && ret2;

}


class YScrollDetector extends SimpleOnGestureListener {

@Override

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

if (Math.abs(distanceY) > Math.abs(distanceX)) {

return true;

}

return false;

}

}


@Override

protected void onScrollChanged(int l, int t, int oldl, int oldt) {

boolean stop = false;

if (Scroll_height - view_height == t) {

stop = true;

}


if (t == 0 || stop == true) {

try {

if (scrollView_mScroller == null) {

scrollView_mScroller = getDeclaredField(this, "mScroller");

}


Object ob = scrollView_mScroller.get(this);

if (ob == null || !(ob instanceof Scroller)) {

return;

}

Scroller sc = (Scroller) ob;

sc.abortAnimation();


} catch (Exception e) {

Log.e(TAG, e.getMessage());

}

}

super.onScrollChanged(l, t, oldl, oldt);

}


private void stopAnim() {

try {

if (scrollView_mScroller == null) {

scrollView_mScroller = getDeclaredField(this, "mScroller");

}


Object ob = scrollView_mScroller.get(this);

if (ob == null) {

return;

}

Method method = ob.getClass().getMethod("abortAnimation");

method.invoke(ob);

} catch (Exception ex) {

Log.e(TAG, ex.getMessage());

}

}


@Override

protected int computeVerticalScrollRange() {

Scroll_height = super.computeVerticalScrollRange();

return Scroll_height;

}


@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

super.onLayout(changed, l, t, r, b);

if (changed == true) {

view_height = b - t;

}

}


@Override

public void requestChildFocus(View child, View focused) {

if (focused != null && focused instanceof WebView) {

return;

}

super.requestChildFocus(child, focused);

}


/**

* 获取一个对象隐藏的属性,并设置属性为public属性允许直接访问

* @return {@link Field} 如果无法读取,返回null;返回的Field需要使用者自己缓存,本方法不做缓存

*/

public static Field getDeclaredField(Object object, String field_name) {

Class<?> cla = object.getClass();

Field field = null;

for (; cla != Object.class; cla = cla.getSuperclass()) {

try {

field = cla.getDeclaredField(field_name);

field.setAccessible(true);

return field;

} catch (Exception e) {


}

}

return null;

}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值