Android动态加载ListView中的Item【安卓进化二十三】

来源:互联网 发布:淘宝上的貂皮能买吗 编辑:程序博客网 时间:2024/06/08 10:42
    

 

        分类:            android开发                  2167人阅读     评论(5)    收藏    举报    
listviewandroidlayoutimageclassnull

           我这周上网看到动态增加listview的每一项item的布局,今天抽空自己写了一个,方便自己日后使用,这个效果还是很不错的,用到了Adapter的notifyDataSetChanged()方法,当点击每一个Item的时候,就通知adapter更新getView,系统得到通知就相应的加载一遍布局,就达到了动态加载item布局的效果。希望给大家带来点启迪,有问题或想要代码的可以留言,欢迎大家留言谈论listview的一些知识,以求共同进步,转载请标明出处:

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

 

                               程序开启界面,默认选中贝贝item,                   点击晶晶后的界面的效果,

                                           

 

                                      点击欢欢后界面的效果:                                      点击迎迎后界面的效果:

                                           

 

                                     点击妮妮后界面的效果:

                          

 

代码奉上,真理还得看代码:

在ListViewRunDemo工程中,

com.cn.daming包中的MainActivity.java类中的代码:

[java] view plaincopyprint?
  1. package com.cn.daming;  
  2.   
  3. import android.app.ListActivity;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.Gravity;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.AdapterView;  
  11. import android.widget.AdapterView.OnItemClickListener;  
  12. import android.widget.BaseAdapter;  
  13. import android.widget.ImageView;  
  14. import android.widget.LinearLayout;  
  15. import android.widget.TextView;  
  16.   
  17. public class MainActivity extends ListActivity {  
  18.   
  19.     private ListAddLayoutAdapter mAdapter;  
  20.     @SuppressWarnings("unused")  
  21.     private LayoutInflater lInflater;;  
  22.       
  23.     int[] image = {  
  24.             R.drawable.s_beibei,  
  25.             R.drawable.s_jingjing,  
  26.             R.drawable.s_huanhuan,  
  27.             R.drawable.s_yingying,  
  28.             R.drawable.s_nini  
  29.         };  
  30.       
  31.     String[] show_name = {  
  32.             "贝贝",  
  33.             "晶晶",  
  34.             "欢欢",  
  35.             "迎迎",  
  36.             "妮妮"  
  37.         };  
  38.       
  39.     private int id = 0;  
  40.     @SuppressWarnings("unused")  
  41.     private int last = 0;   
  42.       
  43.     @Override  
  44.     public void onCreate(Bundle savedInstanceState) {  
  45.         super.onCreate(savedInstanceState);  
  46.         mAdapter = new ListAddLayoutAdapter(this);  
  47.         this.setListAdapter(mAdapter);  
  48.          
  49.         this.getListView().setOnItemClickListener(new OnItemClickListener(){  
  50.             public void onItemClick(AdapterView<?> arg0, View arg1, int position,  
  51.                     long arg3) {  
  52.                 id = position;  
  53.                 mAdapter.notifyDataSetChanged();  
  54.                 last = position;  
  55.             }  
  56.         });  
  57.     }  
  58.       
  59.      public class ListAddLayoutAdapter extends BaseAdapter {  
  60.   
  61.         private Context context;  
  62.         private MainActivity activity;   
  63.           
  64.         public ListAddLayoutAdapter(Context context) {  
  65.             this.context = context;  
  66.             this.activity = (MainActivity)context;  
  67.             lInflater = activity.getLayoutInflater();  
  68.         }  
  69.            
  70.         public int getCount() {  
  71.             return image.length;  
  72.         }  
  73.   
  74.         public Object getItem(int arg0) {  
  75.             return null;  
  76.         }  
  77.   
  78.         public long getItemId(int position) {  
  79.             return position;  
  80.         }  
  81.   
  82.         public View getView(int position, View arg1, ViewGroup arg2) {  
  83.             LinearLayout layout = new LinearLayout(context);  
  84.             layout.setOrientation(LinearLayout.VERTICAL);  
  85.             layout.setPadding(0808);  
  86.               
  87.             layout.addView(addTitleView(position));  
  88.               
  89.             if(id==position){  
  90.                 layout.addView(addCustomView(position));  
  91.             }  
  92.               
  93.             return layout;  
  94.         }  
  95.           
  96.         public View addTitleView(int i){  
  97.             LinearLayout layout = new LinearLayout(context);  
  98.             layout.setOrientation(LinearLayout.HORIZONTAL);  
  99.               
  100.             ImageView iv = new ImageView(context);  
  101.             iv.setImageResource(image[i]);  
  102.             layout.addView(iv,  
  103.                     new LinearLayout.LayoutParams(  
  104.                             LinearLayout.LayoutParams.WRAP_CONTENT,   
  105.                             LinearLayout.LayoutParams.WRAP_CONTENT));  
  106.               
  107.               
  108.             TextView tv = new TextView(context);  
  109.             tv.setText(show_name[i]);  
  110.             tv.setTextSize(18f);  
  111.             layout.addView(tv,  
  112.                     new LinearLayout.LayoutParams(  
  113.                             LinearLayout.LayoutParams.WRAP_CONTENT,   
  114.                             LinearLayout.LayoutParams.WRAP_CONTENT));  
  115.               
  116.             layout.setGravity(Gravity.CENTER);  
  117.             return layout;  
  118.         }  
  119.           
  120.           
  121.         public View addCustomView(int i){  
  122.             View view = new View(context);  
  123.               
  124.             switch(i){  
  125.                 case 0:  
  126.                     ImageView iv1 = new ImageView(context);  
  127.                     iv1.setImageResource(R.drawable.beibei);  
  128.                     view = iv1;  
  129.                     break;  
  130.                       
  131.                 case 1:  
  132.                     ImageView iv2 = new ImageView(context);  
  133.                     iv2.setImageResource(R.drawable.jingjing);  
  134.                     view = iv2;  
  135.                     break;  
  136.                       
  137.                 case 2:  
  138.                     ImageView iv3= new ImageView(context);  
  139.                     iv3.setImageResource(R.drawable.huanhuan);  
  140.                     view = iv3;  
  141.                     break;  
  142.                 case 3:  
  143.                     ImageView iv4 = new ImageView(context);  
  144.                     iv4.setImageResource(R.drawable.yingying);  
  145.                     view = iv4;  
  146.                     break;  
  147.                 case 4:  
  148.                     ImageView iv5 = new ImageView(context);  
  149.                     iv5.setImageResource(R.drawable.nini);  
  150.                     view = iv5;  
  151.                     break;  
  152.             }  
  153.             return view;  
  154.          }  
  155.      }  
  156. }  
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()方法中这么写也是可以的:

[java] view plaincopyprint?
  1. <span style="font-size: 16px;">case 1:  
  2.     view = lInflater.inflate(R.layout.main, null);  
  3.     break;  
  4.   
  5. </span>  
case 1:    view = lInflater.inflate(R.layout.main, null);    break;

 

工程的图片没有上传,希望大家自己找些图片就可以了:

原创粉丝点击