1、MVP模式demo的实现,效果下:
2、创建一个Fruit类:
package com.example.listview;
//Fruit类就是Model,表示应用程序中的数据对象。
public class Fruit {private int imageId;private String name;private String price;public Fruit(int imageId, String name, String price) {this.imageId = imageId;this.name = name;this.price = price;}public int getImageId() {return imageId;}public String getName() {return name;}public String getPrice() {return price;}
}
3、创建FruitAdapter适配器:
package com.example.listview;
import android.content.Context;
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 java.util.List;
public class FruitAdapter extends ArrayAdapter<Fruit> {private int resourceId;public FruitAdapter(Context context, int resourceId, List<Fruit> objects) {super(context, resourceId, objects);this.resourceId = resourceId;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Fruit fruit = getItem(position);View view;ViewHolder viewHolder;if (convertView == null) {view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);viewHolder = new ViewHolder();viewHolder.fruitImage = view.findViewById(R.id.fruit_image);viewHolder.fruitName = view.findViewById(R.id.fruit_name);viewHolder.fruitPrice = view.findViewById(R.id.fruit_price);view.setTag(viewHolder);} else {view = convertView;viewHolder = (ViewHolder) view.getTag();}viewHolder.fruitImage.setImageResource(fruit.getImageId());viewHolder.fruitName.setText(fruit.getName());viewHolder.fruitPrice.setText("价格: " + fruit.getPrice() + "元/斤");return view;}static class ViewHolder {ImageView fruitImage;TextView fruitName;TextView fruitPrice;}
}
4、创建MainView接口,用于显示UI界面。
package com.example.listview;
import java.util.List;
//定义View,负责UI的展示和用户交互。
public interface MainView {void displayFruits(List<Fruit> fruitList);
}
5、创建Presenter:
package com.example.listview;
import java.util.ArrayList;
import java.util.List;
//定义Presenter,用于处理业务逻辑和用户输入。
public class MainPresenter {private MainView view;private List<Fruit> fruitList;public MainPresenter(MainView view) {this.view = view;fruitList = generateFruitData();}public void loadFruits() {view.displayFruits(fruitList);}private List<Fruit> generateFruitData() {List<Fruit> fruitList = new ArrayList<>();fruitList.add(new Fruit(R.drawable.pineapple,"菠萝", "68"));fruitList.add(new Fruit(R.drawable.strawberry,"草莓", "55"));fruitList.add(new Fruit(R.drawable.orange,"橙子", "36"));fruitList.add(new Fruit(R.drawable.longan,"桂圆", "78"));fruitList.add(new Fruit(R.drawable.cantaloupe,"哈密瓜", "99"));fruitList.add(new Fruit(R.drawable.dragon_fruit,"火龙果", "128"));fruitList.add(new Fruit(R.drawable.kiwi, "猕猴桃", "90"));fruitList.add(new Fruit(R.drawable.lemon,"柠檬", "56"));fruitList.add(new Fruit(R.drawable.loquat, "枇杷", "81"));fruitList.add(new Fruit(R.drawable.apple, "苹果", "160"));fruitList.add(new Fruit(R.drawable.mandarin_orange, "砂糖桔", "150"));fruitList.add(new Fruit(R.drawable.pomelo, "樱桃", "200"));fruitList.add(new Fruit(R.drawable.tomato, "柚子", "62"));fruitList.add(new Fruit(R.drawable.cherry, "西红柿", "60"));fruitList.add(new Fruit(R.drawable.pineapple,"菠萝", "68"));fruitList.add(new Fruit(R.drawable.strawberry,"草莓", "55"));fruitList.add(new Fruit(R.drawable.orange,"橙子", "36"));fruitList.add(new Fruit(R.drawable.longan,"桂圆", "78"));fruitList.add(new Fruit(R.drawable.cantaloupe,"哈密瓜", "99"));fruitList.add(new Fruit(R.drawable.dragon_fruit,"火龙果", "128"));fruitList.add(new Fruit(R.drawable.kiwi, "猕猴桃", "90"));fruitList.add(new Fruit(R.drawable.lemon,"柠檬", "56"));fruitList.add(new Fruit(R.drawable.loquat, "枇杷", "81"));fruitList.add(new Fruit(R.drawable.apple, "苹果", "160"));fruitList.add(new Fruit(R.drawable.mandarin_orange, "砂糖桔", "150"));fruitList.add(new Fruit(R.drawable.pomelo, "樱桃", "200"));fruitList.add(new Fruit(R.drawable.tomato, "柚子", "62"));fruitList.add(new Fruit(R.drawable.cherry, "西红柿", "60"));return fruitList;}
}
6、在MainActivity中实现MVP模式的业务逻辑:
package com.example.listview;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
//Model:负责数据的管理和处理。
//View:负责UI的展示和用户交互。
//Presenter:连接Model和View,处理业务逻辑和用户输入。
//Presenter在加载数据后通过MainView接口回调通知View显示水果列表。
public class MainActivity extends AppCompatActivity implements MainView {private ListView listView;private FruitAdapter adapter;private MainPresenter presenter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = findViewById(R.id.list_view);presenter = new MainPresenter(this);presenter.loadFruits();}@Overridepublic void displayFruits(List<Fruit> fruitList) {adapter = new FruitAdapter(this, R.layout.item_fruit, fruitList);listView.setAdapter(adapter);listView.setOnItemClickListener((parent, view, position, id) -> {Fruit fruit = fruitList.get(position);showFruitDetails(fruit);});}private void showFruitDetails(Fruit fruit) {Toast.makeText(MainActivity.this, "名称: " + fruit.getName() + ", 价格: " + fruit.getPrice() + "元/斤", Toast.LENGTH_SHORT).show();}
}
7、activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ListViewandroid:id="@+id/list_view"android:layout_width="match_parent"android:layout_height="match_parent"/></RelativeLayout>
8、item_fruit.xml文件:
<?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:layout_height="wrap_content"android:padding="16dp"><ImageViewandroid:id="@+id/fruit_image"android:layout_width="64dp"android:layout_height="64dp"android:scaleType="centerCrop"android:src="@mipmap/ic_launcher" /><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="vertical"android:layout_marginStart="16dp"><TextViewandroid:id="@+id/fruit_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"android:textStyle="bold"android:text="水果名称" /><TextViewandroid:id="@+id/fruit_price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="14sp"android:textColor="@android:color/darker_gray"android:text="价格: " /></LinearLayout></LinearLayout>