import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class listViewTest extends Activity {
/** Called when the activity is first created. */
  ListView listView;
  MyAdapter listAdapter;
  ArrayList<String> listString;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    listView = (ListView)this.findViewById(R.id.listview);
    listString = new ArrayList<String>();
    for(int i = 0 ; i < 100 ; i++)
    {
      listString.add(Integer.toString(i));
    }
    listAdapter = new MyAdapter(this);
    listView.setAdapter(listAdapter);
  }

  class MyAdapter extends BaseAdapter{
    Context mContext;
    LinearLayout linearLayout = null;
    LayoutInflater inflater;
    TextView tex;
    final int VIEW_TYPE = 3;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;

    public MyAdapter(Context context) {
      // TODO Auto-generated constructor stub
      mContext = context;
      inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
      // TODO Auto-generated method stub
      return listString.size();
    }

    //每个convert view都会调用此方法,获得当前所需要的view样式
    @Override
    public int getItemViewType(int position) {
      // TODO Auto-generated method stub
      int p = position%6;
      if(p == 0)
        return TYPE_1;
      else if(p < 3)
        return TYPE_2;
      else if(p < 6)
        return TYPE_3;
      else
        return TYPE_1;
    }

    @Override
    public int getViewTypeCount() {
      // TODO Auto-generated method stub
      return 3;
    }

    @Override
    public Object getItem(int arg0) {
      // TODO Auto-generated method stub
      return listString.get(arg0);
    }

    @Override
    public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      viewHolder1 holder1 = null;
      viewHolder2 holder2 = null;
      viewHolder3 holder3 = null;
      int type = getItemViewType(position);

      //无convertView,需要new出各个控件
      if(convertView == null)
      {
        Log.e("convertView = ", " NULL");

        //按当前所需的样式,确定new的布局
        switch(type)
        {
        case TYPE_1:
          convertView = inflater.inflate(R.layout.listitem1, parent, false);
          holder1 = new viewHolder1();
          holder1.textView = (TextView)convertView.findViewById(R.id.textview1);
          holder1.checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);
          Log.e("convertView = ", "NULL TYPE_1");
          convertView.setTag(holder1);
          break;
        case TYPE_2:
          convertView = inflater.inflate(R.layout.listitem2, parent, false);
          holder2 = new viewHolder2();
          holder2.textView = (TextView)convertView.findViewById(R.id.textview2);
          Log.e("convertView = ", "NULL TYPE_2");
          convertView.setTag(holder2);
          break;
        case TYPE_3:
          convertView = inflater.inflate(R.layout.listitem3, parent, false);
          holder3 = new viewHolder3();
          holder3.textView = (TextView)convertView.findViewById(R.id.textview3);
          holder3.imageView = (ImageView)convertView.findViewById(R.id.imageview);
          Log.e("convertView = ", "NULL TYPE_3");
          convertView.setTag(holder3);
          break;
        }
      }else{
        //有convertView,按样式,取得不用的布局
        switch(type)
        {
        case TYPE_1:
          holder1 = (viewHolder1) convertView.getTag();
          Log.e("convertView !!!!!!= ", "NULL TYPE_1");
          break;
        case TYPE_2:
          holder2 = (viewHolder2) convertView.getTag();
          Log.e("convertView !!!!!!= ", "NULL TYPE_2");
          break;
        case TYPE_3:
          holder3 = (viewHolder3) convertView.getTag();
          Log.e("convertView !!!!!!= ", "NULL TYPE_3");
          break;
        }
      }

    //设置资源
      switch(type)
      {
        case TYPE_1:
          holder1.textView.setText(Integer.toString(position));
          holder1.checkBox.setChecked(true);
          break;
        case TYPE_2:
          holder2.textView.setText(Integer.toString(position));
          break;
        case TYPE_3:
          holder3.textView.setText(Integer.toString(position));
          holder3.imageView.setBackgroundResource(R.drawable.icon);
          break;
      }

      return convertView;
    }
  }

  //各个布局的控件资源
  class viewHolder1{
    CheckBox checkBox;
    TextView textView;
  }

  class viewHolder2{
    TextView textView;
  }

  class viewHolder3{
    ImageView imageView;
    TextView textView;
  }
}

