首页 > Java > java教程 > 正文

在Java Android应用中实现“最近使用”功能:管理固定大小的列表

碧海醫心
发布: 2025-10-05 13:01:41
原创
153人浏览过

在java android应用中实现“最近使用”功能:管理固定大小的列表

本教程旨在详细指导如何在Java Android应用中实现“最近使用”食谱功能。我们将探讨如何维护一个固定大小的列表来存储最近浏览的食谱索引,并通过高效的添加、移除和移位操作来确保列表始终反映最新的使用情况。此外,文章还将涵盖数据持久化策略,确保用户关闭应用后“最近使用”列表不会丢失,并提供代码示例和最佳实践建议,以构建健壮且用户友好的功能。

1. 理解“最近使用”功能的需求

在许多应用中,“最近使用”、“最近浏览”或“历史记录”功能是提升用户体验的关键。对于一个食谱应用而言,显示用户最近查看过的食谱,可以方便他们快速回顾或再次访问。我们的目标是创建一个“最近食谱”区域,显示用户最近浏览的三个食谱。

原始的食谱数据结构 ArrayList<ArrayList<Integer>> 虽然可以存储食谱信息,但其可读性和维护性有待提升。在实现“最近使用”功能时,我们首先需要一个机制来记录哪些食谱被使用了,以及它们的顺序。

2. 核心概念:管理固定大小的最近使用列表

为了实现“最近使用”功能,我们需要一个单独的数据结构来存储最近被访问的食谱。这个列表应该具有以下特性:

  • 固定大小: 例如,只显示最近的3个食谱。
  • 顺序性: 列表中的食谱应按照访问时间从新到旧排列
  • 动态更新: 每当用户访问一个食谱时,列表需要更新。

最常见的实现方式是使用一个 ArrayList 或 LinkedList 来模拟一个固定大小的队列或堆,并配合移位操作。

立即学习Java免费学习笔记(深入)”;

3. 实现策略:基于 ArrayList 的固定大小列表

我们将创建一个专门的类来管理最近使用的食谱列表。这个类将负责添加新食谱、维护列表顺序和大小。

3.1 RecentRecipesManager 类设计

我们定义一个 RecentRecipesManager 类,它内部包含一个 ArrayList 来存储最近使用的食谱的索引。

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

public class RecentRecipesManager {
    private static final int MAX_RECENT_RECIPES = 3; // 最大最近食谱数量
    private List<Integer> recentRecipeIndexes;

    public RecentRecipesManager() {
        this.recentRecipeIndexes = new ArrayList<>();
    }

    /**
     * 添加一个食谱索引到最近使用列表。
     * 如果食谱已存在,则将其移到列表最前面。
     * 如果列表已满,则移除最旧的食谱。
     *
     * @param recipeIndex 被使用的食谱的索引
     */
    public void addRecipe(int recipeIndex) {
        // 1. 如果食谱已在列表中,先移除旧位置的它
        recentRecipeIndexes.remove(Integer.valueOf(recipeIndex)); // 使用Integer.valueOf确保移除的是对象而不是索引

        // 2. 将新食谱添加到列表的最前面 (索引0)
        recentRecipeIndexes.add(0, recipeIndex);

        // 3. 如果列表大小超过最大限制,移除最旧的 (列表末尾的) 食谱
        if (recentRecipeIndexes.size() > MAX_RECENT_RECIPES) {
            recentRecipeIndexes.remove(MAX_RECENT_RECIPES); // 移除超出限制的元素
        }
    }

    /**
     * 获取当前最近使用的食谱索引列表。
     *
     * @return 最近使用的食谱索引列表,按时间从新到旧排序
     */
    public List<Integer> getRecentRecipeIndexes() {
        return new ArrayList<>(recentRecipeIndexes); // 返回一个副本,防止外部直接修改
    }
}
登录后复制

