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

2014年12月08日 Android 13条评论 阅读25998次
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
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

13 条评论

  1. <a href="http://www.charleyscafe.com/air-max-1-lunar-infrarouge-grill-chaussuresb">air max 1 lunar infrarouge grill</a> <a href="http://www.emmuscades.com/ugg-bailey-button-37-uk-uggse">ugg bailey button 37 uk</a> <a href="http://www.esmokecanada.com/nike-foamposite-kvinders-lyser%C3%B8d-shoesd">nike foamposite kvinders lyser酶d</a> <a href="http://www.steamfestnz.com/nike-flyknit-lunar-1-m%C3%A4nner-rosa-chaussuresd">nike flyknit lunar 1 m盲nner rosa</a> <a href="http://www.toockbespoke.com/pandora-ring-for-engagement-pandorar">pandora ring for engagement</a> <a href="http://www.vindianescort.com/herre-salomo-s-lab-xt-himmelbl%C3%A5-bl%C3%A5-kurpese">herre salomo s lab xt himmelbl氓 bl氓</a>
    kmkonka http://www.kmkonka.com/

  2. <a href="http://www.phantomlites.net/kyrie-2-hombresta-verde-kitchen-schuhee">kyrie 2 hombresta verde kitchen</a> <a href="http://www.pioneerspirit.net/nike-soccers-sko-hvid-guld-butyd">nike soccers sko hvid guld</a> <a href="http://www.pizzablitzmd.net/nike-lebron-soldier-10-gr%C3%A5-s%C3%B8lv-obuvc">nike lebron soldier 10 gr氓 s酶lv</a> <a href="http://www.pleinairjourney.net/nike-mercurial-victory-v-gr%C3%B8n-scarpee">nike mercurial victory v gr酶n</a> <a href="http://www.powersellingmom.net/nike-zoom-vomero-12-marr%C3%B3n-p%C3%BArpura-obuvd">nike zoom vomero 12 marr贸n p煤rpura</a> <a href="http://www.pricespotter.net/jordan-3-gris-flip-chaussuresg">jordan 3 gris flip</a>
    bgcsc http://www.bgcsc.net/

  3. avatar duckbeachmovie

    <a href="http://www.sundialproductions.net/air-jordan-true-flight-powder-bleu-movie-scarpec">air jordan true flight powder bleu movie</a> <a href="http://www.survivewatercrisis.net/nike-femmes-shox-classic-ii-fonctionnement-chaussure-noir-schuhed">nike femmes shox classic ii fonctionnement chaussure noir</a> <a href="http://www.szczybelski.net/nike-tanjun-infant-toddler-sneaker-for-vendita-obuvf">nike tanjun infant toddler sneaker for vendita</a> <a href="http://www.tammyjdalton.net/nike-city-sandal-ii-barn-r%C3%B8d-gr%C3%A5-butya">nike city sandal ii barn r酶d gr氓</a> <a href="http://www.tastingtheacorn.net/mujeres-nike-roshe-run-metric-qs-casual-zapatos-butya">mujeres nike roshe run metric qs casual zapatos</a> <a href="http://www.technologyhandyman.net/kd-8-hombres-zapatos-obuve">kd 8 hombres zapatos</a>
    duckbeachmovie http://www.duckbeachmovie.net/

  4. avatar mipaneldecontrol

    <a href="http://www.beverlywater.net/nike-air-max-thea-beige-kvinders-kurpesa">nike air max thea beige kvinders</a> <a href="http://www.beyondthegame.net/nike-air-force-1-high-gr%C3%B8nn-r%C3%B8d-obuvb">nike air force 1 high gr酶nn r酶d</a> <a href="http://www.bigiony.net/nike-tanjun-guld-kurpesa">nike tanjun guld</a> <a href="http://www.bigreddot.net/adidas-crazylight-boost-marron-argent-butyc">adidas crazylight boost marron argent</a> <a href="http://www.biobill.net/nike-zoom-winflo-3-rosso-viola-butye">nike zoom winflo 3 rosso viola</a> <a href="http://www.bradleycountryacres.net/nike-kyrie-3-rouge-bleu-kurpesd">nike kyrie 3 rouge bleu</a>
    mipaneldecontrol http://www.mipaneldecontrol.net/

  5. avatar patinaloft

    <a href="http://www.pampaflowers.net/air-jordan-5-mettuttiic-blu-kurpesc">air jordan 5 mettuttiic blu</a> <a href="http://www.paradiseawards.net/nike-free-flyknit-4.0-v2-gelb-grau-scarpec">nike free flyknit 4.0 v2 gelb grau</a> <a href="http://www.patinaloft.net/nike-air-max-tailwind-6-oro-blanco-schuhed">nike air max tailwind 6 oro blanco</a> <a href="http://www.patricar.net/air-jordan-6-retro-low-hvit-ghost-gr%C3%B8nn-insignia-bl%C3%A5-obuvf">air jordan 6 retro low hvit ghost gr酶nn insignia bl氓</a> <a href="http://www.patrickmbutler.net/femminile-nike-air-max-tailwind-5-nero-arancia-butya">femminile nike air max tailwind 5 nero arancia</a> <a href="http://www.pauloalopes.net/nike-air-presto-mid-argento-nero-butya">nike air presto mid argento nero</a>
    patinaloft http://www.patinaloft.net/

  6. avatar sulilaw

    <a href="http://www.candacebenson.com/vibram-fivefingers-komodo-svart-obuvb">vibram fivefingers komodo svart</a> <a href="http://www.kodakid.com/nike-free-5.0-junior-fonctionnement-chaussures-rose-kurpesa">nike free 5.0 junior fonctionnement chaussures rose</a> <a href="http://www.brantfordpark.com/adidas-climacool-ride-vi-rosa-gr%C3%A5-butyc">adidas climacool ride vi rosa gr氓</a> <a href="http://www.landsmanbook.com/herren-nike-roshe-yeezy-boost-gr%C3%BCn-blau-butye">herren nike roshe yeezy boost gr眉n blau</a> <a href="http://www.moviestellar.com/nike-lebron-witness-gris-marr%C3%B3n-kurpesd">nike lebron witness gris marr贸n</a> <a href="http://www.djprofi.com/jordan-retro-6-blau-2014-schuhea">jordan retro 6 blau 2014</a>
    sulilaw http://www.sulilaw.com/

  7. avatar flygirlvi

    <a href="http://www.abartz.com/herren-salomon-speedcross-3-outdoor-laufen-sports-schuhe-butye">herren salomon speedcross 3 outdoor laufen sports schuhe</a> <a href="http://www.howatshives.com/nike-kobe-7-verde-rojo-kurpesc">nike kobe 7 verde rojo</a> <a href="http://www.codedtalk.com/nike-air-max-2017-kpu-lilla-gr%C3%B8n-butya">nike air max 2017 kpu lilla gr酶n</a> <a href="http://www.bcrailroad.com/nike-city-sandal-ii-barn-gr%C3%B8nn-brun-schuheg">nike city sandal ii barn gr酶nn brun</a> <a href="http://www.glutenfreebay.com/nike-free-3.0-v4-oransje-kvinners-plus-butyf">nike free 3.0 v4 oransje kvinners plus</a> <a href="http://www.dexterbowl.com/nike-vomero-4-for-vendita-butya">nike vomero 4 for vendita</a>
    flygirlvi http://www.flygirlvi.com/

  8. avatar simplesolaire

    <a href="http://www.imovieye.net/asics-gel-kayano-20-bleu-rouge-obuve">asics gel kayano 20 bleu rouge</a> <a href="http://www.inafj.net/nike-kobe-9-schwarz-wei%C3%9F-kurpesd">nike kobe 9 schwarz wei脽</a> <a href="http://www.infoinvest.net/ua-clutchfit-prodigy-braun-silber-schuhec">ua clutchfit prodigy braun silber</a> <a href="http://www.innerhumanism.net/nike-flyknit-racer-multicolor-acquistare-online-butyg">nike flyknit racer multicolor acquistare online</a> <a href="http://www.inspiriamedia.net/asics-gel-kayano-20-oransje-svart-scarped">asics gel kayano 20 oransje svart</a> <a href="http://www.integralcinema.net/nike-free-run-2-rosso-rosa-chaussuresf">nike free run 2 rosso rosa</a>
    simplesolaire http://www.simplesolaire.net/

  9. avatar tgpmusic

    <a href="http://www.rokettubemi.com/hommes-nike-shox-current-jaune-bleu-skoe">hommes nike shox current jaune bleu</a> <a href="http://www.cumjacket.com/air-jordan-aero-mania-lyser%C3%B8d-gul-footg">air jordan aero mania lyser酶d gul</a> <a href="http://www.hzrdcraft.com/nike-lebron-13-elite-negro-sneakerd">nike lebron 13 elite negro</a> <a href="http://www.clubrockorg.com/jordan-1-wei%C3%9F-rot-2014-schuhee">jordan 1 wei脽 rot 2014</a> <a href="http://www.giaiphapdautu.com/air-jordan-8-midnight-armada-hyperfuse-foote">air jordan 8 midnight armada hyperfuse</a> <a href="http://www.spreymax.com/mujeres-nike-air-max-87-sb-rojo-gris-skof">mujeres nike air max 87 sb rojo gris</a>
    tgpmusic http://www.tgpmusic.com/

  10. avatar wangentreppe

    <a href="http://www.salesmasterypro.net/nike-air-rejuven8-mule-gr%C3%A5-schuhee">nike air rejuven8 mule gr氓</a> <a href="http://www.sastafitness.net/nike-air-max-tn-rosa-and-nero-for-vendita-butyd">nike air max tn rosa and nero for vendita</a> <a href="http://www.sattonet.net/lebron-12-jaune-and-rouge-jordan-obuvc">lebron 12 jaune and rouge jordan</a> <a href="http://www.savetheastrodome.net/femmes-air-jordan-retro-4-rose-blanc-scarpee">femmes air jordan retro 4 rose blanc</a> <a href="http://www.schildgen.net/kobe-10-wolf-grau-obuvd">kobe 10 wolf grau</a> <a href="http://www.scogblog.net/kvinners-nike-air-max-90-vt-r%C3%B8d-hvit-chaussuresg">kvinners nike air max 90 vt r酶d hvit</a>
    [url=http://www.wangentreppe.net/]wangentreppe[/url]

  11. avatar ascential

    <a href="http://www.gaialog.net/air-jordan-aero-flight-blu-verde-kurpesb">air jordan aero flight blu verde</a> <a href="http://www.genopresse.net/adidas-john-walle-1-gr%C3%BCn-kurpesd">adidas john walle 1 gr眉n</a> <a href="http://www.gpsgolfcaddy.net/nike-flyknit-roshe-run-sort-uk-game-kurpesc">nike flyknit roshe run sort uk game</a> <a href="http://www.gregoryjewellers.net/jordan-9-blanco-gris-and-azul-scarpec">jordan 9 blanco gris and azul</a> <a href="http://www.hamiltonbookkeeping.net/air-max-90-jaune-and-noir-jaune-schuhee">air max 90 jaune and noir jaune</a> <a href="http://www.handykontakte.net/nike-zoom-vomero-12-s%C3%B8lv-hvid-obuvf">nike zoom vomero 12 s酶lv hvid</a>
    ascential http://www.ascential.net/

  12. avatar 地源热泵

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

  13. avatar 王语双

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

欢迎留言




用户登录

sitemap