本帖最后由 liu 于 2018-12-13 11:11 编辑
BaseAdapter的使用
相比较于ArrayAdapter和SimpleAdapter,使用BaseAdapter会更加的灵活,使用时先自定义一个MyAdapter(名字随便起)继承BaseAdapter,并实现其中的方法
举例:
先写主布局,就放入一个ListView
activity_main.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
接下来写item布局,用来显示每条数据的样式
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
创建实体类
实体类:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
/**
* Created by Administrator on 2018/12/13.
*/
public class WeaponBean {
public int img; //图片
public String name; //名字
public WeaponBean(int img, String name) {
this.img = img;
this.name = name;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后写适配器,用于绑定数据
MyAdapter:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
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;
import java.util.List;
/**
* Created by Administrator on 2018/12/13.
*/
public class MyAdapter extends BaseAdapter {
private Context context; //上下文对象
private List<WeaponBean> list; //数据
/**
* @param context 上下文对象
* @param list 要绑定的数据
*/
public MyAdapter(Context context, List<WeaponBean> list) {
this.context = context;
this.list = list;
}
//返回数据的总数
@Override
public int getCount() {
return list.size();
}
//返回 在list中指定位置的数据内容
@Override
public Object getItem(int position) {
return list.get(position);
}
//返回数据在list中的位置
@Override
public long getItemId(int position) {
return position;
}
//设置ListView每一行的布局和数据绑定
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder;
//判断convertView,为空则创建,并设置tag,不为空则通过tag取出ViewHolder,减少findViewById次数,达到优化的目的
if (convertView == null) {
holder = new ViewHolder();
//将布局文件转化为View对象
convertView = LayoutInflater.from(context).inflate(R.layout.item_weapon, null);
//初始化控件
holder.iv = (ImageView) convertView.findViewById(R.id.iv);
holder.tv = (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//将数据绑定到指定控件
holder.iv.setImageResource(list.get(position).img);
holder.tv.setText(list.get(position).name);
return convertView;
}
//用于缓存控件
public class ViewHolder {
private ImageView iv;
private TextView tv;
}
}
最后主页面中给ListView设置适配器
MainActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
String[] data = {"M416", "AWM", "M24", "AK47", "平底锅"};
private ListView mListView;
private List<WeaponBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addData();
initView();
}
//创建数据
private void addData() {
list = new ArrayList<>();
WeaponBean weaponBean = new WeaponBean(R.drawable.m4, "M416");
WeaponBean weaponBean2 = new WeaponBean(R.drawable.awm, "AWM");
WeaponBean weaponBean3 = new WeaponBean(R.drawable.m24, "M24");
WeaponBean weaponBean4 = new WeaponBean(R.drawable.ak, "AK47");
WeaponBean weaponBean5 = new WeaponBean(R.drawable.pdg, "神器平底锅");
list.add(weaponBean);
list.add(weaponBean2);
list.add(weaponBean3);
list.add(weaponBean4);
list.add(weaponBean5);
}
private void initView() {
mListView = (ListView) findViewById(R.id.lv);
MyAdapter adapter = new MyAdapter(this, list);
//listview设置适配器
mListView.setAdapter(adapter);
}
}
看下效果:
|