Android控制項之ListView

來源:互聯網
上載者:User


          ListView是Android中最常用的控制項之一. ListView中一個重要的概念就是適配器(Adapter),它是控制項與資料來源之間的橋樑.


常見的資料來源有數組/集合(Array/List),遊標(Cursor).


        1,  從數組中擷取資料:


         運行結果:



        2,  從Cursor中擷取資料:

        // Get a cursor with all people        Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);                startManagingCursor(c);//使用的cursor的生命週期隨Activity生命週期變化而變化.        ListAdapter adapter = new SimpleCursorAdapter(this,                 // Use a template that displays a text view                android.R.layout.simple_list_item_1,                 // Give the cursor to the list adatper                c,                 // Map the NAME column in the people database to...                new String[] {People.NAME} ,                // The "text1" view defined in the XML template                new int[] {android.R.id.text1});         setListAdapter(adapter);


運行結果:

            3, 使用自訂Adapter


private class SpeechListAdapter extends BaseAdapter {        public SpeechListAdapter(Context context) {            mContext = context;        }        public int getCount() {            return mTitles.length;        }        public Object getItem(int position) {//每個條目對應的對象            return position;        }        public long getItemId(int position) {//每個條目的id            return position;        }        public View getView(int position, View convertView, ViewGroup parent) {//ListView每個item對應的view            SpeechView sv;            if (convertView == null) {                sv = new SpeechView(mContext, mTitles[position],                        mDialogue[position]);            } else {                sv = (SpeechView) convertView;                sv.setTitle(mTitles[position]);                sv.setDialogue(mDialogue[position]);            }            return sv;        }}

       結果:

一些ListView效果


                       
1,  帶有分隔字元的ListView.

效果所所示(紅框表示的是分隔條):


    private class MyListAdapter extends BaseAdapter {        public MyListAdapter(Context context) {            mContext = context;        }        public int getCount() {            return mStrings.length;        }        @Override        public boolean areAllItemsEnabled() {//是否所有的item都可點擊和選中            return false;        }        @Override        public boolean isEnabled(int position) {//如果字串是以"-"開頭,那麼對應的item為分隔item            return !mStrings[position].startsWith("-");        }                        public Object getItem(int position) {            return position;        }        public long getItemId(int position) {            return position;        }        public View getView(int position, View convertView, ViewGroup parent) {            TextView tv;            if (convertView == null) {                tv = (TextView) LayoutInflater.from(mContext).inflate(                        android.R.layout.simple_expandable_list_item_1, parent, false);            } else {                tv = (TextView) convertView;            }            tv.setText(mStrings[position]);            return tv;        }        private Context mContext;    }

                          2, 帶收縮和展開效果的ListView

效果如所示:



              

                3 ,類似帶懸浮字母效果的ListView(類似通訊錄導航的效果 )         

如所示




下面來示範notifyDataSetChanged()方法的作用:

每次添加圖片的時候都notifyDataSetChanged(),也就是資料來源改變了執行notifyDataSetChanged(),可以重新整理介面.




                  ListView效率問題.

我們知道一個item的顯示都會調用getView()方法,並且在不停的滑動時候也會不停的調用.

下面來看一下消極式載入的效果:在不停滑動的時候用"Loading"作為顯示文本,當停止滑動顯示真實的文本.



            最佳化Adapter,提高ListView執行效率.

public View getView(int position, View convertView, ViewGroup parent) {            // A ViewHolder keeps references to children views to avoid unneccessary calls            // to findViewById() on each row.            ViewHolder holder;            // When convertView is not null, we can reuse it directly, there is no need            // to reinflate it. We only inflate a new View when the convertView supplied            // by ListView is null.            if (convertView == null) {//當有老的item view的時候借用老的view,這樣避免每次都去解析xml                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);                // Creates a ViewHolder and store references to the two children views                // we want to bind data to.                holder = new ViewHolder();                holder.text = (TextView) convertView.findViewById(R.id.text);                holder.icon = (ImageView) convertView.findViewById(R.id.icon);                convertView.setTag(holder);            } else {                // Get the ViewHolder back to get fast access to the TextView                // and the ImageView.                holder = (ViewHolder) convertView.getTag();            }            // Bind the data efficiently with the holder.            holder.text.setText(DATA[position]);            holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);            return convertView;        }        static class ViewHolder {            TextView text;            ImageView icon;        } 

這樣做的好處有:

            1>重用了 convertView,避免當不需要解析xml檔案的時候去解析xml,填充視圖.

            2>使用ViewHolder避免當不需要的時候調用findViewById()方法.



  歡迎轉載,http://blog.csdn.net/johnny901114/article/details/7867934

























相關文章

聯繫我們

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