HorizontalScrollView 初始化第一次时使用smoothScrollTo无效的解决办法

最近使用HorizontalScrollView 来封装水平滚动布局时 在初始化后第一次调用smoothScrollTo时 没有发生滚动
通过post时发现可行 在此做为笔记

post(new Runnable() {  
    @Override  
    public void run() {  
          smoothScrollTo(f, 0);  
    }  
});  

假如当HorizontalScrollView发生滚动时也将无效 要调用之前需停止滚动

public void abortAnimation() {

        Class superView = this.getClass().getSuperclass();
        try {
            Field field = superView.getDeclaredField("mScroller");
            field.setAccessible(true);
            Class OverScroller = Class.forName(field.getClass().getName());
            Method abortAnimation = OverScroller.getDeclaredMethod("abortAnimation");
            abortAnimation.invoke(OverScroller);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

猜你喜欢

转载自blog.csdn.net/lmjssjj/article/details/75477549