selector中的state_selected不起作用:注意state_activated状态

2014年12月16日 Android 9条评论 阅读35571次
selector中的state_selected不起作用:注意state_activated状态
项目中遇到的问题。
N个功能的一排图标,放置到一个ListView中去显示,点击不同图标做相应功能的设置,为让用户更清楚明了知道现在设置是哪个功能,所以要加一个选择中的功能。
大体实现步骤
1.ListView中的图标在drawable中写出selector,该selector文件中列出了android:state_pressed的图片资源、state_selected的图片资源。
2.ListView的onItemClickedListener监听里,针对当前选择的view,调用view.selected(true)

遇到的问题
在ListView中的某个功能图标点击时,点击下去显示的是state_pressed的图片资源,但手离开后又变会了默认的图片资源。

解决问题
开始猜测是画面中存在的其他控件,如Button、SeekBar等抢占了焦点所以导致ListView中ListItem无法显示selected状态。
在布局文中控制处ListView外的其他控件全都加上android:focusable="true"使其不可获得焦点。
这样的结果并没解决问题,需求中想要的selected状态的图片还是没显示出来。
后来在StackOverflow网站中,找到了state_activated状态,于是就在selector的xml文件中加入了state_activated="true"的控制,结果问题解决了。

官网上的state_activated
定义在android.graphics.drawable.StateListDrawable类中对xml属性的android:state_activated做了如下说明
android:state_activated

State value for StateListDrawable, set when a view or its parent has been "activated" meaning the user has currently marked it as being of interest. This is an alternative representation of state_checked for when the state should be propagated down the view hierarchy.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol state_activated.

android.R.attr中的说明
public static final int state_activated

Added in API level 11
State value for StateListDrawable, set when a view or its parent has been "activated" meaning the user has currently marked it as being of interest. This is an alternative representation of state_checked for when the state should be propagated down the view hierarchy.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

Constant Value: 16843518 (0x010102fe)

注意事项
selector.xml中的state_activated状态属性必须在androidAPI-11及以上版本使用。
分享本文至:

WRITTEN BY

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

9 条评论

  1. avatar maplebeyond

    <a href="http://www.cardiachits.com/air-jordan-retro-3-violet-hommesthe-verte-chaussuresh">air jordan retro 3 violet hommesthe verte</a> <a href="http://www.zestchannel.com/nike-lebron-12-mujeres-p%C3%BArpura-rosado-zapatosp">nike lebron 12 mujeres p煤rpura rosado</a> <a href="http://www.eczacinim.com/nike-air-max-tailwind-7-black-silver-shoesi">nike air max tailwind 7 black silver</a> <a href="http://www.chilangorails.com/dam%C3%A4nner-air-jordan-retro-10-orangen-gold-schuhei">dam盲nner air jordan retro 10 orangen gold</a> <a href="http://www.mariusen.com/hombres-nike-roshe-run-high-naranja-gris-zapatosk">hombres nike roshe run high naranja gris</a> <a href="http://www.brittonoilco.com/kvinders-nike-flynit-air-max-lyser%C3%B8d-guld-skoi">kvinders nike flynit air max lyser酶d guld</a>
    maplebeyond http://www.maplebeyond.com/

  2. avatar sevenguard

    <a href="http://www.thebiarritz.com/polo-ralph-lauren-dip-dyed-linen-button-down-shirt-regular-fit-poloh">polo ralph lauren dip dyed linen button down shirt regular fit</a> <a href="http://www.voydnet.com/mens-nike-air-presto-purple-pink-shoesi">mens nike air presto purple pink</a> <a href="http://www.foneandlone.com/nike-air-max-90-premium-silver-white-shoesk">nike air max 90 premium silver white</a> <a href="http://www.roomwhiz.com/adidas-soccers-sko-gr%C3%B8nn-gull-obuvq">adidas soccers sko gr酶nn gull</a> <a href="http://www.riffsurfers.com/herre-nike-sb-stefan-janoski-max-alle-lilla-skoj">herre nike sb stefan janoski max alle lilla</a> <a href="http://www.davidmgaines.com/nike-air-max-zero-verde-rosa-scarpeq">nike air max zero verde rosa</a>
    sevenguard http://www.sevenguard.com/

  3. avatar delicouncil

    <a href="http://www.postthatass.com/m%C3%A4nner-nike-internationalist-grau-wei%C3%9F-schuhek">m盲nner nike internationalist grau wei脽</a> <a href="http://www.houseoftys.com/limited-demaryius-thomas-mens-jersey-denver-broncos-88-lights-out-grey-nfl-nflh">limited demaryius thomas mens jersey denver broncos 88 lights out grey nfl</a> <a href="http://www.amvdiplomados.com/nike-classic-cortez-premium-hvit-obuvi">nike classic cortez premium hvit</a> <a href="http://www.ybsimports.com/adidas-superstar-holographic-white-shoesj">adidas superstar holographic white</a> <a href="http://www.rochlockyer.com/adidas-tubular-doom-grey-primeknit-shoesl">adidas tubular doom grey primeknit</a> <a href="http://www.allsocialdata.com/men-long-sleeve-check-shirt-ralph-lauren-poloh">men long sleeve check shirt ralph lauren</a>
    delicouncil http://www.delicouncil.com/

  4. avatar thebiarritz

    <a href="http://www.jupickuplines.com/nike-zoom-pegasus-32-oransje-rosa-obuvk">nike zoom pegasus 32 oransje rosa</a> <a href="http://www.saleuggboot.com/mennns-air-jordan-2-gr%C3%A5-obuvi">mennns air jordan 2 gr氓</a> <a href="http://www.fragmanizletv.com/nike-air-max-tailwind-8-mujeres-verde-oro-zapatosk">nike air max tailwind 8 mujeres verde oro</a> <a href="http://www.swisshomeas.com/how-much-are-coach-shoes-worth-coachh">how much are coach shoes worth</a> <a href="http://www.marinersdiary.com/womens-nike-shox-turbo-21-blue-orange-shoesj">womens nike shox turbo 21 blue orange</a> <a href="http://www.bourseasie.com/kvinders-asics-gel-hyper-33-gul-s%C3%B8lv-skoq">kvinders asics gel hyper 33 gul s酶lv</a>
    [url=http://www.thebiarritz.com/]thebiarritz[/url]

  5. avatar brutalistmap

    <a href="http://www.preloxlady.com/hommes-new-balance-420-gris-schuheg">hommes new balance 420 gris</a> <a href="http://www.scalfarogroup.com/mujer-ralph-lauren-mesh-polo-pony-p%C3%BArpura-embroidery-poloz">mujer ralph lauren mesh polo pony p煤rpura embroidery</a> <a href="http://www.tubetotext.com/nike-kd-8-high-gr%C3%B8n-orange-chaussuresa">nike kd 8 high gr酶n orange</a> <a href="http://www.vapequalite.com/herre-nike-free-5.0-guld-lyser%C3%B8d-schuhed">herre nike free 5.0 guld lyser酶d</a> <a href="http://www.beylikduzufm.com/nike-air-max-bw-argento-verde-scarpea">nike air max bw argento verde</a> <a href="http://www.emmuscades.com/air-jordan-future-reflective-nero-and-bianca-kurpesf">air jordan future reflective nero and bianca</a>
    brutalistmap http://www.brutalistmap.com/

  6. avatar 都市女生

    感谢分享

  7. avatar 威客百科

    感觉还哦是挺麻烦的啊

  8. avatar 王语双

    送IP来了!

欢迎留言




用户登录

sitemap