SeekBar动态设置最大值和当前值有先后顺序

2014年12月08日 Android 15条评论 阅读20331次
SeekBar动态设置最大值和当前值有先后顺序

近期项目的APP中,有一个需要垂直SeekBar调整程度的需求。
首先继承了AbsSeekBar自己写了各VerticalSeekBar。

其次就是操作VerticalSeekBar了。

项目背景需求

项目需求中,一个画面中,多个项目需要VerticalSeekBar来调整程度,但每个项目不会同时显示这个VerticalSeekBar,根据不同条件显示不同的VerticalSeekBar的,并且每个设置项目的可调整的最大值还是不同的。

为了UI上布局上方便一些,在布局文件中,我就只添加了一个VerticalSeekBar的控件,然后到java逻辑中判断,每次VerticalSeekBar的初始默认值是多少,每个项目的最大值是多少。

出现的问题

在所有功能写完进行测试时,遇到了下面的问题。
点击功能A按钮,取出A对应的默认progress位置及A最大值,显示A对应的VerticalSeekBar
调整功能A的VerticalSeekBar的progress
点击功能B按钮,取出B对应的默认progress位置及B最大值,显示B对应的VerticalSeekBar
调整功能B的VerticalSeekBar的progress
再次点击功能A按钮,查看A对应的VerticalSeekBar,此时发生问题了,A的VerticalSeekBar的进度位置并不是上次设置的位置;但若再一次重复点击功能A按钮,此时A对应的VerticalSeekBar便有恢复回正常状态了。

查阅应用逻辑的代码后认为保存上次数值时没有错误,从而将目标转向自定义VerticalSeekBar。

自定义VerticalSeekBar的显示位置,由两个因素来决定。


  • progress:对应API中的setProgress(int)
  • max:对应API中的setMax(int)

在自定义VerticalSeekBar类中,我并没有重写这两个方法,也就是setProgress(int)和setMax(int)都是调用的父类方法。
从framework目录的下面两个文件中找到了setProgress(int)和setMax(int)的源代码
  frameworks\base\core\java\android\widget\AbsSeekBar.java
  frameworks\base\core\java\android\widget\ProgressBar.java

代码追踪

frameworks\base\core\java\android\widget\AbsSeekBar.java


@Override
    public synchronized void setMax(int max) {
        super.setMax(max);

        if ((mKeyProgressIncrement == 0) || (getMax() / mKeyProgressIncrement > 20)) {
            // It will take the user too long to change this via keys, change it
            // to something more reasonable
            setKeyProgressIncrement(Math.max(1, Math.round((float) getMax() / 20)));
        }
    }
frameworks\base\core\java\android\widget\ProgressBar.java



@android.view.RemotableViewMethod
    public synchronized void setMax(int max) {
        if (max < 0) {
            max = 0;
        }
        if (max != mMax) {
            mMax = max;
            postInvalidate();

            if (mProgress > max) {
                mProgress = max;
            }
            refreshProgress(R.id.progress, mProgress, false);
        }
    }

    @android.view.RemotableViewMethod
    public synchronized void setProgress(int progress) {
        setProgress(progress, false);
    }
    @android.view.RemotableViewMethod
    synchronized void setProgress(int progress, boolean fromUser) {
        if (mIndeterminate) {
            return;
        }

        if (progress < 0) {
            progress = 0;
        }

        if (progress > mMax) {
            progress = mMax;
        }

        if (progress != mProgress) {
            mProgress = progress;
            refreshProgress(R.id.progress, mProgress, fromUser);
        }
    }
这两段代码贴出来仅供参考,最后无论是setProgress(int)还是setMax(int),都走到了下面的refreshProgress方法。
frameworks\base\core\java\android\widget\ProgressBar.java
private synchronized void refreshProgress(int id, int progress, boolean fromUser) {
        if (mUiThreadId == Thread.currentThread().getId()) {
            doRefreshProgress(id, progress, fromUser, true);
        } else {
            if (mRefreshProgressRunnable == null) {
                mRefreshProgressRunnable = new RefreshProgressRunnable();
            }

            final RefreshData rd = RefreshData.obtain(id, progress, fromUser);
            mRefreshData.add(rd);
            if (mAttached && !mRefreshIsPosted) {
                post(mRefreshProgressRunnable);
                mRefreshIsPosted = true;
            }
        }
    }

在应用层的逻辑中,调换了setProgress(int)和setMax(int)的顺序之后,意外地发现上面说的问题竟然就解决了。
这里还没有确凿的证据来说明为什么之前先调用setProgress(int)再调用setMax(int)为什么的问题,但后来的实验结果证实了setProgress(int)和setMax(int)貌似有先后顺序的制约。
我最后成功的调用顺序是:setMax(int)--->setProgress(int)。
希望本文能对在使用自定义SeekBar时出现类似问题的读者能有些启发性的帮助。

