问题:部分手机(如三星)ListView或GridView设置scrollbars显示时会报错奔溃。
问题分析:
ScrollView继承于View,在View的构造器中初始化scrollbars。
根据initializeScrollbars判断是否需要进行scrollbars初始化,并在initializeScrollbars函数进行初始化。
/*** <p>* Initializes the scrollbars from a given set of styled attributes. This* method should be called by subclasses that need scrollbars and when an* instance of these subclasses is created programmatically rather than* being inflated from XML. This method is automatically called when the XML* is inflated.* </p>** @param a the styled attributes set to initialize the scrollbars from*/
protected void initializeScrollbars(TypedArray a) {initScrollCache();final ScrollabilityCache scrollabilityCache = mScrollCache;if (scrollabilityCache.scrollBar == null) {scrollabilityCache.scrollBar = new ScrollBarDrawable();}final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);if (!fadeScrollbars) {scrollabilityCache.state = ScrollabilityCache.ON;}scrollabilityCache.fadeScrollBars = fadeScrollbars;scrollabilityCache.scrollBarFadeDuration = a.getInt(R.styleable.View_scrollbarFadeDuration, ViewConfiguration.getScrollBarFadeDuration());scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(R.styleable.View_scrollbarDefaultDelayBeforeFade,ViewConfiguration.getScrollDefaultDelay());scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(com.android.internal.R.styleable.View_scrollbarSize,ViewConfiguration.get(mContext).getScaledScrollBarSize());Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);if (thumb != null) {scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);}boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,false);if (alwaysDraw) {scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);}track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);if (thumb != null) {scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);}alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,false);if (alwaysDraw) {scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);}// Apply layout direction to the new Drawables if neededfinal int layoutDirection = getLayoutDirection();if (track != null) {track.setLayoutDirection(layoutDirection);}if (thumb != null) {thumb.setLayoutDirection(layoutDirection);}// Re-apply user/background padding so that scrollbar(s) get addedresolvePadding();
}
在initializeScrollbars方法中有scrollbarThumbHorizontal、scrollbarAlwaysDrawHorizontalTrack、scrollbarTrackVertical、scrollbarThumbVertical四个Drawable属性的赋值。
在部分手机(如三星)系统定制过Drawable的默认值,并修改过默认资源路径,导致插件环境下无法找到相应资源报错。
解决方案:
自定义scrollbars,修改scrollbarThumbHorizontal、scrollbarAlwaysDrawHorizontalTrack、scrollbarTrackVertical、scrollbarThumbVertical属性。
1、定义scrollbars的style,并自定义scrollbarThumbHorizontal、scrollbarAlwaysDrawHorizontalTrack、scrollbarTrackVertical、scrollbarThumbVertical四个属性(一个不能漏);
2、ListView添加style;