Android ListView 使用

一.ListView點擊變色

    1.selector_text.xml listview——item中textview字體顏色改變

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 沒有焦點時字體顏色 -->
    <item android:state_selected="false" android:color="#720606"/>
    <!-- 選中時的字體顏色 -->
    <item android:state_selected="true" android:color="#FF6666"/>
    <!-- 非觸摸模式下得到焦點並單擊時的字體顏色 -->
    <item android:state_focused="true" android:state_pressed="true" android:color="#720606"/>
</selector>

<!--或者以下-->
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默認時的背景圖片-->
    <item android:drawable="@drawable/pic1" />

<!-- 沒有焦點時的背景圖片 -->
    <item android:state_window_focused="false"
                android:drawable="@drawable/pic1" />

<!-- 非觸摸模式下得到焦點並單擊時的背景圖片 -->
    <item android:state_focused="true"
                android:state_pressed="true"
                android:drawable= "@drawable/pic2" />

<!-- 觸摸模式下單擊時的背景圖片-->
    <item android:state_focused="false"
                android:state_pressed="true"
                android:drawable="@drawable/pic3" />

<!--選中時的圖片背景-->
    <item android:state_selected="true"
                android:drawable="@drawable/pic4" />

<!--得到焦點時的圖片背景-->
    <item android:state_focused="true"
                android:drawable="@drawable/pic5" />
</selector>

    2.selector.xml listview——點擊背景顏色改變

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/winered" android:state_pressed="true"></item>
    <item android:drawable="@color/themewhite" android:state_pressed="false"></item>
</selector>

    3.xml中設置

<!--ListView中設置-->
<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:listSelector="@drawable/selector" //主要設置這個
    android:cacheColorHint="@android:color/transparent" //解決列表有時候爲黑
    android:scrollbars="none" />
 <!--或者ListView的Item中設置背景屬性-->
 android:background="@drawable/xxx"

    4.代碼中設置

Drawable drawable = getResources().getDrawable(R.drawable.xxx);  
ListView.setSelector(drawable);
//可是這樣會出現列表有時候爲黑的狀況,須要加上:android:cacheColorHint="@android:color/transparent"使其透明。

    5.備註

    android:state_selected是選中java

    android:state_focused是得到焦點android

    android:state_pressed是點擊數組

    android:state_enabled是設置是否響應事件,指全部事件ide

二.ListView中使用CheckBox,在翻頁後CheckBox被選中亂序問題

    假如ListView,分紅2頁(或者設置數據能夠縱向拉,可隱藏),每頁3條數據,每一個Listview的Item 裏面有個checkBox,字體

如今,當我選擇第一頁的前兩天數據,翻到第二頁,居然第二頁後兩條數據也選中了,這是絕對不容許的。spa

    通過本人的N次調試,發現public View getView(int position, View convertView, ViewGroup parent)傳進來的convertView居然產生屢次重用。解決方案:當選中checkedBox時候,咱們用一個List來保存該checkBox的position。.net

而後在每次產生View時取得傳來的convertView賦值爲null,再遍歷List裏保存的checkBox的位置,當在數組內時,checkBox置爲選中,問題解決了。調試

該問題有兩種解決方案,我的目前所實現了的。code

    1.用HashMap保存checkbox的狀態值。

HashMap<Integer, Boolean> state = new HashMap<Integer,Boolean>();

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){  
        state.put(position, isChecked);
        System.out.println("複選框以選中,選中的行數爲:" + temp_position);
    }else{
        state.remove(position);
    }
}

    在getView()方法裏面:xml

holder.cbox.setChecked(state.get(position)==null? false : true);

    2.用List<String>保存checkbox位置

private static List<String> selected = new ArrayList<String>();

cbSelect.setOnClickListener(new OnClickListener() {

	@Override
	public void onClick(View arg0) {
		if (cbSelect.isChecked()) {
			selected.add(String.valueOf(fid));			
		} else if (!cbSelect.isChecked()) {
			selected.remove(String.valueOf(position));			
		}
	}
});

    在getView()方法裏面:

cbSelect.setChecked((selected.contains(String.valueOf(position))));

    3.(不推薦使用,由於會產生許多垃圾對象)

    public View getView(int position, View convertView, ViewGroup parent)在每次傳進convertView時候,設爲null。

    而後每調用一次getView就產生一個view對象。

    Android ListView或GridView中含有CheckBox時,獲取選中的id

    Android ListView Adapter中checkbox 勾選位置錯亂

   http://blog.csdn.net/fancylovejava/article/details/43227321

 

三.Android 側滑刪除 SwipeMenuListView

    這是個人博客裏面有詳細介紹:http://my.oschina.net/u/2320057/blog/637111

    Android 側滑刪除 SwipeMenuListView

 

四.ViewHolder簡潔寫法與替代findViewById方法

    Android ViewHolder簡潔寫法及替代findViewById方法

 

五.ScrollView和ListView共同使用時,ListView所有展開

    ScrollView+listView共同使用時,ListView所有展開

六.ListView沒有數據時,頁面處理

    setEmptyView()方式:

    ListView有自帶setEmptyView(View view)方法,只要將沒有數據時的View添加進去就能夠了。

    可是要注意,添加前先要作以下操做:

((ViewGroup)listView.getParent()).addView(emptyView);

七.ListView中滾動條設置

    http://blog.csdn.net/ccpat/article/details/50805129

相關文章
相關標籤/搜索