Android動態載入ListView中的Item【安卓進化二十三】

來源:互聯網
上載者:User

           我這周上網看到動態增加listview的每一項item的布局,今天抽空自己寫了一個,方便自己日後使用,這個效果還是很不錯的,用到了Adapter的notifyDataSetChanged()方法,當點擊每一個Item的時候,就通知adapter更新getView,系統得到通知就相應的載入一遍布局,就達到了動態載入item布局的效果。希望給大家帶來點啟迪,有問題或想要代碼的可以留言,歡迎大家留言談論listview的一些知識,以求共同進步,轉載請標明出處:

http://blog.csdn.net/wdaming1986/article/details/6787455

 

                               程式開啟介面,預設選中貝貝item,                   點擊晶晶後的介面的效果,

                                           

 

                                      點擊歡歡後介面的效果:                                      點擊迎迎後介面的效果:

                                           

 

                                     點擊妮妮後介面的效果:

                          

 

代碼奉上,真理還得看代碼:

在ListViewRunDemo工程中,

com.cn.daming包中的MainActivity.java類中的代碼:

package com.cn.daming;import android.app.ListActivity;import android.content.Context;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends ListActivity {private ListAddLayoutAdapter mAdapter;@SuppressWarnings("unused")private LayoutInflater lInflater;;int[] image = {R.drawable.s_beibei,R.drawable.s_jingjing,R.drawable.s_huanhuan,R.drawable.s_yingying,R.drawable.s_nini};String[] show_name = {"貝貝","晶晶","歡歡","迎迎","妮妮"};private int id = 0;@SuppressWarnings("unused")private int last = 0; @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mAdapter = new ListAddLayoutAdapter(this);        this.setListAdapter(mAdapter);               this.getListView().setOnItemClickListener(new OnItemClickListener(){public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {id = position;mAdapter.notifyDataSetChanged();last = position;}        });    } public class ListAddLayoutAdapter extends BaseAdapter {private Context context;private MainActivity activity; public ListAddLayoutAdapter(Context context) {this.context = context;this.activity = (MainActivity)context;lInflater = activity.getLayoutInflater();} public int getCount() {return image.length;}public Object getItem(int arg0) {return null;}public long getItemId(int position) {return position;}public View getView(int position, View arg1, ViewGroup arg2) {LinearLayout layout = new LinearLayout(context);        layout.setOrientation(LinearLayout.VERTICAL);        layout.setPadding(0, 8, 0, 8);                layout.addView(addTitleView(position));                if(id==position){        layout.addView(addCustomView(position));        }        return layout;}public View addTitleView(int i){LinearLayout layout = new LinearLayout(context);        layout.setOrientation(LinearLayout.HORIZONTAL);                ImageView iv = new ImageView(context);        iv.setImageResource(image[i]);        layout.addView(iv,        new LinearLayout.LayoutParams(        LinearLayout.LayoutParams.WRAP_CONTENT,         LinearLayout.LayoutParams.WRAP_CONTENT));                        TextView tv = new TextView(context);        tv.setText(show_name[i]);        tv.setTextSize(18f);        layout.addView(tv,        new LinearLayout.LayoutParams(        LinearLayout.LayoutParams.WRAP_CONTENT,         LinearLayout.LayoutParams.WRAP_CONTENT));                layout.setGravity(Gravity.CENTER);        return layout;}public View addCustomView(int i){View view = new View(context);switch(i){case 0:ImageView iv1 = new ImageView(context);iv1.setImageResource(R.drawable.beibei);view = iv1;break;case 1:ImageView iv2 = new ImageView(context);iv2.setImageResource(R.drawable.jingjing);view = iv2;break;case 2:ImageView iv3= new ImageView(context);iv3.setImageResource(R.drawable.huanhuan);view = iv3;break;case 3:ImageView iv4 = new ImageView(context);iv4.setImageResource(R.drawable.yingying);view = iv4;break;case 4:ImageView iv5 = new ImageView(context);iv5.setImageResource(R.drawable.nini);view = iv5;break;    }return view; } }}

 

說明:也可以動態載入布局檔案也可以的:

在addCustomView()方法中這麼寫也是可以的:

case 1:    view = lInflater.inflate(R.layout.main, null);    break;

 

工程的圖片沒有上傳,希望大家自己找些圖片就可以了:

聯繫我們

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