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

标签 java android listview user-interface

我有以下要求:

  • 不同 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/

相关文章:

java - 动态加载可能有异常的对象可以声明为final吗?

java - 有没有办法使用 ACTION_SEND 在 google plus 上发布多张图片

android - 如何在 Android 上使背景透明 20%

android - 如何为 ListView 下载图像设置条件?

java - 在 Java 中获取操作系统动画首选项

java - 如何在 Java 中使用带有语言运算符和扩展 Number 的泛型类的泛型

android - 在 2.2 上获取 ListView 项目/倒序的 View ;适用于 4.0.3

android - ListView 在数据库更新和 adapter.notifyDataSetChanged() 后不更新;

java - AutocompleteTextview 中的 Android Listview 没有响应

java - 初始化 Spring 上下文时 org.springframework.asm.ClassReader 出现 IllegalArgumentException