关于BaseAdapter使用与优化
2016-04-03 本文已影响312人
阳翟后生
在activity_main.xml里定义一个ListVIew
<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
新建一个item.xml布局
在这个布局里,我们假设我们的每一个列表项有三个内容,一个缩略图,一个标题,一个概略内容.我们使用RelativeLayout,添加一个ImageView两个Textview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_tittle"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_toEndOf="@+id/iv_image"
android:text="tittle"
android:gravity="center"
android:textSize="25sp"/>
<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_tittle"
android:text="content"
android:gravity="center_vertical"
android:textSize="20sp"/>
</RelativeLayout>
新建一个ItemBeam对像,用于封装数据
public class ItemBean {
public int ItemImageResid;
public String ItemTittle;
public String ItemContent;
public ItemBean(int itemImageResid, String itemTittle, String itemContent) {
ItemImageResid = itemImageResid;
ItemTittle = itemTittle;
ItemContent = itemContent;
}
}
新建MyAdapter ,将MyAdapter extends BaseAdapter
在构造方法中初始化用于映射的数据List,重写方法
private List<ItemBean> mlist;
private LayoutInflater mInflater;
public MyAdapter(Context context,List<ItemBean> list){
mlist = list;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int position) {
return mlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
创建ViewHolder内部类,创建布局映射关系
class ViewHolder{
public ImageView imageView;
public TextView tittle;
public TextView content;
}
在getView中判断convertView是否为空
如果为空,则创建convertView,并设置tag,否则通过tag来取出ViewHolder
if (convertView == null){
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item,null);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_image);
viewHolder.content = (TextView) convertView.findViewById(R.id.tv_content);
viewHolder.tittle = (TextView) convertView.findViewById(R.id.tv_tittle);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
给viewH中的空间设置数据
ItemBean bean = mlist.get(position);
viewHolder.imageView.setImageResource(bean.ItemImageResid);
viewHolder.tittle.setText(bean.ItemTittle);
viewHolder.content.setText(bean.ItemContent);
完整的MyAdapter
public class MyAdapter extends BaseAdapter{
private List<ItemBean> mlist;
private LayoutInflater mInflater;
public MyAdapter(Context context,List<ItemBean> list){
mlist = list;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
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 position, View convertView, ViewGroup parent) {
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.content = (TextView) convertView.findViewById(R.id.tv_content);
viewHolder.tittle = (TextView) convertView.findViewById(R.id.tv_tittle);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
ItemBean bean = mlist.get(position);
viewHolder.imageView.setImageResource(bean.ItemImageResid);
viewHolder.tittle.setText(bean.ItemTittle);
viewHolder.content.setText(bean.ItemContent);
return convertView;
}
class ViewHolder{
public ImageView imageView;
public TextView tittle;
public TextView content;
}
}
MainActivity的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<ItemBean> itemBeanList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
itemBeanList.add(new ItemBean(
R.mipmap.ic_launcher,
"我是标题"+i,
"我是内容"+i
));
}
ListView listView = (ListView) findViewById(R.id.lv_main);
listView.setAdapter(new MyAdapter(this,itemBeanList));
}
}
总结
这样使用BaseAdapter为ListView添加数据充分利用了ListView的缓存机制,使得ListView内容加载时间大大缩减,推荐使用这种方法。
android新手,欢迎指正