Android ListView 自訂Adapter 並最佳化UI

來源:互聯網
上載者:User

本文地址:http://blog.csdn.net/csheng1204/article/details/7233910 轉載請註明,謝謝~~

其實網上挺多這種例子了,但還是自己寫個吧,當是複習使用方法。

可參考配合ListActivity使用方法,如下文:

ListActivity使用及ListView的setEmptyView

 

以下Adapter存在static class ViewHolder的意義在於,getView方法是在UI線程啟動並執行,為了不阻塞UI,造成卡的感覺,必須做一些最佳化,關於getView最佳化的,想知道更多,請合理利用搜尋引擎~~

ListViewAdapter.java

package com.csheng.asynctask;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class ListViewAdapter extends BaseAdapter {    public static final String TAG = "ListViewAdapter";    private List<String> strs = new ArrayList<String>();    private Context mContext = null;    public ListViewAdapter(Context context) {        mContext = context;        for (int i = 0; i < 10; i++) {            strs.add("Test String " + i);        }    }    @Override    public int getCount() {        if (!strs.isEmpty()) {            return strs.size();        } else {            return 0;        }    }    @Override    public Object getItem(int position) {        if (!strs.isEmpty()) {            return strs.get(position);        } else {            return null;        }    }    @Override    public long getItemId(int position) {        return position;    }    static class ViewHolder {        ImageView logoIv;        TextView textTv;    }    /**     * 在UI線程運行,因此必須減少非必要的new,只做取資料,設定資料操作     * 自訂Adapter最重要的部分,使用static class ViewHolder來減少new對象     */    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder holder = null;        if (convertView == null) {            convertView = LayoutInflater.from(mContext).inflate(                    R.layout.list_item, null);            holder = new ViewHolder();            holder.logoIv = (ImageView) convertView.findViewById(R.id.logoIv);            holder.textTv = (TextView) convertView.findViewById(R.id.textTv);            convertView.setTag(holder);        } else {            holder = (ViewHolder) convertView.getTag();        }        final String string = strs.get(position);// 從strs資料來源取出對應位置的string        /* 自訂設定,例如可設定圖片,設定文字等 */        holder.textTv.setText(string);        return convertView;    }}

list_item.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView        android:id="@+id/logoIv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/ic_launcher" />    <TextView        android:id="@+id/textTv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@+id/logoIv" /></RelativeLayout>

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.