标签:
学习自慕课网,先扔出一个listview布局(main)
<?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="vertical" > <ListView android:id="@+id/lv_main" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView> </LinearLayout>
然后新建一个item.xml,相当于一个实例,随便弄一个实例吧,反正后面用假想数据模拟一下。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/iv_image" android:layout_width="45dp" android:layout_height="60dp" android:src="@drawable/four" /> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="30dp" android:layout_toEndOf="@+id/iv_image" android:text="中津静流" android:gravity="center" android:textSize="20sp" /> <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="30dp" android:layout_toEndOf="@+id/iv_image" android:layout_below="@+id/tv_title" android:text="这是我的老婆" android:gravity="center_vertical" android:textSize="15sp" /> </RelativeLayout>
然后在MainActivity中创建假象以及实例化
package com.example.deemo; import java.util.ArrayList; import java.util.List; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.os.Build; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建假想数据 List<ItemBean> itemBeanList = new ArrayList<ItemBean>(); for (int i = 0; i < 20; i++) { itemBeanList.add(new ItemBean( R.drawable.ic_launcher, "i am title"+i, "i am content"+i )); } //实例化 ListView listView = (ListView) findViewById(R.id.lv_main); listView.setAdapter(new MyAdapter(this, itemBeanList)); } }
然后创建ItemBean实例化三控件
package com.example.deemo; public class ItemBean { public int ItemImageResid; public String Itemtitle; public String ItemContent; public ItemBean(int itemImageResid, String itemtitle, String itemContent) { ItemImageResid = itemImageResid; Itemtitle = itemtitle; ItemContent = itemContent; } }
然后在MyAdapter
package com.example.deemo; import java.util.List; 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.TextView; public class MyAdapter extends BaseAdapter{ private LayoutInflater mInflater; //关联数据源与适配器 private List<ItemBean> mList; public MyAdapter(Context context,List<ItemBean> list){ mList = list; mInflater = LayoutInflater.from(context);//将xml文件装换成view } @Override public int getCount() {//返回ListView数据量 return mList.size(); } //取出所对应的数据项 @Override public Object getItem(int position) { return mList.get(position); } //返回指定索引对应的数据项 @Override public long getItemId(int position) { return position; } //返回每一项内容 @Override public View getView(int positon, View convertView, ViewGroup parent) { //无缓存机制 /* View view = mInflater.inflate(R.layout.item, null);//需要装在到item中的文件 ImageView imageView = (ImageView) view.findViewById(R.id.iv_image); TextView title = (TextView) view.findViewById(R.id.tv_title); TextView context = (TextView) view.findViewById(R.id.tv_content); //取出数据,赋给控件 ItemBean bean = mList.get(positon); imageView.setImageResource(bean.ItemImageResid); title.setText(bean.Itemtitle); context.setText(bean.ItemContent); return view; */ //普通缓存,避免重复创建 /* if(convertView == null){//缓存池中是否有 convertView = mInflater.inflate(R.layout.item, null); } ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_image); TextView title = (TextView) convertView.findViewById(R.id.tv_title); TextView context = (TextView) convertView.findViewById(R.id.tv_content); //取出数据,赋给控件 ItemBean bean = mList.get(positon); imageView.setImageResource(bean.ItemImageResid); title.setText(bean.Itemtitle); context.setText(bean.ItemContent); return convertView; */ //比较好的优化,避免重复find id ViewHolder viewHolder; if(convertView == null){ viewHolder = new ViewHolder(); convertView = mInflater.inflate(R.layout.item, null); //将控件实例化到对象中 viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_image);//关联 viewHolder.title = (TextView) convertView.findViewById(R.id.tv_title); viewHolder.content = (TextView) convertView.findViewById(R.id.tv_content); convertView.setTag(viewHolder);//将ViewHolder与conventView绑定 }else {//通过viewholder对象找到控件 viewHolder = (ViewHolder) convertView.getTag(); } ItemBean bean = mList.get(positon); viewHolder.imageView.setImageResource(bean.ItemImageResid); viewHolder.title.setText(bean.Itemtitle); viewHolder.content.setText(bean.ItemContent); return convertView; } class ViewHolder{//对应布局控件 public ImageView imageView; public TextView title; public TextView content; } }
标签:
原文地址:http://www.cnblogs.com/webgavin/p/5740997.html