1、在mainactivity_layout中使用LinearLayout布局,加入ListView控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/ListView" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
2、创建fruit_item的layout,完成显示名称,图片等信息的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!--显示水果的图片--> <ImageView android:id="@+id/fruit_image" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!--显示水果的名称--> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp"/> </LinearLayout>
3、创建Fruit.java实体类,定义有参构造器和getxxx()方法
public class Fruit { private String name;//水果的名字 private int imgId;//水果对应图片资源的ID public Fruit(String name, int imgId) { this.name = name; this.imgId = imgId; } public String getName() { return name; } public int getImgId() { return imgId; } }
4、创建自定义的适配器继承自ArrayAdapter<Fruit>
4-1 定义构造函数
private int resourceId; /* private ImageView fruitImg; private TextView fruitName;*/ public FruitAdapter(@NonNull Context context, int textViewResourceId, @NonNull List<Fruit> objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; }
4-2 重写getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)方法
@NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { //获取当前项的Fruit实例 Fruit fruit = getItem(position); /*View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);*/ //利用convertView提升运行效率 /* View view; if (convertView == null){ //convertView缓存中没有布局,加载布局 view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); }else{ //convertView缓存有加载完成的布局,直接调用 view = convertView; }*/ //同时优化convertView和调用控件 View view; Viewholder viewholder; if (convertView == null){ //convert缓存中没有布局,加载布局 view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); viewholder = new Viewholder(); viewholder.fruitImg = view.findViewById(R.id.fruit_image); viewholder.fruitName = view.findViewById(R.id.fruit_name); view.setTag(viewholder);//讲viewholder存储在View中 }else { view = convertView; viewholder = (Viewholder) view.getTag();//重新获取viewholder } //设置水果的图片 /*fruitImg = view.findViewById(R.id.fruit_image);//多次调用控件可优化*/ viewholder.fruitImg.setImageResource(fruit.getImgId()); //设置水果名 /*fruitName = view.findViewById(R.id.fruit_name);*/ viewholder.fruitName.setText(fruit.getName()); return view; } class Viewholder{ ImageView fruitImg; TextView fruitName; }
5、在main_activity_layout中完成以下步骤:
1、初始化数据
2、调用自定义的适配器创建适配器对象
3、传入数据,进行展示
public class MainActivity extends AppCompatActivity { //初始化数据 /* private String[] data = {"apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","cherry","Mongo", "apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","cherry","Mongo"}; */ private List<Fruit> fruitList = new ArrayList<>(); private ListView mLv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //无图片时 /*//创建适配器对象 ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_list_item_1,data); mLv1 = findViewById(R.id.ListView); //传入数据 mLv1.setAdapter(adapter);*/ //有图片时 //初始化水果数据 initFruits(); //调用自定义的水果适配器创建适配器对象 FruitAdapter fruitAdapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList); //传入数据 mLv1 = findViewById(R.id.ListView); mLv1.setAdapter(fruitAdapter); } private void initFruits() { for (int i = 0; i < 2; i++) { Fruit apple = new Fruit("Apple", R.drawable.apple_pic); fruitList.add(apple); Fruit banana = new Fruit("Bnana", R.drawable.banana_pic); fruitList.add(banana); Fruit orange = new Fruit("Orange", R.drawable.apple_pic); fruitList.add(orange); Fruit watermelon = new Fruit("Watermelon", R.drawable.apple_pic); fruitList.add(watermelon); Fruit pear = new Fruit("Pear", R.drawable.apple_pic); fruitList.add(pear); Fruit grape = new Fruit("Grape", R.drawable.apple_pic); fruitList.add(grape); Fruit pineapple = new Fruit("Pineapple", R.drawable.apple_pic); fruitList.add(pineapple); Fruit strawberry = new Fruit("Strawberry", R.drawable.apple_pic); fruitList.add(strawberry); Fruit cherry = new Fruit("Cherry", R.drawable.apple_pic); fruitList.add(cherry); Fruit mongo = new Fruit("Mongo", R.drawable.apple_pic); fruitList.add(mongo); } } }