码迷,mamicode.com
首页 > 其他好文 > 详细

08_ListView使用2

时间:2014-10-21 21:16:51      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   io   os   ar   使用   

  实现带imageListView

bubuko.com,布布扣

package com.example.listviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class CustomListView extends Activity{
    
    private ListView lv;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_listview);
        
        lv = (ListView) findViewById(R.id.lv);
        lv.setAdapter(new CustomListViewAdapter(this));        
    }

}
package com.example.listviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomListViewAdapter extends BaseAdapter {

    private Context context = null;
    
    CustomListViewAdapter(Context context){
        this.context = context;
    }
    
    public Context getContext() {
        return context;
    }
    
    private CustomListCellData[] data = new CustomListCellData[]{
        new CustomListCellData("imag1", "dec 1", R.drawable.a),    
        new CustomListCellData("imag2", "dec 2", R.drawable.b),    
        new CustomListCellData("imag3", "dec 3", R.drawable.c)    
    };
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
//        TextView tv = null;
//        
//        //这样做的好处:防止重复创建TextView
//        //在有view被回收后,convertView就不是null
//        if (convertView != null) {
//            tv = (TextView) convertView;
//        }else {
//            tv = new TextView(CustomListView.this);
//        }
//        
//        tv.setTextSize(30);
//        tv.setText(getItem(position));
//        
//        return tv;
        
        LinearLayout ll = null;
        if (convertView != null) {
            ll = (LinearLayout) convertView;
        }else {
            ll = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.custom_listv_cell, null);
        }
        
        CustomListCellData data = getItem(position);
        
        ImageView icon = (ImageView) ll.findViewById(R.id.icon);
        TextView name = (TextView) ll.findViewById(R.id.name);
        TextView dec = (TextView) ll.findViewById(R.id.dec);
        
        icon.setImageResource(data.iconId);
        name.setText(data.name);
        dec.setText(data.dec);
        
        return ll;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public CustomListCellData getItem(int position) {
        return data[position];
    }
    
    @Override
    public int getCount() {
        return data.length;
    }

}
package com.example.listviewdemo;

public class CustomListCellData {

    public CustomListCellData(String name, String dec, int iconId) {
        this.name = name;
        this.dec = dec;
        this.iconId = iconId;
    }

    public String name = "";
    public String dec = "";
    public int iconId = 0;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <ImageView 
        android:layout_width="100dp" 
        android:layout_height="100dp"
        android:id="@+id/icon"/>

    <LinearLayout 
        android:layout_weight="1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/dec"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>
    
</LinearLayout>

 

 

08_ListView使用2

标签:android   style   blog   http   color   io   os   ar   使用   

原文地址:http://www.cnblogs.com/510602159-Yano/p/4041535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!