解释:

  • MAX_RECENT_RECIPES 定义了我们希望保留的最近食谱数量。
  • addRecipe(int recipeIndex) 方法是核心:
    • 它首先检查 recipeIndex 是否已经存在于 recentRecipeIndexes 中。如果存在,会将其从当前位置移除,以避免重复,并确保它能被添加到列表的最新位置。
    • 然后,它使用 add(0, recipeIndex) 将新的(或更新的)食谱索引添加到列表的最前面,使其成为“最新”的食谱。
    • 最后,如果列表的大小超过了 MAX_RECENT_RECIPES,它会移除列表末尾的元素,即“最旧”的食谱,从而保持列表的固定大小。

4. 数据持久化:确保“最近使用”列表不丢失

在Android应用中,如果“最近使用”列表只存在于内存中,那么当应用关闭或被系统回收时,这些数据就会丢失。为了让“最近使用”列表在应用会话之间持久化,我们需要将其存储起来。SharedPreferences 是存储少量键值对数据的理想选择。

4.1 修改 RecentRecipesManager 以支持持久化

我们需要在 RecentRecipesManager 中添加保存和加载列表的方法。

import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class RecentRecipesManager {
    private static final int MAX_RECENT_RECIPES = 3;
    private static final String PREFS_NAME = "RecentRecipesPrefs";
    private static final String KEY_RECENT_RECIPES = "recent_recipe_indexes";

    private List<Integer> recentRecipeIndexes;
    private Context context;
    private Gson gson; // 用于将List<Integer>序列化为JSON字符串

    public RecentRecipesManager(Context context) {
        this.context = context.getApplicationContext(); // 使用ApplicationContext防止内存泄漏
        this.recentRecipeIndexes = new ArrayList<>();
        this.gson = new Gson();
        loadRecentRecipes(); // 构造时加载数据
    }

    public void addRecipe(int recipeIndex) {
        recentRecipeIndexes.remove(Integer.valueOf(recipeIndex));
        recentRecipeIndexes.add(0, recipeIndex);

        if (recentRecipeIndexes.size() > MAX_RECENT_RECIPES) {
            recentRecipeIndexes.remove(MAX_RECENT_RECIPES);
        }
        saveRecentRecipes(); // 每次修改后保存数据
    }

    public List<Integer> getRecentRecipeIndexes() {
        return new ArrayList<>(recentRecipeIndexes);
    }

    /**
     * 保存最近使用的食谱索引列表到SharedPreferences。
     */
    private void saveRecentRecipes() {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        String json = gson.toJson(recentRecipeIndexes); // 将List转换为JSON字符串
        editor.putString(KEY_RECENT_RECIPES, json);
        editor.apply(); // 异步保存
    }

    /**
     * 从SharedPreferences加载最近使用的食谱索引列表。
     */
    private void loadRecentRecipes() {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String json = prefs.getString(KEY_RECENT_RECIPES, null);
        if (json != null) {
            Type type = new TypeToken<List<Integer>>() {}.getType(); // 定义List<Integer>的类型
            recentRecipeIndexes = gson.fromJson(json, type); // 将JSON字符串反序列化为List
        } else {
            recentRecipeIndexes = new ArrayList<>();
        }
    }
}
登录后复制

注意:

AI Humanize
AI Humanize

使用AI改写工具,生成不可被AI检测的文本内容

AI Humanize 154
查看详情 AI Humanize
  • 为了将 List<Integer> 存储为 SharedPreferences 中的一个字符串,我们使用了 Gson 库进行JSON序列化和反序列化。你需要将Gson库添加到你的 build.gradle 文件中:
    // build.gradle (app module)
    dependencies {
        implementation 'com.google.code.gson:gson:2.8.9' // 使用最新版本
    }
    登录后复制
  • 在构造函数中调用 loadRecentRecipes(),确保 RecentRecipesManager 实例创建时就加载了之前保存的数据。
  • 在 addRecipe() 方法中调用 saveRecentRecipes(),确保每次列表更新后数据都能及时持久化。

5. 集成到 Android 应用界面

