ListView实现加载不同Item布局?

大家好,今天总结一片ListView加载不同Item布局的博客,在Android的app开发当中ListView貌似是基本配置,一般我们的ListView主要是作为列表类型的数据展示,普通的展示数据很好做,无非就是利用Volley、Xutils等等框架请求,然后Gosn或其他的解析,ImageLoader加载并显示图片,其实今天也用这些但是要做一些处理,网易新闻我想大部分人都看过,它的ListView展示数据的Item的布局有几种,而不是一种那么这种ListView展示的数据结构,是如何实现的呢,其实要实现这个效果有很多方法,比如:我们可以在adapter中通过type来加载不同的item布局,如果比较简单的话我们有时也可以利用ScrollView嵌套多个ListView或者GridView实现,还有就是通过Android5.0的中新的特性控件RecyclerView来轻松实现,这些方法我会在下面的博客当中讲解,今天主要是同过type来实现,通过type来实现加载不同Item的布局有个好处就是,条理清晰,不容易出错(上代码前给大家看个效果图)。下面就进入主题,上代码................

ListView实现加载不同Item布局?_第1张图片

 1 public class ListViewTypeActivity extends AppCompatActivity {
 2 
 3     private ListView listview;
 4     private Integer[]images2={R.drawable.appmain_subject_1,
                    R.drawable.appmain_subject_1,R.drawable.appmain_subject_1};
5 private Integer[]images={R.drawable.appmain_subject_1}; 6 public List<DataModel> listDatamodel = new ArrayList<DataModel>(); 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_list_view_type); 12 InitView(); 13 14 } 15 16 public void InitView() { 17 initData(); 18 listview = (ListView) findViewById(R.id.listview); 19 listview.setAdapter(new MyAdapter(listDatamodel,this)); 20 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 21 @Override 22 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 23 Toast.makeText(ListViewTypeActivity.this,
                             listDatamodel.get(position).getDmiaoshu()+"",Toast.LENGTH_SHORT).show(); 24 } 25 }); 26 } 27 28 /** 初始化数据 29 public void initData() { 30 for (int i = 0; i <1; i++) { 31 DataModel dml = new DataModel(images,"一个美女","一个美女"); 32 listDatamodel.add(dml); 33 } 34 for (int i = 0; i <1 ; i++) { 35 DataModel dml = new DataModel(images2,"三个美女","三个美女"); 36 listDatamodel.add(dml); 37 } 38 for (int i = 0; i <2 ; i++) { 39 DataModel dml = new DataModel(images,"一个美女","一个美女"); 40 listDatamodel.add(dml); 41 } 42 for (int i = 0; i <1 ; i++) { 43 DataModel dml = new DataModel(images2,"三个美女","三个美女"); 44 listDatamodel.add(dml); 45 } 46 for (int i = 0; i <3 ; i++) { 47 DataModel dml = new DataModel(images,"一个美女","一个美女"); 48 listDatamodel.add(dml); 49 } 50 for (int i = 0; i <1 ; i++) { 51 DataModel dml = new DataModel(images2,"三个美女","三个美女"); 52 listDatamodel.add(dml); 53 } 54 }

    Activity的代码非常简单,我就不讲解了,接下来我们来看核心的adapter的代码
  1 package com.example.joexiang.listviewtypedemo;
  2 import android.content.Context;
  3 import android.text.TextUtils;
  4 import android.view.LayoutInflater;
  5 import android.view.View;
  6 import android.view.ViewGroup;
  7 import android.widget.BaseAdapter;
  8 import android.widget.ImageView;
  9 import android.widget.TextView;
 10 import java.util.List;
 11 
 12 /**
 13  * Created by joe.xiang on 2016/2/15.
 14  */
 15 public class MyAdapter extends BaseAdapter {
 16     public LayoutInflater mInflater;
 17     public List<DataModel> listDatamodel;
 18     //加载布局类型
 19     public static final int LAYOUTONE=1;
 20     public static final int LAYOUTTWO=2;
 21 
 22     public MyAdapter(List<DataModel> listDatamodel,Context context) {
 23         this.listDatamodel = listDatamodel;
 24         mInflater = LayoutInflater.from(context);
 25     }
 26 
 27     @Override
 28     public int getCount() {
 29         return listDatamodel.size();
 30     }
 31 
 32 
 33     /**
 34      *  决定加载那个类型的Item布局
 35      * @param position
 36      * @return
 37      */
 38     @Override
 39     public int getItemViewType(int position) {
 40         DataModel dml= listDatamodel.get(position);
 41         String dtitle = dml.dtitle.trim();
 42         if(!TextUtils.isEmpty(dtitle)&&dtitle.equals("三个美女")){
 43                 return LAYOUTTWO;
 44         }else{
 45             return LAYOUTONE;
 46         }
 47     }
 48 
 49 
 50     /**
 51      * 
 52      * @return
 53      */
 54     @Override
 55     public int getViewTypeCount() {
 56         return 3;
 57     }
 58 
 59     @Override
 60     public Object getItem(int position) {
 61         return listDatamodel.get(position);
 62     }
 63 
 64     @Override
 65     public long getItemId(int position) {
 66         return position;
 67     }
 68 
 69     @Override
 70     public View getView(final int position, View convertView, ViewGroup parent) {
 71         //得到加载布局的类型
 72        int viewType =  getItemViewType(position);
 73          ViewHolder1 viewHolder1 = null;
 74           ViewHolder2 viewHolder2 = null;
 75         if (convertView==null){
          //根据返回类型加载不同的布局文件和创建不同的缓存类ViewHolder
76 switch (viewType){ 77 case 1: 78 convertView = mInflater.inflate(R.layout.content_list_view_type,null); 79 viewHolder1 = new ViewHolder1(); 80 viewHolder1.imageview = (ImageView) convertView.findViewById(R.id.image); 81 viewHolder1.tv = (TextView) convertView.findViewById(R.id.pname); 82 convertView.setTag(viewHolder1); 83 break; 84 case 2: 85 convertView = mInflater.inflate(R.layout.content_list_view_type2,null); 86 viewHolder2 = new ViewHolder2(); 87 viewHolder2.imageview2 = (ImageView) convertView.findViewById(R.id.image1); 88 viewHolder2.tv2 = (TextView) convertView.findViewById(R.id.pname2); 89 convertView.setTag(viewHolder2); 90 break; 91 } 92 }else{ 93 switch (viewType){ 94 case 1: 95 viewHolder1 = (ViewHolder1)convertView.getTag(); 96 break; 97 case 2: 98 viewHolder2 = (ViewHolder2)convertView.getTag(); 99 break; 100 } 101 }
//根据返回类型来展示不同的数据
102 switch (viewType){ 103 case 1: 104 Integer[] images = listDatamodel.get(position).getDimage(); 105 for (int i = 0; i <images.length ; i++) { 106 viewHolder1.imageview.setImageResource(images[i]); 107 } 108 viewHolder1.tv.setText(listDatamodel.get(position).getDmiaoshu()); 109 break; 110 case 2: 111 Integer[] images2 = listDatamodel.get(position).getDimage(); 112 for (int i = 0; i <images2.length ; i++) { 113 viewHolder2.imageview2.setImageResource(images2[i]); 114 } 115 viewHolder2.tv2.setText(listDatamodel.get(position).getDmiaoshu()); 116 break; 117 } 118 return convertView; 119 } 120 121 class ViewHolder1{ 122 TextView tv; 123 ImageView imageview; 124 } 125 class ViewHolder2{ 126 TextView tv2; 127 ImageView imageview2; 128 } 129 }
主要是通过GetItemViewType()来决定加载那一个布局,getViewTypeCount()返回显示Item的最布局数量,在getView()方法中通过getItemViewType()来获得当前加载的布局类型,在根据类型制定加载的布局,和相应的缓存类,设置值也是通过该返回类型来确定。咳咳到此本篇博客介绍完毕了,希望能帮到需要帮助的人。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(ListView实现加载不同Item布局?)