gpt4 book ai didi

java - 不同 ListView 项的不同选择颜色

转载 作者:太空宇宙 更新时间:2023-11-03 13:23:21 25 4
gpt4 key购买 nike

我有以下要求:

  • 不同 ListView 项目的不同颜色
  • 颜色在代码中动态指定
  • 仅当按下/选择 ListView 项时才应显示颜色
  • ListView 项的颜色不应永久改变

无论出于何种原因,它似乎并不像我想象的那么简单。唯一至少朝着正确方向前进一点点的解决方案是这个:https://stackoverflow.com/a/16978159/658718

需要注意的是,这不会更改选择颜色,但会永久更改背景颜色,而且如果向下滚动一点,它已经更改了 ListView 项的背景颜色。

我该如何解决这个问题?

最佳答案

这里的困难在于按下/选中的颜色是动态的。您不能使用静态 xml 颜色状态列表。但是你可以创建 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将在模拟器和真实设备上正常工作。

只需调用 List.setChoiceMode(...);在初始化 listView 和 ListView.setItemChecked 时在 clickListener 中。

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 - 不同 ListView 项的不同选择颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24285185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com