现在我们已经有了管理和持久化最近食谱的逻辑,接下来需要将其集成到 MainActivity 和食谱详情页面。

5.1 在 MainActivity 中初始化和显示

在 MainActivity 中,我们将创建 RecentRecipesManager 的实例,并在 onCreate 方法中加载并显示最近的食谱。

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecentRecipesManager recentRecipesManager;
    private ImageButton recent1, recent2, recent3;
    private ReceiptsBase receiptsBase; // 假设这是你的食谱数据库类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // hideSystemUI(); // 如果有需要,可以保留

        recentRecipesManager = new RecentRecipesManager(this); // 初始化管理器
        receiptsBase = new ReceiptsBase(); // 初始化你的食谱数据库

        // 初始化按钮
        Button button_recipes = findViewById(R.id.button2);
        button_recipes.setOnClickListener(view -> openRecipes());

        Button button_search = findViewById(R.id.button3);
        button_search.setOnClickListener(view -> openSearch());

        Button button_supriseme = findViewById(R.id.button4);
        button_supriseme.setOnClickListener(view -> openSupriseMe());

        recent1 = findViewById(R.id.rec1);
        recent2 = findViewById(R.id.rec2);
        recent3 = findViewById(R.id.rec3);

        // 更新最近食谱显示
        updateRecentRecipesUI();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 当Activity重新可见时,可能需要刷新UI,例如从食谱详情页返回
        updateRecentRecipesUI();
    }

    private void updateRecentRecipesUI() {
        List<Integer> recentIndexes = recentRecipesManager.getRecentRecipeIndexes();
        ImageButton[] recentButtons = {recent1, recent2, recent3};

        // 隐藏所有最近食谱按钮,然后只显示存在的
        for (ImageButton button : recentButtons) {
            button.setVisibility(View.GONE);
            button.setOnClickListener(null); // 清除旧的点击监听器
            button.setImageDrawable(null); // 清除旧图片
        }

        for (int i = 0; i < recentIndexes.size() && i < MAX_RECENT_RECIPES; i++) {
            int recipeIndex = recentIndexes.get(i);
            // 从你的食谱数据库中获取食谱的图片资源ID
            // 假设食谱的图片资源ID在ArrayList<Integer>的第一个位置 (索引0)
            int imageResId = receiptsBase.getReceipt(recipeIndex).get(0);

            ImageButton currentButton = recentButtons[i];
            currentButton.setVisibility(View.VISIBLE);
            currentButton.setImageResource(imageResId); // 设置图片
            // 为每个最近食谱按钮设置点击监听器
            currentButton.setOnClickListener(view -> openRecipe(recipeIndex));
        }
    }

    // 其他原有方法
    public void openRecipes(){
        Intent rec = new Intent(this, recipes.class);
        startActivity(rec);
    }
    public void openSearch(){
        Intent sea = new Intent(this, search.class);
        startActivity(sea);
    }
    public void openSupriseMe(){
        Intent sup = new Intent(this, Example.class); // Example可能是食谱详情页
        startActivity(sup);
    }

    // 修改 openRecipe 方法以记录被查看的食谱
    public void openRecipe(int recipeIndex){
        // 记录食谱被使用
        recentRecipesManager.addRecipe(recipeIndex);

        // 跳转到食谱详情页,并传递食谱索引
        Intent sup = new Intent(this, Example.class); // 假设Example是你的食谱详情Activity
        sup.putExtra("recipeIndex", recipeIndex);
        startActivity(sup);
        // finish(); // 通常在从主页跳转到详情页时不finish主页,以便返回
    }
}
登录后复制

5.2 在食谱详情页中记录使用

当用户在食谱详情页(例如 Example.class)中查看某个食谱时,也应该记录这个行为。

// Example.java (你的食谱详情Activity)
public class Example extends AppCompatActivity {

