在Android上更改ListView项目的背景颜色

Mar*_*tój 70 android listview colors

如何ListView在每个项目的基础上更改项目的背景颜色.当我android:backgroundColorListView项目布局中使用时,我可以实现此目的,但列表选择器不再可见.我可以通过设置drawSelectorOnTop为true 使选择器再次可见,但选择器覆盖整个项目.

有任何想法如何改变这些背景颜色并保持选择器?

PS我宁愿不改变选择器本身.

编辑:GMail应用程序的作者已经设法实现了这一点,所以它绝对是可能的.

Haw*_*kee 36

您必须为要使用的每种颜色创建不同的状态drawable.

例如:list_selector_read.xmllist_selector_unread.xml.

您需要做的就是将所有内容设置为透明,但android:state_window_focused="false"项目除外.

然后,当您绘制列表时,您需要调用 setBackgroundResource(R.drawable.list_selector_unread/read)每一行.

您根本不在ListView上设置listSelector.这将维护您的特定Android风格的默认选择器.

  • +1这应该是接受的答案.并且`row.setBackgroundResource()`发生在`YourCustomArrayAdapter.getView()`或者你可以通过在`res/layout /的最外层元素上设置`android:background ="@ drawable/your_list_entry_selector"`完全放弃这一点. your_list_entry_layout.xml`. (6认同)

Fra*_*zas 26

这是基于上面代码的修改,最简单的代码:

private static int save = -1;

public void onListItemClick(ListView parent, View v, int position, long id) { 

    parent.getChildAt(position).setBackgroundColor(Color.BLUE);

    if (save != -1 && save != position){
        parent.getChildAt(save).setBackgroundColor(Color.BLACK);
    }

    save = position;                

}
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助

问候!


Ada*_*dam 23

好的,我让它像这样工作:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:drawable="@color/BackgroundColor" />
    <item android:drawable="@color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)

因人而异!


ubz*_*ack 11

似乎没有人提供任何使用适配器这样做的例子,所以我想我会发布我的代码片段来显示ListViews,其中"curSelected"项具有不同的背景:

final ListView lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new BaseAdapter()
{
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            convertView = new TextView(ListHighlightTestActivity.this);
            convertView.setPadding(10, 10, 10, 10);
            ((TextView)convertView).setTextColor(Color.WHITE);
        }

        convertView.setBackgroundColor((position == curSelected) ? 
            Color.argb(0x80, 0x20, 0xa0, 0x40) : Color.argb(0, 0, 0, 0));
        ((TextView)convertView).setText((String)getItem(position));

        return convertView;
    }

    public long getItemId(int position)
    {
        return position;
    }

    public Object getItem(int position)
    {
        return "item " + position;
    }

    public int getCount()
    {
        return 20;
    }
});
Run Code Online (Sandbox Code Playgroud)

对于我来说,当列表项的外观需要动态更改时,这一直是一种有用的方法.


The*_*ard 8

Android 2.2电子邮件应用程序的源代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_selected="true"
        android:drawable="@android:color/transparent" />
    <item android:state_selected="true"
        android:drawable="@android:color/transparent" />
    <item android:state_pressed="true" android:state_selected="false"
        android:drawable="@android:color/transparent" />
    <item android:state_selected="false"
        android:drawable="@color/message_item_read" />
</selector>
Run Code Online (Sandbox Code Playgroud)

没什么好说的...


Mbo*_*and 7

mAgendaListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//view.setBackgroundColor(Color.RED);

for(int i=0; i<parent.getChildCount(); i++)
{
     if(i == position)
     {
               parent.getChildAt(i).setBackgroundColor(Color.BLUE);
     }
     else
     {
               parent.getChildAt(i).setBackgroundColor(Color.BLACK);
     }

 }
Run Code Online (Sandbox Code Playgroud)


OWA*_*DVL 7

这是最简单的方法.在ListArrayAdapter中,只需执行此操作即可

if(your condition here) rowView.setBackgroundColor(Color.parseColor("#20FFFFFF"));
Run Code Online (Sandbox Code Playgroud)

不要过于复杂

  • 这不是回答问题。.他想为某些行保持列表选择器具有不同的背景色..您的答案将删除列表选择器样式! (2认同)

Pra*_*adW 5

您是要在单击时更改自定义列表项的背景颜色吗?

如果是这样的话:

您只需将以下代码添加到 xml 中的列表视图布局中。


android:drawSelectorOnTop="true" android:listSelector="@android:drawable/list_selector_background"


此处的列表选择器使用具有深灰色的默认选择器。您可以制作自己的可绘制对象并将其分配给上述列表选择器。

希望这是你想要的。


小智 5

更改项目布局中所有内容的简单代码(自定义列表视图扩展baseadapter):

lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            RelativeLayout layout=(RelativeLayout) arg1.findViewById(R.id.rel_cell_left);
            layout.setBackgroundColor(Color.YELLOW);



        }
    });
Run Code Online (Sandbox Code Playgroud)


Hei*_*nen 1

看一下List14 示例getView()您可以在其中调用convertView.setBackgroundDrawable()每个条目。例如,您可以使用类成员计数器来决定使用哪个背景来调用它以获得交替的背景。

  • 将“android:drawSelectorOnTop”更改为“false”。 (2认同)