android学习笔记(五)-MVP模式

1、MVP模式demo的实现,效果下:

2、创建一个Fruit类:

java 复制代码
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适配器:

java 复制代码
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;
    }

    @Override
    public 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界面。

java 复制代码
package com.example.listview;
import java.util.List;
//定义View,负责UI的展示和用户交互。
public interface MainView {
    void displayFruits(List<Fruit> fruitList);
}

5、创建Presenter:

java 复制代码
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模式的业务逻辑:

java 复制代码
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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.list_view);
        presenter = new MainPresenter(this);
        presenter.loadFruits();
    }

    @Override
    public 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 复制代码
<?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">

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

</RelativeLayout>

8、item_fruit.xml文件:

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">

    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_marginStart="16dp">

        <TextView
            android:id="@+id/fruit_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text="水果名称" />

        <TextView
            android: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>
相关推荐
非概念1 分钟前
stm32学习笔记----51单片机和stm32单片机的区别
笔记·stm32·单片机·学习·51单片机
无敌最俊朗@1 小时前
stm32学习之路——八种GPIO口工作模式
c语言·stm32·单片机·学习
EterNity_TiMe_2 小时前
【论文复现】STM32设计的物联网智能鱼缸
stm32·单片机·嵌入式硬件·物联网·学习·性能优化
L_cl2 小时前
Python学习从0到1 day28 Python 高阶技巧 ⑤ 多线程
学习
前端SkyRain2 小时前
后端Node学习项目-用户管理-增删改查
后端·学习·node.js
青椒大仙KI112 小时前
24/11/13 算法笔记<强化学习> DQN算法
笔记·算法
提笔惊蚂蚁2 小时前
结构化(经典)软件开发方法: 需求分析阶段+设计阶段
后端·学习·需求分析
DDDiccc2 小时前
JAVA学习日记(十五) 数据结构
数据结构·学习
promise-render2 小时前
npm、yarn、pnpm 切换查看镜像源笔记
前端·笔记·npm
烬奇小云3 小时前
认识一下Unicorn
android·python·安全·系统安全