    private RecentRecipesManager recentRecipesManager;
    private int currentRecipeIndex; // 当前食谱的索引

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example); // 假设你的布局文件是activity_example.xml

        recentRecipesManager = new RecentRecipesManager(this);

        // 获取传递过来的食谱索引
        if (getIntent().hasExtra("recipeIndex")) {
            currentRecipeIndex = getIntent().getIntExtra("recipeIndex", -1);
            if (currentRecipeIndex != -1) {
                // 在这里加载和显示食谱详情
                // ...

                // 记录当前食谱被查看
                recentRecipesManager.addRecipe(currentRecipeIndex);
            }
        }
        // ... 其他初始化代码
    }
}
登录后复制

6. 优化与最佳实践

6.1 改进食谱数据模型

使用 ArrayList<ArrayList<Integer>> 来存储食谱数据是非常不推荐的,因为它缺乏类型安全和可读性。强烈建议定义一个自定义的 Recipe 类来封装食谱的所有属性:

public class Recipe {
    private int imageResId;
    private int nameResId;
    private int infoResId;
    private int prepResId;
    private int blurImageResId;
    private int ingrResId;

    public Recipe(int imageResId, int nameResId, int infoResId, int prepResId, int blurImageResId, int ingrResId) {
        this.imageResId = imageResId;
        this.nameResId = nameResId;
        this.infoResId = infoResId;
        this.prepResId = prepResId;
        this.blurImageResId = blurImageResId;
        this.ingrResId = ingrResId;
    }

    // 提供getter方法
    public int getImageResId() { return imageResId; }
    public int getNameResId() { return nameResId; }
    // ... 其他getter
}
登录后复制

然后,你的 ReceiptsBase 类可以存储 ArrayList<Recipe>:

public class ReceiptsBase {
    private ArrayList<Recipe> recipesAll = new ArrayList<>();

    public ReceiptsBase() {
        recipesAll.add(new Recipe(R.drawable.mush, R.string.mush_name, R.string.mush_info, R.string.mush_prep, R.drawable.mush_blur, R.string.mush_ingr));
        recipesAll.add(new Recipe(R.drawable.tomato, R.string.tomato_name, R.string.tomato_info, R.string.tomato_prep, R.drawable.tomato_blur, R.string.tomato_ingr));
        // ... 添加所有食谱
    }

    public Recipe getRecipe(int recipeIndex) {
        return recipesAll.get(recipeIndex);
    }

    public ArrayList<Recipe> getRecipes() {
        return recipesAll;
    }
}
登录后复制

这样,你的代码将更具可读性、类型安全,并且更容易维护。在 MainActivity 中获取图片资源时,你就可以通过 receiptsBase.getRecipe(recipeIndex).getImageResId() 来获取。

6.2 处理空状态

当 recentRecipeIndexes 列表为空时(例如,应用首次启动或用户尚未浏览任何食谱),updateRecentRecipesUI() 方法应该能够优雅地处理这种情况,例如显示一个“暂无最近食谱”的提示文本。

6.3 性能考虑

对于只有3个元素的“最近使用”列表,ArrayList 的 remove() 和 add(0, ...) 操作性能影响微乎其微。如果需要管理更大的列表(例如,几十个或上百个),可以考虑使用 LinkedList,因为它在列表两端的添加和删除操作效率更高。但对于当前需求,ArrayList 已经足够。

6.4 线程安全

对于单线程的Android UI应用,通常不需要担心 RecentRecipesManager 的线程安全问题。但如果你的应用在后台线程中也可能修改这个列表,那么需要使用 Collections.synchronizedList() 或 CopyOnWriteArrayList 来确保线程安全。

7. 总结

通过本教程,我们学习了如何在Java Android应用中实现一个健壮的“最近使用”食谱功能。核心在于创建一个 RecentRecipesManager 类,利用 ArrayList 管理固定大小的食谱索引列表,并通过在列表头部添加新项、移除重复项和尾部旧项

以上就是在Java Android应用中实现“最近使用”功能:管理固定大小的列表的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号