这是我的列表视图代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/darkgrey">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
这是列表选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg_hover" />
<item android:drawable="@drawable/gradient_bg" />
</selector>
我的目标是为列表项添加背景:初始(没有点击,没有焦点)-> 灰色背景。按下时 -> 蓝色背景。发布时 -> 灰色背景。
但最初的背景没有出现。打开应用程序和尚未单击项目时,不会显示灰色背景。有人知道如何解决这个问题吗?
java部分:
package com.example.whs;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class Index extends Activity {
public static final Object TITLE = "title";
public static final Object SUBTITLE = "subtitle";
public static final Object THUMBNAIL = "thumbnail";
protected static final String POSITION = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
// Back actionbar icon enable
//ActionBar actionBar = getActionBar();
//actionBar.setDisplayHomeAsUpEnabled(true);
buildMenu();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.index, menu);
return true;
}
//Builds the menu for listview
public void buildMenu(){
ArrayList<HashMap<String, String>> menu = new ArrayList<HashMap<String, String>>();
//Arrays for info
String[] menuTitleArray = {"Updates", "Gallerij"};
String[] menuSubtitleArray = {"Bekijk updates", "Bekijk foto's en geef reacties", "Bekijk de updates"};
String[] menuThumbnailArray = {"updates", "gallery"};
for(int i=0; i < menuTitleArray.length; i++){
// Build Hashmap for the item
HashMap<String, String> item = new HashMap<String, String>();
item.put((String) TITLE, menuTitleArray[i]);
item.put((String) SUBTITLE, menuSubtitleArray[i]);
item.put((String) THUMBNAIL, menuThumbnailArray[i]);
menu.add(item);
}
// Add adapter to the list
MenuAdapter adapter = new MenuAdapter(this, menu);
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
// Initialize the click event
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
switch(position){
case 0:
Intent intent = new Intent(Index.this, Updates.class);
startActivity(intent);
}
}
});
}
}