插件框架篇一之scrollbars

在部分三星手机上,ListView或GridView设置scrollbars时会出现错误导致应用崩溃。原因是系统对默认Drawable资源进行了定制,插件环境中找不到相应资源。为解决此问题,可以自定义scrollbars样式,覆盖scrollbarThumbHorizontal、scrollbarAlwaysDrawHorizontalTrack、scrollbarTrackVertical、scrollbarThumbVertical属性,通过定义新的style并在ListView中应用来避免资源找不到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:部分手机(如三星)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 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;
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值