ListView背景颜色不是白色

时间:2015-01-26 19:17:41

标签: android listview navigation-drawer

我正在尝试使用ListView创建NavigationDrawer幻灯片,listview颜色为紫色而不是白色。 这是我的代码: activity_main

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->
    <ListView
            android:id="@+id/listDrawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:cacheColorHint="@color/nliveo_white"
            android:choiceMode="singleChoice"
            android:divider="@color/nliveo_transparent"
            android:background="@color/nliveo_white"
            android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>

MyAdapter

public class NavDrawerListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<NavDrawerItem> navDrawerItems;

    private static final int NOT_SELECTED = -1;
    private int selectedPos = NOT_SELECTED;

    public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
        this.context = context;
        this.navDrawerItems = navDrawerItems;
    }

    @Override
    public int getCount() {
        return navDrawerItems.size();
    }

    @Override
    public Object getItem(int position) {
        return navDrawerItems.get(position);
    }

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

    public void setSelection(int position) {
        if (selectedPos == position) {
            selectedPos = NOT_SELECTED;
        } else {
            selectedPos = position;
        }
        notifyDataSetChanged();
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.drawer_list_item, null);
        }

        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.rowIcon);
        TextView txtTitle = (TextView) convertView.findViewById(R.id.rowTitle);

        imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
        txtTitle.setText(navDrawerItems.get(position).getTitle());

        if (position == selectedPos) {
            convertView.setBackgroundResource(R.drawable.list_item_bg_pressed);
        } else {
            convertView.setBackgroundColor(R.drawable.list_item_bg_normal);
        }

        return convertView;
    }

}

和风格: list_item_bg_normal

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
          android:drawable="@color/nliveo_transparent" />
    <item android:state_pressed="true"
          android:drawable="@color/nliveo_transparent" />
    <item android:state_selected="false"
          android:drawable="@color/nliveo_white"/>
</selector>

list_item_bg_pressed

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
          android:drawable="@color/nliveo_transparent" />
    <item android:state_pressed="true"
          android:drawable="@color/nliveo_transparent" />
    <item android:state_selected="false"
          android:drawable="@color/nliveo_gray"/>
</selector>

color.xml

<color name="nliveo_white">#ffffff</color>
    <color name="nliveo_gray">#e0e0e0</color>
    <color name="nliveo_black">#000000</color>
    <color name="nliveo_transparent">#00000000</color>

我做错了什么?列表视图应该是白色且处于选定状态 - 灰色。

2 个答案:

答案 0 :(得分:0)

我猜问题是list_item_bg_pressed文件。 对于Listview在选定状态下为白色和灰色
它应该被修改为:

bg_pressed

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="true"
      android:drawable="@color/nliveo_gray" />
 <item android:state_pressed="true"
      android:drawable="@color/nliveo_gray" />
 <item android:state_selected="false"
      android:drawable="@color/nliveo_transparent"/>
 </selector>

答案 1 :(得分:0)

我发现了问题。 我使用函数setBackgroundColor而不是setBackgroundResource。 所以代码应该是:

if (position == selectedPos) {
            convertView.setBackgroundResource(R.drawable.list_item_bg_pressed);
        } else {
            convertView.setBackgroundColor(R.drawable.list_item_bg_normal);
        }