您的位置:首页 > 其它

插件框架篇一之scrollbars

2016-07-23 02:10 387 查看
问题:部分手机(如三星)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_scrollbarDefaultDelayBeforeF
4000
ade,
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 needed
final 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 added
resolvePadding();
}


在initializeScrollbars方法中有scrollbarThumbHorizontal、scrollbarAlwaysDrawHorizontalTrack、scrollbarTrackVertical、scrollbarThumbVertical四个Drawable属性的赋值。

在部分手机(如三星)系统定制过Drawable的默认值,并修改过默认资源路径,导致插件环境下无法找到相应资源报错。

解决方案:

自定义scrollbars,修改scrollbarThumbHorizontal、scrollbarAlwaysDrawHorizontalTrack、scrollbarTrackVertical、scrollbarThumbVertical属性。

1、定义scrollbars的style,并自定义scrollbarThumbHorizontal、scrollbarAlwaysDrawHorizontalTrack、scrollbarTrackVertical、scrollbarThumbVertical四个属性(一个不能漏);



2、ListView添加style;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  插件