关闭

listview展现不同的布局内容

228人阅读 评论(0) 收藏 举报
分类:

listview展现不同的布局内容,关键在于重写适配器中的两个方法:

getItemViewType
getViewTypeCount

其中 getViewTypeCount 返回布局文件的种类
而getItemViewType是根据不同的条件来返回不同的布局文件,进行判断的地方.

demo实例:

1 定义常量,表示 种类与不同的类型:

    public static final int TYPE_COUNT = 2;
    public static final int TYPE_TEXT = 1;
    public static final int TYPE_IMG = 0;

2 复写方法:

    @Override
    public int getItemViewType(int position) {
        String type = mData.get(position).getType();
        if ("text".equals(type)) {
            return TYPE_TEXT;
        } else {
            return TYPE_IMG;
        }
    }

    /*
     * 返回总共几种类型
     */
    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return TYPE_COUNT;
    }

3 在getView方法中展现不同的内容,使用到缓存.

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewTextHolder holder1 = null;
        ViewImgHolder holder2 = null;

        currentType = getItemViewType(position);
        CommInfo domain = mData.get(position);

        if(convertView==null){
            switch (currentType) {
            case TYPE_TEXT:
                holder1=new ViewTextHolder();
                convertView=inflater.inflate(R.layout.firstpage_info_list_item, null);
                holder1.head=(TextView) convertView.findViewById(R.id.head);
                holder1.time=(TextView) convertView.findViewById(R.id.time);
                holder1.content=(TextView) convertView.findViewById(R.id.content);
                convertView.setTag(holder1);
                break;
            case TYPE_IMG:
              holder2=new ViewImgHolder();
              convertView=inflater.inflate(R.layout.firstpage_share_list_item,null);
              holder2.head=(TextView) convertView.findViewById(R.id.head);
              holder2.time=(TextView) convertView.findViewById(R.id.time);
              holder2.id_gallery=(LinearLayout) convertView.findViewById(R.id.id_gallery);
              convertView.setTag(holder2);
                break;
            }
        }else{
            switch(currentType){
            case  TYPE_TEXT:
                holder1=(ViewTextHolder) convertView.getTag();
                break;
            case TYPE_IMG:
                holder2=(ViewImgHolder) convertView.getTag();
                break;
            }
        }

        switch(currentType){
        case TYPE_TEXT:
            holder1.head.setText(domain.getTitle());
            holder1.time.setText(domain.getTime());
            holder1.content.setText(domain.getDescription());
            break;
        case TYPE_IMG:
            holder2.head.setText(domain.getTitle());
            holder2.time.setText(domain.getTime());
            for(int i=0;i<domain.getPaths().length;i++){
                View view = inflater.inflate(R.layout.activity_index_gallery_item,
                        holder2.id_gallery, false);
                ImageView img = (ImageView) view
                        .findViewById(R.id.id_index_gallery_item_image);
                img.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Log.v("qin", "我被点击了.");
                    }
                });
                utils.display(img, domain.getPaths()[i], config);
                holder2.id_gallery.addView(view);
            }
            break;
        }
        return convertView;
    }


    class ViewTextHolder {
        public TextView head;
        public TextView time;
        public TextView content;
    }

    class ViewImgHolder {
        public TextView head;
        public TextView time;
        public LinearLayout id_gallery;
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:101517次
    • 积分:1547
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:108篇
    • 译文:0篇
    • 评论:5条
    最新评论