不同的颜色选择不同的列表视图项目 [英] Different select colors for different list view items

查看:227
本文介绍了不同的颜色选择不同的列表视图项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要求:


  • 不同颜色不同的列表视图项

  • 颜色是动态的code
  • 规定
  • 只应显示的颜色,如果列表视图产品pressed /选择

  • 列表视图项的颜色不应该永久更改

不管什么原因,它似乎并没有像我想象的简单。就是那张在唯一的解决办法,至少在正确的方向一点点是这个: http://stackoverflow.com/a/16978159/ 658718

For whatever reasons it seems not to be as straight forward as I thought. The only solution that goes at least a little bit in the right direction is this one: http://stackoverflow.com/a/16978159/658718

需要注意的是,这并没有改变上选择颜色,但永久改变背景颜色,再加上如果你稍微向下滚动它已经改变了列表视图项的背景颜色。

The caveat is, that this does not change the on select color, but changes the background color permanently, plus it already changes the background color for list view items if you scroll down a bit.

我该如何处理这个?

推荐答案

这里的困难是,pressed /检查颜色是动态的。不能使用静态XML色状态列表。但是你可以通过code创建 Col​​orStateList 。这里是如何做到这一点。

The difficulty here is that pressed/checked color is dynamic. You cannot use static xml color-state-list. But you can create ColorStateList by code. Here is how to do that.

您只需要执行ListAdapter:

You just have to implement the 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 不是很$ p $带触摸屏的pcisely定义(即state_selected可以给模拟器上的预期的行为,而是一个真正的设备上恐怕也会失败)

Note the use of android.R.attr.state_checked instead of the more intuitive android.R.attr.state_selected because the state_selected is not very precisely define with a touch screen (i.e. state_selected can give the expected behavior on the emulator, but on a real device it will probably fail)

在另一方面 state_checked + CheckedTextView 是要在两个模拟器和真实的设备正常工作。

On the other hand state_checked + CheckedTextView is going to work correctly on both simulator and real device.

只需拨打 列表。 setChoiceMode(...); 初始化时ListView和<一个href=\"http://developer.android.com/reference/android/widget/AbsListView.html#setItemChecked%28int,%20boolean%29\"相对=nofollow> ListView.setItemChecked 在clickListener。

Just call List.setChoiceMode(...); when initializing listView and ListView.setItemChecked in the 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的:


EDIT : to change the item background : just create a StateListDrawable instead of a simple 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));

这篇关于不同的颜色选择不同的列表视图项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