Listview中显示不同的视图布局的更多相关文章

  1. Android开发-Listview中显示不同的视图布局

    1. 使用场景 在重写ListView的BaseAdapter时,我们常常在getView()方法中复用convertView,以提高性能.convertView在Item为单一的同种类型布局时,能够 ...

  2. Xcode在playground的quick look框中显示对象自定义视图

    对于一般对象,playground中默认的quick look显示已经够用,比如简单的字符串,Int,或简单的自定义Class等等. 不过对于有些情况,我们需要自定义对象在playground中的显示 ...

  3. Json文件放入Assets文件,读取解析并且放入listview中显示。

    package com.lixu.TestJson; import android.app.Activity; import android.content.Context; import andro ...

  4. Android ListView中添加不同的多种布局

    最近做项目要使用ListView加载不同的布局,由于自己写的代码不能贴出,故找了一篇自认为比较好的blog给分享出来,希望对用到此项技术的同学有点帮助. http://logc.at/2011/10/ ...

  5. 如何在自己的窗体(控件)中显示XAF的视图

    Form form = new Form(); DevExpress.ExpressApp.View listView = Application.CreateListView(Application ...

  6. 一个ListView中显示不同的item(分组)

    MainActivity: package com.zzw.qqgroup; import java.util.ArrayList; import java.util.HashMap; import ...

  7. YII总结学习7(在一个视图中显示另外一个视图)

    controller\hello <?php namespace app\controllers; use yii\web\Controller; class helloController e ...

  8. Android ListView中 每一项都有不同的布局

    实现代码 Adapter的代码 其中:ViewHolder分别是三个不同的布局,也就是ListView中每一项的布局 TYPE_1...是三种类型. 在使用不同布局的时候,getItemViewTyp ...

  9. C#在listview控件中显示数据库数据

    一.了解listview控件的属性 view:设置为details columns:设置列 items:设置行 1.将listview的view设置为details 2.设置列属性 点击添加,添加一列 ...

随机推荐

  1. Duiib 创建不规则窗口(转载)

    方法一: 转载:http://blog.csdn.net/chenlycly/article/details/46447297 转载:http://blog.csdn.net/harvic880925 ...

  2. MySQL INSERT插入条件判断:如果不存在则插入

    摘要: 我们经常需要进行sql的批量插入,要求:该条记录不存在则插入,存在则不插入.如果使用一条INSERT语句实现呢? 普通的 INSERT INTO 插入: INSERT INTO card(ca ...

  3. memcached安装

    memcached安装 一.安装gcc # yum -y install gcc 二.安装libevent # wget http://www.monkey.org/~provos/libevent- ...

  4. .Net环境下的缓存技术介绍

    .Net环境下的缓存技术介绍 摘要: 介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1         概念 1.1 ...

  5. RealSense开发-Session和SenseManager的几种创建方法

    从Intel RealSense 的SDK文档对其架构(如图1所示)的始描述可知,Session是SDK应用的主控模块,必须在所有模块操作之前创建,并且在所有模块注销后最后注销.SenseManage ...

  6. oracle中trim,ltrim,rtrim函数用法

    该函数共有两种作用:第一种,即大家都比较熟悉的去除空格.例子:--TRIM去除指定字符的前后空格SQL> SELECT TRIM(' dd df ') FROM dual;TRIM('DDDF' ...

  7. 程序设计入门——C语言 第6周编程练习 1 分解质因数(5分)

    1 分解质因数(5分) 题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数.比如,6可以被分解为2x3,而24可以被分解为2x2x2x3. ...

  8. c++程序判断系统是Linux还是Windows

    用C++来实现,本来想了很多,后来越写越烂,而且结果总是不尽人意,干脆这样子好了: int main() { int judge = system("cls"); ) cout & ...

  9. iOS适配https详解

    马上就要元旦了,网上流传元旦之后苹果会对所有的app进行https的验证,据说会拒绝所有没有使用https的app.但是后来又听说是我们开发者误解了,元旦过后还是会支持http,不过开发者需要说明为什 ...

  10. wmware 怎么 跟主机相互通信

    VMnet1和VMware8其实就是软件模拟出来的两块网卡提供DHCP服务,两块网卡对应VMware的两种不同的模式VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转 ...