yel*_*von 8 resources android listview preferenceactivity
我想在用户点击它时更改listview项的背景.有点像Honeycomb设置页面(虽然我没有处理只是设置所以我没有使用PreferenceActivity)我有这个功能通过资源状态选择器状态选择器工作,除了单击列表视图菜单更改线性的情况列表视图右侧的布局(一种分屏视图).我猜测listview失去焦点所以state_pressed不再是真的.
<item android:state_pressed="true">
<shape >
<solid android:color="@color/blue1" />
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
在选择另一个列表视图项目之前,是否保持列表视图项目着色的任何提示?谢谢!
编辑:
我能够在setOnItemClickListener中更改背景
view.setBackgroundResource(R.color.red);
Run Code Online (Sandbox Code Playgroud)
我只需要一次一个选择,从而被点击其他列表项目时,我试图lv.invalidate()和lv.getChildAt(0).invalidate(),但既不工作和第二导致空指针异常.有什么想法让颜色回来?
当您从手指中释放手指时,它不会再按下注册状态.您要做的实际上是在用户选择时更改单个行的背景.这意味着实现onItemClick或onItemTouch并标记适配器以使用新背景重绘行.如果您已经在使用自定义列表适配器,则可以在getView()方法中对布尔值实施检查.您还需要跟踪哪些行被"选中"哪些行未被选中.
伪代码:
public View getView(int pos, View convertView, ViewGroup parent) {
if(isChecked[pos]) //set background to checked color
}
Run Code Online (Sandbox Code Playgroud)
小智 5
希望这有帮助,
1.-为焦点项创建一个形状文件:\ drawable\list_selector_focused.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:angle="90" android:startColor="#f5c98c" android:endColor="#f7ddb8"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
2.-为按下的项目创建一个形状文件:\ drawable\list_selector_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:angle="90" android:startColor="#fb9d23" android:endColor="#ffc579" />
</shape>
Run Code Online (Sandbox Code Playgroud)
3.-创建列表选择器文件:\ drawable\list_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/list_selector_pressed" />
<item android:state_focused="true" android:drawable="@drawable/list_selector_focused" />
<item android:drawable="@drawable/list_selector_focused" />
</selector>
Run Code Online (Sandbox Code Playgroud)
4.-将此属性添加到布局文件中的ListView:
android:choiceMode="singleChoice"
android:listSelector="@drawable/list_selector"
Run Code Online (Sandbox Code Playgroud)
您可以使用颜色代替渐变形状,
| 归档时间: |
|
| 查看次数: |
20377 次 |
| 最近记录: |