新建一个activity,命名为 BaseAdapterActivity,这里以Spinner为载体,演示 BaseAdapter 的用法
public class BaseAdapterActivity extends AppCompatActivity {
private ArrayList<Planet> planetList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_adapter);
initPlanetSpinner();
}
private void initPlanetSpinner() {
planetList = Planet.getDefaultList();
PlanetListAdapter adapter = new PlanetListAdapter(this, planetList);
Spinner sp = findViewById(R.id.sp_planet);
sp.setPrompt("请选择行星");
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new MySelectedListener());
}
private class MySelectedListener implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ToastUtil.showToast(BaseAdapterActivity.this, "您选择的是" + planetList.get(position).name);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
}
BaseAdapterActivity 对应的xml文件为:activity_base_adapter 如下:
<?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:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="行星的列表视图"
android:textColor="@color/black"
android:textSize="20sp" />
<Spinner
android:id="@+id/sp_planet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:spinnerMode="dialog" />
</LinearLayout>
编写新的适配器 PlanetListAdapter 继承于 BaseAdapter,实现对列表项的获取与操作,如下:
public class PlanetListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Planet> mPlanetList;
public PlanetListAdapter(Context context, ArrayList<Planet> arrayList) {
mContext = context;
mPlanetList = arrayList;
}
@Override
public int getCount() {
return mPlanetList.size();
}
@Override
public Object getItem(int position) {
return mPlanetList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);
holder.iv_icon = convertView.findViewById(R.id.iv_icon);
holder.tv_name = convertView.findViewById(R.id.tv_name);
holder.tv_desc = convertView.findViewById(R.id.tv_desc);
convertView.setTag(holder); // 将视图持有者保存到转换视图中
} else {
holder = (ViewHolder)convertView.getTag();
}
Planet info = mPlanetList.get(position);
holder.iv_icon.setImageResource(info.image);
holder.tv_name.setText(info.name);
holder.tv_desc.setText(info.desc);
return convertView;
}
public final class ViewHolder {
public ImageView iv_icon;
public TextView tv_name;
public TextView tv_desc;
}
}
PlanetListAdapter 中对应的布局文件为 item_list ,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="@color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_height="100dp">
<ImageView
android:id="@+id/iv_icon"
android:layout_marginLeft="5dp"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:scaleType="fitCenter" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_marginLeft="5dp"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:textColor="@color/black" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_height="0dp"
android:layout_weight="2"
android:textColor="@color/gray"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
PlanetListAdapter 中对应的模型为Planet 如下:
public class Planet {
public int image;
public String name;
public String desc;
public Planet(int image, String name, String desc) {
this.image = image;
this.name = name;
this.desc = desc;
}
private static int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
private static String[] nameArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
private static String[] descArray = {
"水星是太阳系八大行星最内侧也是最小的一颗行星,也是离太阳最近的行星",
"金星是太阳系八大行星之一,排行第二,距离太阳0.725天文单位",
"地球是太阳系八大行星之一,排行第三,也是太阳系中直径、质量和密度最大的类地行星,距离太阳1.5亿公里",
"火星是太阳系八大行星之一,排行第四,属于类地行星,直径约为地球的53%",
"木星是太阳系八大行星中体积最大、自转最快的行星,排行第五。它的质量为太阳的千分之一,但为太阳系中其它七大行星质量总和的2.5倍",
"土星为太阳系八大行星之一,排行第六,体积仅次于木星"
};
public static ArrayList<Planet> getDefaultList() {
ArrayList<Planet> planetList = new ArrayList<Planet>();
for (int i = 0; i < nameArray.length; i++) {
planetList.add(new Planet(iconArray[i], nameArray[i], descArray[i]));
}
return planetList;
}
}
效果图如下
- 左边的图是BaseAdapterActivity的显示效果
- 右边的图是PlanetListAdapter的显示效果