QQlistview展示

public class MainActivity extends Activity {
        ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(MainActivity.this);
        ImageLoader.getInstance().init(configuration);
        
        //初始化控件
        lv = (ListView)findViewById(R.id.lv);
        DataBean bean = new DataBean();
        List<DataBean.ItemBean>list = new ArrayList<DataBean.ItemBean>();
        for(int i =0;i<10;i++){
            if(i<3){
            DataBean.mssageBean msg = new DataBean().new mssageBean("http://www.bkill.com/u/info_img/2012-09/02/2012083116140522302.jpg", "张无忌", "让我找他"+i);
            DataBean.ItemBean itemBean = new DataBean().new ItemBean("right", msg);
            list.add(itemBean);
            }else if(i%2==0){
            DataBean.TimeBean timeBean = new DataBean().new TimeBean("8:48"+i);
            DataBean.ItemBean itemBean = new DataBean().new ItemBean("time", timeBean);
            list.add(itemBean);
            }else{
                DataBean.mssageBean mssageBean = new DataBean().new mssageBean("http://img1.touxiang.cn/uploads/20120902/02-055331_356.jpg","赵敏","我知道了"+i);
                DataBean.ItemBean itemBean = new DataBean().new ItemBean("letf", mssageBean);
                list.add(itemBean);
            }
        }
        MyAdapter adapter = new MyAdapter(MainActivity.this, list);
        lv.setAdapter(adapter);
        

    }









适配器

public class MyAdapter extends BaseAdapter{
    Context context;
    List<DataBean.ItemBean>list = new ArrayList<DataBean.ItemBean>();
    //加载不同布局对应的类型,自定义
    final int type_right = 0;
    final int type_letf = 1;
    final int type_time = 2;
    ImageLoader loader;
    DisplayImageOptions options;

    public MyAdapter(Context context, List<ItemBean> list) {
        super();
        this.context = context;
        this.list = list;
        options = new DisplayImageOptions.Builder()
        .cacheInMemory(true)
        .cacheOnDisc(true)
        .build();
        loader = ImageLoader.getInstance();
    }
    

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

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public int getItemViewType(int position) {
        //从list中取出对应position加载的数据
        DataBean.ItemBean itemBean = list.get(position);
        //根据数据类型,返回不同的type
        if(itemBean.tag.equals("right")){
            return type_right;
        }
        if (itemBean.tag.equals("left")) {
            return type_letf;
        }
        if(itemBean.tag.equals("time")){
            return type_time;
        }
        return 0;
    }
    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 3;
    }
    
    
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //********第一步:初始化布局;
        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;
        ViewHolder3 holder3 = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case type_letf:
                    convertView = View.inflate(context, R.layout.letf_item, null);
                    holder1 = new ViewHolder1(convertView);
                    convertView.setTag(holder1);
                    break;
                case type_right:
                    convertView = View.inflate(context, R.layout.right_item, null);
                    holder2 = new ViewHolder2(convertView);
                    convertView.setTag(holder2);
                    break;
                case type_time:
                    convertView = View.inflate(context, R.layout.item, null);
                    holder3 = new ViewHolder3(convertView);
                    convertView.setTag(holder3);
                    break;
                default:
                    break;
            }
        } else {
            switch (type) {
                case type_letf:
                    holder1 = (ViewHolder1) convertView.getTag();
                    break;
                case type_right:
                    holder2 = (ViewHolder2) convertView.getTag();
                    break;
                case type_time:
                    holder3 = (ViewHolder3) convertView.getTag();
                    break;
                default:
                    break;
            }
        }

        //********第二步:给布局赋值,显示数据;
        DataBean.ItemBean itemBean = list.get(position);
        switch (type) {
            case type_letf:
                DataBean.mssageBean left = (DataBean.mssageBean) itemBean.data;
                loader.displayImage(left.photo, holder1.left_item_img, options);
                holder1.left_item_name.setText(left.name);
                holder1.left_item_msg.setText(left.msg);
                break;
            case type_right:
                DataBean.mssageBean right = (DataBean.mssageBean) itemBean.data;
                loader.displayImage(right.photo, holder2.left_item_img, options);
                holder2.left_item_name.setText(right.name);
                holder2.left_item_msg.setText(right.msg);
                break;
            case type_time:
                DataBean.TimeBean time = (DataBean.TimeBean) itemBean.data;
                holder3.left_item_name.setText(time.time);
                break;
            default:
                break;
        }
        return convertView;
    
    }
      public static class ViewHolder1 {
            public View rootView;
            public ImageView left_item_img;
            public TextView left_item_name;
            public TextView left_item_msg;

            public ViewHolder1(View rootView) {
                this.rootView = rootView;
                this.left_item_img = (ImageView) rootView.findViewById(R.id.left_item_img);
                this.left_item_name = (TextView) rootView.findViewById(R.id.left_item_name);
                this.left_item_msg = (TextView) rootView.findViewById(R.id.left_item_msg);
            }

        }

        class ViewHolder2 {
            public View rootView;
            public TextView left_item_msg;
            public TextView left_item_name;
            public ImageView left_item_img;

            public ViewHolder2(View rootView) {
                this.rootView = rootView;
                this.left_item_msg = (TextView) rootView.findViewById(R.id.left_item_msg);
                this.left_item_name = (TextView) rootView.findViewById(R.id.left_item_name);
                this.left_item_img = (ImageView) rootView.findViewById(R.id.left_item_img);
            }

        }

        public static class ViewHolder3 {
            public View rootView;
            public TextView left_item_name;

            public ViewHolder3(View rootView) {
                this.rootView = rootView;
                this.left_item_name = (TextView) rootView.findViewById(R.id.left_item_name);
            }

        }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值