一、BaseAdapter+ListView
1、思维导图
2、详细代码
(1)listview_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/fruitImg"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/strawberry" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/fruitName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="草莓"
android:textSize="18sp" />
<TextView
android:id="@+id/fruitPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15元/KG"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
(2)ListViewAdapter
public class ListViewAdapter extends BaseAdapter {
private List<Fruit> mList;
private Context mContext;
private ViewHolder viewHolder;
public ListViewAdapter(List<Fruit> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
}
@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 = new ViewHolder();
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, parent, false);
viewHolder.imageView = convertView.findViewById(R.id.fruitImg);
viewHolder.textName = convertView.findViewById(R.id.fruitName);
viewHolder.textPrice = convertView.findViewById(R.id.fruitPrice);
// 缓存各个控件的实例
convertView.setTag(viewHolder);
} else {
// 取出缓存
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageResource(mList.get(position).getImage());
viewHolder.textName.setText(mList.get(position).getName());
viewHolder.textPrice.setText(mList.get(position).getPrice());
return convertView;
}
// 内部类
static class ViewHolder {
ImageView imageView;
TextView textName;
TextView textPrice;
}
}
(3)fragment_home.xml
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
(4)HomeFragment 类
public class HomeFragment extends Fragment {
private ListView listView;
private ListViewAdapter adapter;
private List<Fruit> mList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
listView = view.findViewById(R.id.listView);
initFruit();
adapter = new ListViewAdapter(mList, getContext());
listView.setAdapter(adapter);
return view;
}
private void initFruit() {
mList = new ArrayList<Fruit>();
mList.add(new Fruit(R.drawable.strawberry, "草莓", "15元/KG"));
mList.add(new Fruit(R.drawable.cherry, "樱桃", "18元/KG"));
mList.add(new Fruit(R.drawable.watermelon, "西瓜", "6元/KG"));
mList.add(new Fruit(R.drawable.apple, "苹果", "10元/KG"));
mList.add(new Fruit(R.drawable.litchi, "荔枝", "9.9元/KG"));
mList.add(new Fruit(R.drawable.strawberry, "草莓", "15元/KG"));
mList.add(new Fruit(R.drawable.cherry, "樱桃", "18元/KG"));
mList.add(new Fruit(R.drawable.watermelon, "西瓜", "6元/KG"));
mList.add(new Fruit(R.drawable.apple, "苹果", "10元/KG"));
mList.add(new Fruit(R.drawable.litchi, "荔枝", "9.9元/KG"));
mList.add(new Fruit(R.drawable.strawberry, "草莓", "15元/KG"));
mList.add(new Fruit(R.drawable.cherry, "樱桃", "18元/KG"));
mList.add(new Fruit(R.drawable.watermelon, "西瓜", "6元/KG"));
mList.add(new Fruit(R.drawable.apple, "苹果", "10元/KG"));
mList.add(new Fruit(R.drawable.litchi, "荔枝", "9.9元/KG"));
}
}
运行效果,如下图所示:
二、ListView加载不同布局
通过getItemViewType()方法,根据不同类型,加载不同的布局,不同布局的加载顺序由具体业务逻辑决定,结合上面1。
1、思维导图
2、详细代码
(1)ListViewAdapter
public class ListViewAdapter extends BaseAdapter {
private List<Fruit> mList;
private Context mContext;
private ViewHolder viewHolder;
private ViewHolderSe viewHolderSe;
private LayoutInflater inflater;
private static final int type_1 = 0;
private static final int type_2 = 1;
public ListViewAdapter(List<Fruit> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
}
@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 int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
int type = position % 5;
if (type < 4) {
return type_1;
} else {
return type_2;
}
// return position % 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
viewHolder = new ViewHolder();
viewHolderSe = new ViewHolderSe();
inflater = LayoutInflater.from(mContext);
if (convertView == null) {
switch (type) {
case type_1:
convertView = inflater.inflate(R.layout.listview_item, parent, false);
viewHolder.imageView = convertView.findViewById(R.id.fruitImg);
viewHolder.textName = convertView.findViewById(R.id.fruitName);
viewHolder.textPrice = convertView.findViewById(R.id.fruitPrice);
// 缓存各个控件的实例
convertView.setTag(viewHolder);
break;
case type_2:
convertView = inflater.inflate(R.layout.other_listview_item, parent, false);
viewHolderSe.imageViewSe = convertView.findViewById(R.id.oFruitImg);
viewHolderSe.textDetails = convertView.findViewById(R.id.oFruitDetails);
// 缓存各个控件的实例
convertView.setTag(viewHolderSe);
break;
default:
break;
}
} else {
// 取出缓存
switch (type) {
case type_1:
viewHolder = (ViewHolder) convertView.getTag();
break;
case type_2:
viewHolderSe = (ViewHolderSe) convertView.getTag();
break;
default:
break;
}
}
switch (type) {
case type_1:
viewHolder.imageView.setImageResource(mList.get(position).getImage());
viewHolder.textName.setText(mList.get(position).getName());
viewHolder.textPrice.setText(mList.get(position).getPrice());
break;
case type_2:
viewHolderSe.imageViewSe.setImageResource(mList.get(position).getImage());
viewHolderSe.textDetails.setText("\t\t\t\t" + mList.get(position).getDetails());
break;
default:
break;
}
return convertView;
}
// 内部类
static class ViewHolder {
ImageView imageView;
TextView textName;
TextView textPrice;
}
static class ViewHolderSe {
ImageView imageViewSe;
TextView textDetails;
}
}
运行效果,如下图所示: