我正在开发一个食谱应用程序。我想要做到这一点,以便单击“早餐主菜”进入“早餐主菜活动”,单击“甜点”进入“甜点活动”,单击“饮料”进入“饮料活动”,等等。现在,单击任何食谱类别都会将我带到“早餐主菜活动”。
我在下面包含了 RecipeCategories 类以及 RecipeCategoriesAdapter 类。
package app.android.easyvegan;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import android.view.Menu;
import android.view.MenuItem;
public class RecipeCategories extends AppCompatActivity implements RecipeCategoriesSelectionListener {
public void onCreate(Bundle savedInstanceState) {
Utilities.SetTheme(getSharedPreferences("Themes", 0).getInt("theme", 0));
Utilities.onActivityCreateSetTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_categories);
setSupportActionBar(findViewById(R.id.topToolbar));
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
RecyclerView recyclerView = findViewById(R.id.recipeCategoriesRecycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
List<String> recipeCategoriesList = Arrays.asList(getResources().getStringArray(R.array.recipe_categories));
RecipeCategoriesAdapter adapter = new RecipeCategoriesAdapter(recipeCategoriesList, this);
recyclerView.setAdapter(adapter);
}
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.menu_recipe_categories, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item){
SharedPreferences.Editor editor = getSharedPreferences("Themes", 0).edit();
switch (item.getItemId()) {
case 2131296477:
startActivity(new Intent(this, HomeScreen.class));
break;
case 2131296666:
Intent intent = new Intent("android.intent.action.SEND");
intent.setType("text/plain");
intent.putExtra("android.intent.extra.SUBJECT", "EasyVegan Recipes");
intent.putExtra("android.intent.extra.TEXT", "EasyVegan: Plant-Based Recipes App");
startActivity(Intent.createChooser(intent, "Share using"));
break;
case 2131296782:
Utilities.changeToTheme(this, 0);
editor.putInt("theme", 0);
editor.apply();
break;
case 2131296780:
Utilities.changeToTheme(this, 1);
editor.putInt("theme", 1);
editor.apply();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onRecipeCategorySelected(String recipeCategory)
{
Intent intent = new Intent(this, BreakfastEntrees.class);
startActivity(intent);
}
}
__________________________________________________________________________________
package app.android.easyvegan;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class RecipeCategoriesAdapter extends RecyclerView.Adapter<RecipeCategoriesAdapter.RecipeCategoriesViewHolder> {
private final List<String> recipeCategoriesList;
private final RecipeCategoriesSelectionListener selectionListener;
public RecipeCategoriesAdapter(List<String> recipeCategoriesList, Context context) {
this.recipeCategoriesList = recipeCategoriesList;
selectionListener = (RecipeCategories) context;
}
@NonNull
@Override
public RecipeCategoriesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
TextView textView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_categories_recycler,parent,false);
return new RecipeCategoriesViewHolder(textView);
}
@Override
public void onBindViewHolder(@NonNull RecipeCategoriesViewHolder holder, int position) {
String recipeCategory = recipeCategoriesList.get(position);
holder.recipeCategory.setText(recipeCategory);
holder.recipeCategory.setOnClickListener(view -> selectionListener.onRecipeCategorySelected(recipeCategoriesList.get(holder.getAdapterPosition())));
}
@Override
public int getItemCount() {
return recipeCategoriesList.size();
}
public static class RecipeCategoriesViewHolder extends RecyclerView.ViewHolder
{
public TextView recipeCategory;
public RecipeCategoriesViewHolder (@NonNull TextView itemView) {
super(itemView);
recipeCategory = itemView;
}
}
}
我尝试在每个位置的 onBindViewHolder 中添加“when”语句,但如果这是解决方案,我不知道如何正确编码。
我知道在 OnRecipeCategorySelected 中我已将其编码为仅启动 BreakfastEntreesActivity。我只是不确定如何调整它来为每个食谱类别启动不同的活动。
在 BindViewHolder 中,您必须设置 onClickListener 来启动与单击的项目相关的新活动。
如果您使用列表来显示 recyclerView 中的项目,这是我的应用程序适配器中的代码,它工作得很好。
public class RecipeViewHolder extends RecyclerView.ViewHolder {
ImageView recipeImageView;
TextView recipeNameTextView;
TextView recipeTimeTextView;
ConstraintLayout recipeItem;
public RecipeViewHolder(View itemView) {
super(itemView);
recipeImageView = itemView.findViewById(R.id.recipeImageView1);
recipeNameTextView = itemView.findViewById(R.id.recipeNameTextView1);
recipeTimeTextView = itemView.findViewById(R.id.recipeTimeTextView1);
recipeItem = itemView.findViewById(R.id.recipeItemSample);
}
}
@Override
public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_recipe, parent, false);
return new RecipeViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecipeViewHolder holder, int position) {
Recipe recipe = recipeList.get(position);
holder.recipeNameTextView.setText(recipe.getName());
holder.recipeTimeTextView.setText(recipe.getCookingTime());
holder.recipeImageView.setImageResource(recipe.getImageUrl());
holder.recipeItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedRecipeId = recipeList.get(position).getId();
Intent intent = new Intent(fragment.requireContext(), RecipeDetailsActivity.class);
intent.putExtra("recipe_id", selectedRecipeId);
fragment.requireContext().startActivity(intent);
}
});
}
在我的例子中,该项目的父布局是约束布局。 另外,我在片段中使用 recyclerView。 如果您愿意,我可以分享适配器的完整代码。