android listview

今天写一下listview 的小demo.具体效果如下述所示

具体实现代码如下所示,其中有3个java文件,2个xml文件。

package com.example.listviewtest;

public class Fruit {
    private String name;
    private int imageId;
    private double price;

    public Fruit(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;

    }

    public Fruit(String name, int imageId, double price) {
        this.name = name;
        this.imageId = imageId;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public int getImageId() {
        return imageId;
    }

    public double getPrice() {
        return price;
    }
}
Fruit
package com.example.listviewtest;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import static android.content.ContentValues.TAG;

//适配器界面
public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int resourceId;
    public FruitAdapter(@NonNull Context context, int resource, @NonNull List<Fruit> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    //通过convertview优化Listview防止每次都加载一次item的结构
    //通过viewHold 优化Listview防止每次都去寻找控件的id;
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Fruit fruit = getItem(position);//获取当前项目的fruit实例
       // View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        View view;
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        }else{
            view = convertView;
        }
        ImageView fruitImage = view.findViewById(R.id.fruit_image);
        fruitImage.setImageResource(fruit.getImageId());
        TextView fruitName = view.findViewById(R.id.fruit_name);
        fruitImage.setImageResource(fruit.getImageId());
        fruitName.setText(fruit.getName());
        TextView fruitPrice = view.findViewById(R.id.fruit_price);
        Double a = fruit.getPrice();
        String b = ""+a;
        fruitPrice.setText(b);//将double型转化为字符串型
        return view;
    }
}
FruitAdapter
package com.example.listviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

//    private String[] data = {"Apple","Banana","Orange","Watermelon","pear","Grape","pineapple","Strawberry",
//    "Cherry","Mango","Apple","Banana","Orange","Watermelon","pear","Grape","pineapple","Strawberry",
//            "Cherry","Mango"};
    private List<Fruit> fruitList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
//        ListView listView = findViewById(R.id.list_view);
//        listView.setAdapter(adapter);
        initFruits();
        FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item, fruitList);

        ListView listView = findViewById(R.id.list_view);
        listView.setAdapter(adapter);
       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Fruit fruit = fruitList.get(position);
               Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
           }
       });
    }

    private void initFruits() {
        for(int i=0;i<2;i++){
            Fruit apple = new Fruit("apple",R.mipmap.apple,666);
            fruitList.add(apple);
            Fruit banana = new Fruit("banana",R.mipmap.banana, 2);
            fruitList.add(banana);
            Fruit orange = new Fruit("orange",R.mipmap.orange, 999);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("watermelon",R.mipmap.watermelon, 5.8);
            fruitList.add(watermelon);
            Fruit pear = new Fruit("pear",R.mipmap.pear, 3.3);
            fruitList.add(pear);
            Fruit grape = new Fruit("grape",R.mipmap.grape, 3.3);
            fruitList.add(grape);
            Fruit pineapple = new Fruit("pineapple",R.mipmap.pineapple, 3.3);
            fruitList.add(pineapple);
            Fruit strawberry = new Fruit("strawberry",R.mipmap.strawberry, 3.3);
            fruitList.add(strawberry);
            Fruit cherry = new Fruit("cherry",R.mipmap.cherry, 3.3);
            fruitList.add(cherry);
            Fruit mango = new Fruit("mango",R.mipmap.mango, 3.3);
            fruitList.add(mango);

        }
    }
}
MainActivity
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

  <ListView
      android:id="@+id/list_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>


</LinearLayout>
activity_main.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">

    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"/>
    <TextView
        android:id="@+id/fruit_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_gravity="center"
        android:layout_marginLeft="10dp"/>
</LinearLayout>
fruit_item.xml
原文地址:https://www.cnblogs.com/wfswf/p/14861736.html