1.listView就是列表形式的View,展示形式: 数据+适配器
常见的自带ArrayAdapter,SimpleAdapter等等,但是大多情况都是自己自定义.
自定义listview的适配器
Item项 icon_item.xml
<?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" >
<RelativeLayout
android:id="@+id/rl_item_all"
android:layout_width="match_parent"
android:layout_height="144dp"
android:background="#ffffff"
android:gravity="center_vertical"
>
<ImageView
android:id="@+id/iv_item_icon"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="24dp"
android:background="@drawable/head_man"
/>
<TextView
android:id="@+id/tv_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft=182dp"
android:text="奥巴马"
android:textSize="38dp"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</LinearLayout
public class IconShowAdapter extends BaseAdapter
{
private Context context;
//Entity为实体对象,填充listView数据
private List<Entity> list=new ArrayList<Entity>();
public IconShowAdapter(Context context)
{
this.context=context;
}
//确定listView中显示的数据条目
@Overide
public int getCount()
{
return list.Size();
}
@Overide
public Object getItem(int position)
{
return list.get(position);
}
@Overide
public long getItem(int position)
{
return position;
}
//使用内部类ViewHolder构建临时View,重中View,减少刷新listView数据所占内存
//当然ViewHoler也设为static
@Overide
public View getView(int position ,View convertView,ViewGroup parent)
{
View view=null;
ViewHolder holder=null;
final Entity entity=list.get(position);
if(convertView==null)
{
view=LayoutInflater.from(context).inflate(R.id.icon_item.xml);
holder=new ViewHolder();
holder.iv_icon=(ImageView)view.findViewById(R.id.iv_item_icon);
holder.tv_name=(TextView)view.findViewById(R.id.tv_item_name);
view.setTag(holder);
}
else
{
view=convertView.
holder=(Viewholder)view.getTag();
}
holder.iv_icon.setBackgroundResource(R.drawble.head_man);
holder.tv_name.setText(entity.getName());
return view;
}
public class ViewHolder
{
ImageView iv_icon;
TextView tv_name;
}
//用户刷新listView数据
public void flushData(List<Entity> list)
{
if(list!=null)
{
this.list=list;
this.notifyDataSetChanged();
}
}
}