分享本文至:

WRITTEN BY

avatar
本文标签:VerticalSeekBar
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

15 条评论

  1. <a href="http://www.betascgx.com/mackage-bags-australia-day-2017-mackaged">mackage bags australia day 2017</a> <a href="http://www.hobbyfreaks.com/tory-burch-tote-bag-sale-toryburchr">tory burch tote bag sale</a> <a href="http://www.ruoiled.com/adidas-adizero-f50-trx-herre-gr%C3%B8n-runninga">adidas adizero f50 trx herre gr酶n</a> <a href="http://www.zanascuisine.com/mackage-high-neck-leather-jacket-mackager">mackage high neck leather jacket</a> <a href="http://www.puyichinese.com/michael-kors-crossbody-studded-mini-selma-price-mke">michael kors crossbody studded mini selma price</a> <a href="http://www.philipelara.com/moncler-coat-womens-red-leggings-price-monclerg">moncler coat womens red leggings price</a>
    bibleofpeace http://www.bibleofpeace.com/

  2. <a href="http://www.getmohrfit.com/nike-air-max-90-light-gray-gr%C3%B8n-sort-scarpec">nike air max 90 light gray gr酶n sort</a> <a href="http://www.lovehosaga.com/adidas-neo-negro-trainers-schuhed">adidas neo negro trainers</a> <a href="http://www.kidminpros.com/adidas-tubular-runner-west-plata-azul-obuvf">adidas tubular runner west plata azul</a> <a href="http://www.typecomment.com/adidas-yeezy-boost-350-triple-schwarz-diamond-butya">adidas yeezy boost 350 triple schwarz diamond</a> <a href="http://www.minstrelhouse.com/nike-air-jordan-3-noir-flip-butya">nike air jordan 3 noir flip</a> <a href="http://www.barrettmark.com/jordan-retro-6-infrarosso-23-for-vendita-obuve">jordan retro 6 infrarosso 23 for vendita</a>
    annabelpatton http://www.annabelpatton.com/

  3. <a href="http://www.8eqraqzg.com/femmes-nike-internationalist-orange-violet-butyc">femmes nike internationalist orange violet</a> <a href="http://www.2ihz9gpj.com/adidas-john-wtutti-3-marrone-viola-scarpeb">adidas john wtutti 3 marrone viola</a> <a href="http://www.l4ohbay4.com/air-jordan-ultra-fly-oro-argento-kurpesf">air jordan ultra fly oro argento</a> <a href="http://www.06eexrxg.com/herren-nike-free-tr-flyknit-5.0-blau-rosa-scarpea">herren nike free tr flyknit 5.0 blau rosa</a> <a href="http://www.2wfq945g.com/nike-hyperdunk-2014-low-rosa-verde-obuvb">nike hyperdunk 2014 low rosa verde</a> <a href="http://www.7quwnxec.com/kvinders-air-jordan-retro-12-bl%C3%A5-gr%C3%A5-chaussuresd">kvinders air jordan retro 12 bl氓 gr氓</a>
    n1jvmtjv http://www.n1jvmtjv.com/

  4. <a href="http://www.knapzac.com/nike-limited-12th-fan-jersey-nfl-seattle-seahawks-strobe-youth-navy-blue-nflu">nike limited 12th fan jersey nfl seattle seahawks strobe youth navy blue</a> <a href="http://www.nipiservices.com/air-max-zero-br%C3%B8d-low-kurpesb">air max zero br酶d low</a> <a href="http://www.awningsbyben.com/air-jordan-retro-6-gituttio-nero-schuhed">air jordan retro 6 gituttio nero</a> <a href="http://www.blogtourney.com/adidas-nitrocharge-hvit-jordans-schuheg">adidas nitrocharge hvit jordans</a> <a href="http://www.buranna.com/nike-city-arenaal-iii-ni%C3%B1os-plata-azul-kurpesd">nike city arenaal iii ni帽os plata azul</a> <a href="http://www.cardilwo.com/hombres-asics-gel-noosa-tri-9-cielo-azul-obuvg">hombres asics gel noosa tri 9 cielo azul</a>
    shkcollege http://www.shkcollege.com/

  5. avatar ankaralokanta

    <a href="http://www.georgesherry.com/asics-in-esecuzione-scarpe-bianca-blu-kurpesc">asics in esecuzione scarpe bianca blu</a> <a href="http://www.jenalgroup.com/ray-ban-rb4098-keyboard-sunglassese">ray ban rb4098 keyboard</a> <a href="http://www.badacamshotel.com/limited-nike-white-mens-ibraheim-campbell-alternate-jersey-nfl-20-dallas-cowboys-throwback-nflr">limited nike white mens ibraheim campbell alternate jersey nfl 20 dallas cowboys throwback</a> <a href="http://www.compressmyjs.com/air-max-90-ultra-2.0-flyknit-black-skoa">air max 90 ultra 2.0 flyknit black</a> <a href="http://www.fetison.com/jordan-retro-4-powder-blue-2015-sneakerf">jordan retro 4 powder blue 2015</a> <a href="http://www.neffloot.com/asics-gel-noosa-tri-9-dam%C3%A4nner-laufen-schuhe-uk-online-kurpesa">asics gel noosa tri 9 dam盲nner laufen schuhe uk online</a>
    ankaralokanta http://www.ankaralokanta.com/

  6. avatar amglawgroup

    <a href="http://www.nedshare.com/air-jordan-eclipse-bl%C3%A5-hvit-kurpesb">air jordan eclipse bl氓 hvit</a> <a href="http://www.njbabyboomer.com/adidas-devils-28-damon-severson-green-salute-to-service-stitched-nhl-jersey-nflr">adidas devils 28 damon severson green salute to service stitched nhl jersey</a> <a href="http://www.chipperradio.com/nike-air-presto-mid-orange-grau-butyg">nike air presto mid orange grau</a> <a href="http://www.danaperth.com/nike-youth-rob-gronkowski-limited-red-alternate-jersey-new-england-patriots-nfl-87-vapor-untouchable-nflz">nike youth rob gronkowski limited red alternate jersey new england patriots nfl 87 vapor untouchable</a> <a href="http://www.detabouw.com/womens-nike-atlanta-falcons-21-desmond-trufant-limited-black-2016-salute-to-service-nfl-jersey-nflw">womens nike atlanta falcons 21 desmond trufant limited black 2016 salute to service nfl jersey</a> <a href="http://www.elyseprather.com/nike-zoom-pegasus-31-kvinders-guld-orange-chaussuresa">nike zoom pegasus 31 kvinders guld orange</a>
    amglawgroup http://www.amglawgroup.com/

  7. avatar googleizate

    <a href="http://www.artikhazirsin.com/adidas-zx-flux-y3-shoesa">adidas zx flux y3</a> <a href="http://www.audioalpaca.com/pittsburgh-steelers-sideline-hat-hatr">pittsburgh steelers sideline hat</a> <a href="http://www.selvasurf.com/bon-march%C3%A9-nike-archive-75-femmes-noir-trainersa">bon march茅 nike archive 75 femmes noir</a> <a href="http://www.coldditch.com/nike-huarache-ultra-black-and-red-wine-skoe">nike huarache ultra black and red wine</a> <a href="http://www.grassmannword.com/black-mens-nike-toki-slip-txt-skoc">black mens nike toki slip txt</a> <a href="http://www.mjchronicles.com/barato-nike-roshe-yeezy-boost-hombres-plata-shoesc">barato nike roshe yeezy boost hombres plata</a>
    googleizate http://www.googleizate.com/

  8. avatar f509ceic

    <a href="http://www.lwjqi578.com/air-jordan-11-low-gs-bl%C3%A5-moon-knight-chaussuresc">air jordan 11 low gs bl氓 moon knight</a> <a href="http://www.7tptd902.com/nike-darwin-barato-hombres-kurpesg">nike darwin barato hombres</a> <a href="http://www.iik13si3.com/adidas-x-16.1-fg-ag-negro-schuhea">adidas x 16.1 fg ag negro</a> <a href="http://www.9fzx9977.com/nike-free-grau-and-gelb-butyf">nike free grau and gelb</a> <a href="http://www.g47lscmx.com/nike-free-3.0-v4-dam%C3%A6nd-lila-lyser%C3%B8d-kurpesd">nike free 3.0 v4 dam忙nd lila lyser酶d</a> <a href="http://www.7ro66zgi.com/nike-mercurialx-finale-gull-hvit-butya">nike mercurialx finale gull hvit</a>
    f509ceic http://www.f509ceic.com/

  9. avatar bonairecliffs

    <a href="http://www.faithboxwomen.com/limited-nike-camo-womens-reshad-jones-jersey-nfl-20-miami-dolphins-2018-salute-to-service-nflx">limited nike camo womens reshad jones jersey nfl 20 miami dolphins 2018 salute to service</a> <a href="http://www.jellyville.com/kyrie-1-all-svart-movie-schuheg">kyrie 1 all svart movie</a> <a href="http://www.juraamei.com/adidas-zx-flux-herre-salg-parts-scarpef">adidas zx flux herre salg parts</a> <a href="http://www.mesassy.com/adidas-nitrocharge-gelb-diamond-kurpesf">adidas nitrocharge gelb diamond</a> <a href="http://www.rawnessoflife.com/air-jordan-son-of-mars-oro-rojo-schuheg">air jordan son of mars oro rojo</a> <a href="http://www.shkcollege.com/nike-kd-8-barn-gul-r%C3%B8d-schuhec">nike kd 8 barn gul r酶d</a>
    bonairecliffs http://www.bonairecliffs.com/

  10. avatar eklogida

    <a href="http://www.comsoftdesign.com/air-jordan-retro-29-svart-hvit-kurpesd">air jordan retro 29 svart hvit</a> <a href="http://www.eadscs.com/air-yeezy-ii-2-sp-max-90-hvit-schuhec">air yeezy ii 2 sp max 90 hvit</a> <a href="http://www.exechars.com/femminile-nike-free-run-5.0-2014-butyg">femminile nike free run 5.0 2014</a> <a href="http://www.finkoin.com/nike-air-force-1-storm-azul-blanco-vivid-naranja-scarpec">nike air force 1 storm azul blanco vivid naranja</a> <a href="http://www.alyanskapi.com/nike-mercurialx-proximo-street-indoor-hvid-r%C3%B8d-sort-chaussuresf">nike mercurialx proximo street indoor hvid r酶d sort</a> <a href="http://www.asupsllc.com/maschio-nike-shox-zappos-butya">maschio nike shox zappos</a>
    eklogida http://www.eklogida.com/

  11. avatar pcwolftech

    <a href="http://www.tickbits.com/nobis-abby-vs-meredith-nobisr">nobis abby vs meredith</a> <a href="http://www.jmgtours.com/air-jordan-retro-28-mint-green-white-skod">air jordan retro 28 mint green white</a> <a href="http://www.gregertopsoil.com/hermes-orange-cashmere-scarf-scarfr">hermes orange cashmere scarf</a> <a href="http://www.nedshare.com/toronto-blue-jays-hats-new-era-50-hatc">toronto blue jays hats new era 50</a> <a href="http://www.itmontwitter.com/nike-hypervenom-phinish-orange-skox">nike hypervenom phinish orange</a> <a href="http://www.itsanomadlife.com/air-jordan-retro-13-kvinders-s%C3%B8lv-shoesb">air jordan retro 13 kvinders s酶lv</a>
    pcwolftech http://www.pcwolftech.com/

  12. avatar relojesorion

    <a href="http://www.moviezluv.com/bon-march%C3%A9-nike-free-5.0-2015-hommes-orange-runningd">bon march茅 nike free 5.0 2015 hommes orange</a> <a href="http://www.trulsbakken.com/reebok-ventilator-vintage-3m-sand-foota">reebok ventilator vintage 3m sand</a> <a href="http://www.dentaleden.com/womens-tan-ugg-boots-uggsr">womens tan ugg boots</a> <a href="http://www.zovailangnews.com/a-buon-mercato-nike-kobe-7-femminile-nero-footf">a buon mercato nike kobe 7 femminile nero</a> <a href="http://www.charlesklamut.com/north-face-soft-shell-fleece-northfacer">north face soft shell fleece</a> <a href="http://www.ncctifton.com/the-north-face-denali-blanket-northfacer">the north face denali blanket</a>
    [url=http://www.relojesorion.com/]relojesorion[/url]

  13. avatar kuaforylmz

    <a href="http://www.sixseconddiet.com/new-michael-kors-crossbody-mkr">new michael kors crossbody</a> <a href="http://www.responsibleny.com/nike-free-5.0-trainer-fit-4-herren-sneakere">nike free 5.0 trainer fit 4 herren</a> <a href="http://www.marcofabrin.com/new-york-yankees-7-mantle-white-with-pink-pinstripe-jersey-nflb">new york yankees 7 mantle white with pink pinstripe jersey</a> <a href="http://www.beaudoutjoel.com/womens-nike-roshe-run-hyp-all-white-shoesc">womens nike roshe run hyp all white</a> <a href="http://www.godsentmusic.com/gold-pink-mens-nike-aptare-essential-shoes-skog">gold pink mens nike aptare essential shoes</a> <a href="http://www.cesafetyprod.com/nike-kaishi-kvinders-lyser%C3%B8d-sportsf">nike kaishi kvinders lyser酶d</a>
    kuaforylmz http://www.kuaforylmz.com/

  14. avatar 地源热泵

    在茶树上就已经呈现白色,

  15. avatar 王语双

    [F4] 很专业的文章,打哈哈的路过。
    例行路过,快周末了,祝过得愉快。

欢迎留言




用户登录

sitemap