不同列表视图项目的不同选择颜色

2023-12-28

我有以下要求:

  • 不同的列表视图项目有不同的颜色
  • 颜色在代码中动态指定
  • 仅当按下/选择列表视图项时才应显示颜色
  • 列表视图项的颜色不应永久更改

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

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

我该如何处理这个问题?


这里的困难在于按下/检查的颜色是动态的。您不能使用静态 xml 颜色状态列表。但你可以创建颜色状态列表 http://developer.android.com/reference/android/content/res/ColorStateList.html通过代码。以下是如何做到这一点。

你只需要实现 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 + 选中的文本视图 http://developer.android.com/reference/android/widget/CheckedTextView.html将在模拟器和真实设备上正常工作。

只需致电List.setChoiceMode(...); http://developer.android.com/reference/android/widget/AbsListView.html#setChoiceMode%28int%29初始化 listView 时和ListView.setItemChecked http://developer.android.com/reference/android/widget/AbsListView.html#setItemChecked%28int,%20boolean%29在点击监听器中。

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);
    }
});

EDIT:更改项目背景:只需创建一个 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));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不同列表视图项目的不同选择颜色 的相关文章

随机推荐