
在许多应用中,“最近使用”、“最近浏览”或“历史记录”功能是提升用户体验的关键。对于一个食谱应用而言,显示用户最近查看过的食谱,可以方便他们快速回顾或再次访问。我们的目标是创建一个“最近食谱”区域,显示用户最近浏览的三个食谱。
原始的食谱数据结构 ArrayList<ArrayList<Integer>> 虽然可以存储食谱信息,但其可读性和维护性有待提升。在实现“最近使用”功能时,我们首先需要一个机制来记录哪些食谱被使用了,以及它们的顺序。
为了实现“最近使用”功能,我们需要一个单独的数据结构来存储最近被访问的食谱。这个列表应该具有以下特性:
最常见的实现方式是使用一个 ArrayList 或 LinkedList 来模拟一个固定大小的队列或堆栈,并配合移位操作。
立即学习“Java免费学习笔记(深入)”;
我们将创建一个专门的类来管理最近使用的食谱列表。这个类将负责添加新食谱、维护列表顺序和大小。
我们定义一个 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); // 返回一个副本,防止外部直接修改
}
}解释:
在Android应用中,如果“最近使用”列表只存在于内存中,那么当应用关闭或被系统回收时,这些数据就会丢失。为了让“最近使用”列表在应用会话之间持久化,我们需要将其存储起来。SharedPreferences 是存储少量键值对数据的理想选择。
我们需要在 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<>();
}
}
}注意:
// build.gradle (app module)
dependencies {
implementation 'com.google.code.gson:gson:2.8.9' // 使用最新版本
}现在我们已经有了管理和持久化最近食谱的逻辑,接下来需要将其集成到 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主页,以便返回
}
}当用户在食谱详情页(例如 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);
}
}
// ... 其他初始化代码
}
}使用 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() 来获取。
当 recentRecipeIndexes 列表为空时(例如,应用首次启动或用户尚未浏览任何食谱),updateRecentRecipesUI() 方法应该能够优雅地处理这种情况,例如显示一个“暂无最近食谱”的提示文本。
对于只有3个元素的“最近使用”列表,ArrayList 的 remove() 和 add(0, ...) 操作性能影响微乎其微。如果需要管理更大的列表(例如,几十个或上百个),可以考虑使用 LinkedList,因为它在列表两端的添加和删除操作效率更高。但对于当前需求,ArrayList 已经足够。
对于单线程的Android UI应用,通常不需要担心 RecentRecipesManager 的线程安全问题。但如果你的应用在后台线程中也可能修改这个列表,那么需要使用 Collections.synchronizedList() 或 CopyOnWriteArrayList 来确保线程安全。
通过本教程,我们学习了如何在Java Android应用中实现一个健壮的“最近使用”食谱功能。核心在于创建一个 RecentRecipesManager 类,利用 ArrayList 管理固定大小的食谱索引列表,并通过在列表头部添加新项、移除重复项和尾部旧项
以上就是在Java Android应用中实现“最近使用”功能:管理固定大小的列表的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号