【问题标题】:java android listView set diffrent background color and selected itemjava android listView设置不同的背景颜色和选定的项目
【发布时间】:2017-08-18 18:37:20
【问题描述】:

我想在适配器的点亮视图中设置不同的背景颜色我这样做了

if (position % 2 == 0) {
                convertView.setBackgroundColor(context.getResources().getColor(R.color.darkGrey));
            } else {
                convertView.setBackgroundColor(context.getResources().getColor(R.color.grey));
            }

它可以工作,但是当我添加这个时:

android:listSelector="@drawable/list_selector"

这里:

<ListView
        android:id="@+id/list"
        android:layout_marginTop="5dp"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"
        android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"
        android:visibility="visible"
        android:listSelector="@drawable/list_selector"
        android:layout_above="@+id/bottomPanel" />

我没有看到选定的项目,但是当我在适配器中删除设置颜色时,我看到了一个选定的项目

【问题讨论】:

  • 您已经在 pojo 列表项类中获取了一个变量,并根据该变量值设置了背景。
  • 您应该将list_selector.xml 添加到 ListView 项目中。并作为背景。
  • @DheerubhaiBansal 我不懂你
  • @KeLiuyue 我将list_selector.xml添加到listView
  • 我的意思是你将它添加到 ListView 的项目中。@DheerubhaiBansal

标签: java android listview


【解决方案1】:

您可以在此方法下为选中的项目设置背景

  myListView.setOnItemSelectedListener(new OnItemSelectedListener(){

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

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

【讨论】:

  • 如何更改颜色?
  • 我认为你只需要在 onItemSelected 中调用 convertView.setBackgroundColor(context.getResources().getColor(R.color.colorname))
猜你喜欢
  • 2011-08-28
  • 1970-01-01
  • 2013-11-06
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多