ListView项不会停留"选择" [英] ListView item won't stay "selected"

查看:127
本文介绍了ListView项不会停留"选择"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要当用户点击它来改变一个列表视图项的背景。有点像蜂窝设置页(虽然我不处理的只是设置,因此我不使用preferenceActivity)我有这样的功能,通过资源状态选择状态选择除了列表视图菜单上,单击当案件工作变为线性布局列表视图右侧(排序分屏视图)。我猜列表视图失去焦点,这样STATE_ pressed不再是正确的。

I want to change the background of a listview item when the user clicks it. Kind of like the Honeycomb settings page (Although I'm not dealing with just settings so I'm not using PreferenceActivity) I have this functionality working through a resource state selector state selector except for the cases when clicking on the listview menu changes the linear layout to the right of the listview (sort of a split screen view). I'm guessing the listview looses focus so state_pressed is no longer true.

   <item android:state_pressed="true">
     <shape  >
        <solid android:color="@color/blue1" />
     </shape>
   </item>

任何技巧,让彩色直到另一个列表视图项的列表视图项被选中?谢谢!

Any tips to keep that listview item colored until another listview item is selected? Thanks!

编辑:

我能得到背景的setOnItemClickListener改变以

I was able to get the background changed in a setOnItemClickListener with

view.setBackgroundResource(R.color.red); 

我只需要一次,所以当其他列表项被点击,我试图选择 lv.invalidate() lv.getChildAt (0).invalidate()但都没有成功,第二原因空指针异常。任何想法把颜色了?

I only need one selected at a time so when the other list items are clicked, I tried lv.invalidate() and lv.getChildAt(0).invalidate() but neither worked and the second causes null pointer exception. Any ideas for putting the color back?

推荐答案

当你从它不再注册为pressed细胞松开手指。什么,你会想要做的实际上是改变当用户选择是个体行的背景。这意味着实施的<一个href="http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html">onItemClick或onItemTouch和标记适配器重绘该行的新的背景。如果您正在使用一个自定义列表适配器,你可以实现针对您的getView()方法为布尔检查。此外,还需要哪些行的选择,并跟踪哪些不是。

When you release your finger from the cell it no longer registers as pressed. What you are going to want to do is actually change the background of the individual row when a users selects is. This means implementing an onItemClick or onItemTouch and flagging the adapter to redraw the row with the new background. If you are already using a custom list adapter you can just implement a check against a boolean in your getView() method. You will also need to keep track which rows are 'selected' and which are not.

伪code:

   public View getView(int pos, View convertView, ViewGroup parent) {
      if(isChecked[pos]) //set background to checked color
   }

这篇关于ListView项不会停留&QUOT;选择&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