2015-01-26 191 views
1

我想创建带有ListView的NavigationDrawer幻灯片,并且listview颜色是紫色而不是白色。 这是我的代码: activity_mainListView背景颜色不是白色

<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> 

我在做什么错ListView控件应该是白色的,并在选定的状态 - 灰色?

+0

100%确保'@色/ nliveo_white'指'color.xml'定义的? CTRL +点击它指的是正确的?听起来很疯狂,但我找不到任何其他原因。 – 2015-01-26 19:27:46

+0

@ G.T。我相信 – Haim127 2015-01-27 14:16:07

回答

0

我想问题是与list_item_bg_pressed文件。 对于列表视图要白色和灰色的选中状态
它应修改为:

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> 
+0

不工作,按下的模式是OK,正常模式是紫色的 – Haim127 2015-01-27 14:11:30

0

我发现这个问题。 我用功能setBackgroundColor而不是setBackgroundResource。 因此,代码应该是:

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