java-不同列表视图项的不同选择颜色
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-不同列表视图项的不同选择颜色,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3383字,纯文字阅读大概需要5分钟。
内容图文
![java-不同列表视图项的不同选择颜色](/upload/InfoBanner/zyjiaocheng/679/e7003cb2b8444f8390104218ffa480c2.jpg)
我有以下要求:
>不同的颜色用于不同的列表视图项目
>在代码中动态指定颜色
>仅在按下/选择列表视图项目时才显示颜色
>列表视图项的颜色不应永久更改
无论出于什么原因,这似乎都不像我想的那么直接.唯一在正确方向上至少走一点的解决方案是:https://stackoverflow.com/a/16978159/658718
需要注意的是,这不会更改选定的颜色,而是会永久更改背景色,而且如果您向下滚动,它已经更改了列表视图项目的背景色.
我该如何处理?
解决方法:
这里的困难在于,按下/选中的颜色是动态的.您不能使用静态xml color-state-list.但是您可以通过代码创建ColorStateList.这是这样做的方法.
您只需要实现ListAdapter:
private class MyListAdapter implements ListAdapter{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView!=null){
CheckedTextView textView = (CheckedTextView)convertView;
textView.setText("the text for item "+position);
textView.setTextColor(makeColorStateListForItem(position));
return textView;
}else{
CheckedTextView textView = new CheckedTextView(parent.getContext());
textView.setText("the text for item "+position);
textView.setTextColor(makeColorStateListForItem(position));
return textView;
}
}
private ColorStateList makeColorStateListForItem(int position){
int pressedColor = pressedColorForItem(position);
int checkedColor = checkedColorForItem(position);
int defaultColor = defaultColorForItem(position);
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_checked},
new int[]{0},
},
new int[]{
pressedColor, //use when state is pressed
checkedColor, //use when state is checked, but not pressed
defaultColor}); //used when state is not pressed, nor checked
}
private int pressedColorForItem(int position){
//write your business logic to determine color here
return ...;
}
private int checkedColorForItem(int position){
//write your business logic to determine color here
return ...;
}
private int defaultColorForItem(int position){
return Color.WHITE;
}
//all other adapter methods
//...
请注意,使用android.R.attr.state_checked而不是更直观的android.R.attr.state_selected,因为state_selected并不是通过触摸屏精确定义的(即state_selected可以在模拟器上提供预期的行为,但在实际设备可能会失败)
另一方面,state_checked CheckedTextView将在模拟器和真实设备上正常工作.
在clickListener中初始化listView和ListView.setItemChecked时只需调用List.setChoiceMode(...);.
final ListView listView = ...
listView.setAdapter(new MyListAdapter());
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listView.setItemChecked(position,true);
}
});
编辑:更改项目背景:只需创建一个StateListDrawable而不是简单的ColorStateList即可:
private Drawable makeBackgroungForItem(int position){
int pressedColor = pressedBackgroundColorForItem(position);
int checkedColor = checkedBackgroundColorForItem(position);
int defaultColor = defaultBackgroundColorForItem(position);
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_list_pressed}, new ColorDrawable(pressedColor));
stateListDrawable.addState(new int[]{android.R.attr.state_list_checked}, new ColorDrawable(checkedColor));
stateListDrawable.addState(new int[]{0, new ColorDrawable(defaultColor));
return stateListDrawable;
}
并在getView(…)
textView.setBackground(makeBackgroungForItem(position));
内容总结
以上是互联网集市为您收集整理的java-不同列表视图项的不同选择颜色全部内容,希望文章能够帮你解决java-不同列表视图项的不同选择颜色所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。