一.ListView的事件:
1.setOnItemClickListener()點擊事件,
設置“點擊”listview某一項的監聽事件。
其中view參數指點擊的Listview的那一項的布局,可通過view.findViewById找到布局中感興趣的控件,然后再獲取控件的值
position指點擊的adapter的那一項的位置(索引從0開始)
id指點擊的Listview的那一項的Id(索引從0開始)
listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ArrayAdapterActivity.this, "你點擊了第"+(position+1)+"項", Toast.LENGTH_SHORT).show(); } });
2.setOnItemLongClickListener()長按事件,設置“長按”listview某一項的監聽事件。
listview.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ArrayAdapterActivity.this, "你長按
了第"+(position+1)+"項", Toast.LENGTH_SHORT).show();
return false;
}
});
二.ListView由於item項中包含某些可以搶焦點的控件導致無法獲取焦點問題:
(注意:ListView的項根元素layout的寬要設置成android:layout_width="fill_parent",否則可能會因為項內容太短,導致點擊時獲取不到焦點)
1>.如果你自定義ListView的項中包含能獲得焦點的子控件(RadioGroup、Button、CheckBox、DatePicker、ImageButton、ScrollView、SeekBar、EditText、ToggleButton、RatingBar等)的話,默認焦點會被交給這些子控件,而ListView的項能被選中的基礎是它能獲取焦點,所以項中的這些子控件的焦點必須為false,這樣ListView的項才能獲取onItemLongClick事件與onItemClick事件
解決辦法(以Button為例):
在布局文件中,在項的layout布局文件根元素中加入android:descendantFocusability="blocksDescendants"如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants" >
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/header"
android:layout_toRightOf="@id/header" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/name"
android:layout_below="@id/name" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/deleteselect" />
</RelativeLayout>
如果layout是程序動態生成的,則調用
layout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
2>.如果你自定義ListView的項中包含能獲得焦點的子控件GridView的話,默認焦點會被交給這些子控件,而ListView的項能被選中的基礎是它能獲取焦點,所以項中的這些子控件的焦點必須為false,這樣ListView的項才能獲取onItemLongClick事件與onItemClick事件
解決辦法(以Button為例):
1.在布局文件中,在項的layout布局文件根元素中加入
android:descendantFocusability="blocksDescendants"
2.程序中給GridView進行如下設置
gridView.setClickable(false); gridView.setPressed(false); gridView.setEnabled(false);
三.ListView的UI顯示中常見問題及解決辦法
1>如果需求是listview點擊時,item無背景變色效果
步驟:
1.drawable文件夾中新建timer_list_selector.xml內容如下
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_selected="true" android:drawable="@color/transparent"/> </selector>
2.values文件夾中新建colors.xml內容如下
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="transparent">#50000000</color> </resources>
3.布局文件中給listview加入如下屬性
android:listSelector="@drawable/timer_list_selector"
2>listview設置分割線Divider樣式,布局文件給listview加入如下屬性(已分隔符為顏色為例)
android:divider屬性用來設置分割線顏色(或圖片),當設置為#00000000時表示item之間無間隙;
通過android:dividerHeight屬性設置分割線高度
android:divider="#ff999999"
android:dividerHeight="1sp"
3>UI展現時常見問題
問題1:listview設置背景,拖動listview時顯示黑色,只有拖動完才會顯示我們設置的背景顏色或圖片
產生原因:listview的背景是固定不變的,默認Listview的每項的背景是透明的,拖動滾動條的過程中需要實時將每個項的顯示內容跟背景進行混合運算,android系統為了優化這個過程,使用了android:cacheColorHint屬性,在黑色背景下默認顏色為#191919,所以出現了上面的黑色顯示問題
解決辦法:(根據需求而定)
1.如果只換背景顏色:將android:cacheColorHint設置成和背景顏色一樣或android:scrollingCache="false"如下
android:cacheColorHint="#ff00ff00"
android:background="#ff00ff00"
或
android:scrollingCache="false"
android:background="#ff00ff00"
2.如果用圖片做背景:將android:cacheColorHint設為#00000000變為透明或android:scrollingCache="false"即可如下
android:cacheColorHint="#00000000"
android:background="@drawable/ic_launcher"
或
android:scrollingCache="false"
android:background="@drawable/ic_launcher"
問題2:listview上面或下面有黑色陰影
解決辦法:布局文件中給listview加入android:fadingEdge="none"
UI總結:
綜上問題得到最終的布局為(帶分割線+背景色)
<ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="180dip" android:divider="#ff999999" android:dividerHeight="1sp" android:fadingEdge="none" android:scrollingCache="false" android:background="#ff00ff00" />