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>
相关推荐
虾球xz22 分钟前
游戏引擎学习第64天
redis·学习·游戏引擎
胡西风_foxww24 分钟前
【ES6复习笔记】Map(14)
前端·笔记·es6·map
半导体守望者26 分钟前
Associa asresearch HypotMAX 7705 7710 7715 7720 OPERATION AND SERVICE MANUAL
经验分享·笔记·功能测试·自动化·制造
虾球xz29 分钟前
游戏引擎学习第63天
学习·游戏引擎
Hacker_Nightrain30 分钟前
linux 网络安全不完全笔记
linux·笔记·web安全
亚瑟-灰太狼42 分钟前
memory泄露分析方法(Binder,Window,View篇)
android
朝九晚五ฺ1 小时前
【Linux探索学习】第二十三弹——理解文件系统:认识硬件、探索文件在硬件上的存储问题
linux·运维·学习
两水先木示1 小时前
【Unity3D】ECS入门学习(六)状态组件 ISystemStateComponentData
学习·unity·ecs
l and2 小时前
Git 行尾换行符,导致无法进入游戏
android·git
程序媛小果2 小时前
基于Django+python的Python在线自主评测系统设计与实现
android·python·django