【发布时间】:2014-06-10 10:56:33
【问题描述】:
如何更改列表视图的颜色并使其保持不变,直到从该列表视图中单击另一个项目?
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<3;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("ICONIMG", Integer.toString(drwbl_imgs[i]) );
hm.put("TITLE",str_tit[i]);
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "ICONIMG","TITLE"};
// Ids of views in listview_layout
int[] to = { R.id.img_listicon,R.id.txtvw_slidebar_listview};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.child_slidebar_listview_layout, from, to);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
}
main_layout.xml
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:divider="@android:color/transparent" >
</ListView>
child_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_listview_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:orientation="horizontal"
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:background="#808080"
>
<ImageView
android:id="@+id/img_listicon"
android:layout_width="15dp"
android:layout_height="15dp"
/>
<TextView
android:id="@+id/txtvw_slidebar_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:text=""
/>
</LinearLayout>
</LinearLayout>
【问题讨论】: