Android ListView中常見錯誤匯總

來源:互聯網
上載者:User

android中ListView使用最為頻繁,但是同時出現錯誤也是最多的,下面就總結一下我在開發中遇到的問題,

問題一:在自訂添加adapter中添加button,或是checkBox時無法同時響應setOnItemClickListener事件中的item click 事件和控制項本身點擊事件

item 原布局檔案;

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="wrap_content"><Buttonandroid:id="@+id/array_button"android:layout_width="wrap_content" android:layout_height="wrap_content"    android:text="一個按鈕"  /><ImageView android:id="@+id/array_image"android:layout_toRightOf="@+id/array_button"android:layout_width="wrap_content" android:layout_height="fill_parent"android:layout_alignParentTop="true" android:layout_alignParentBottom="true"android:adjustViewBounds="true"android:padding="2dip" /><TextView android:id="@+id/array_title"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_toRightOf="@+id/array_image"android:layout_alignParentBottom="true"android:layout_alignParentRight="true" android:singleLine="true"android:ellipsize="marquee" android:textSize="15dip"  /><TextView android:id="@+id/array_text"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_toRightOf="@+id/array_image"android:layout_below="@+id/array_title"android:layout_alignParentBottom="true"android:layout_alignParentRight="true" android:singleLine="true"android:ellipsize="marquee" android:textSize="20dip" /></RelativeLayout>

重寫的adapter:

public View getView(final int position, View convertView, ViewGroup parent) {    ImageView iamge = null;    TextView title = null;    TextView text = null;    Button button = null;    if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(mTextViewResourceID, null);iamge = (ImageView) convertView.findViewById(R.id.array_image);title = (TextView) convertView.findViewById(R.id.array_title);text = (TextView) convertView.findViewById(R.id.array_text);button = (Button)convertView.findViewById(R.id.array_button);button.setOnClickListener(new OnClickListener() {        @Override    public void onClick(View arg0) {Toast.makeText(arrayList,"您點擊的第"+position +"個按鈕", Toast.LENGTH_LONG).show();    }});    }    }

解決方案:只要在item布局檔案中添加 android:descendantFocusability="blocksDescendants" 屬性,

在添加的控制項添加屬性

android:clickable="true"
 android:focusable="false"

更改後的item布局檔案內容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="wrap_content"android:descendantFocusability="blocksDescendants"><Buttonandroid:id="@+id/array_button"android:layout_width="wrap_content" android:layout_height="wrap_content"    android:text="一個按鈕"    android:clickable="true"    android:focusable="false"/><ImageView android:id="@+id/array_image"android:layout_toRightOf="@+id/array_button"android:layout_width="wrap_content" android:layout_height="fill_parent"android:layout_alignParentTop="true" android:layout_alignParentBottom="true"android:adjustViewBounds="true"android:padding="2dip" /><TextView android:id="@+id/array_title"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_toRightOf="@+id/array_image"android:layout_alignParentBottom="true"android:layout_alignParentRight="true" android:singleLine="true"android:ellipsize="marquee" android:textSize="15dip"  /><TextView android:id="@+id/array_text"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_toRightOf="@+id/array_image"android:layout_below="@+id/array_title"android:layout_alignParentBottom="true"android:layout_alignParentRight="true" android:singleLine="true"android:ellipsize="marquee" android:textSize="20dip" /></RelativeLayout>

問題二:如何再點擊item時擷取item內子物件,根據適當條件改變其屬性內容

例如單擊item選項後改變這條item背景色

解決方案:使用adapterView.getChildAt(index)方法

mListView.setOnItemClickListener(new OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> adapterView, View view, int position,    long id) {View v=adapterView.getChildAt(position);v.setBackgroundColor(Color.RED);    }});

看上去好像可以了,但是以上方法只是用於資料沒有滾動時即可見地區內

當資料量很大時,即開始滑動是再點擊會發生adapterView.getChildAt(index)擷取view為空白

這時就需要另一個方法adapterView.getFirstVisiblePosition();擷取第一個可見item位置,使用當前position-這個位置

具體實現View v=adapterView.getChildAt(pos-adapterView.getFirstVisiblePosition());

listview 中幾個實用的屬性:

首先是stackFromBottom屬性,這隻該屬性之後你做好的列表就會顯示你列表的最下面,值為true和false
android:stackFromBottom="true"             

第二是transciptMode屬性,需要用ListView或者其它顯示大量Items的控制項即時跟蹤或者查看資訊,並且希望最新的條目可以自動滾動到可視範圍內。通過設定的控制項transcriptMode屬性可以將Android平台的控制項(支援ScrollBar)自動滑動到最底部。
 android:transcriptMode="alwaysScroll"    

第三cacheColorHint屬性,很多人希望能夠改變一下它的背景,使他能夠符合整體的UI設計,改變背景背很簡單只需要準備一張圖片然後指定屬性 android:background="@drawable/bg",不過不要高興地太早,當你這麼做以後,發現背景是變了,但是當你拖動,或者點擊list空白位置的時候發現ListItem都變成黑色的了,破壞了整體效果。

如果你只是換背景的顏色的話,可以直接指定android:cacheColorHint為你所要的顏色,如果你是用圖片做背景的話,那也只要將android:cacheColorHint指定為透明(#00000000)就可以了

第四divider屬性,該屬性作用是每一項之間需要設定一個圖片做為間隔,或是去掉item之間的分割線
android:divider="@drawable/list_driver"  其中  @drawable/list_driver 是一個圖片資源,如果不想顯示分割線則只要設定為android:divider="@drawable/@null" 就可以了

第五fadingEdge屬性,上邊和下邊有黑色的陰影
android:fadingEdge="none" 設定後沒有陰影了~

 第五scrollbars屬性,作用是隱藏listView的捲軸,
android:scrollbars="none"與setVerticalScrollBarEnabled(true);的效果是一樣的,不活動的時候隱藏,活動的時候也隱藏

第六fadeScrollbars屬性,android:fadeScrollbars="true"  配置ListView布局的時候,設定這個屬性為true就可以實現捲軸的自動隱藏和顯示。

持續更新中。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.