Android菜谱APP:如何实现RecyclerView获取用户输入并编辑食材项
菜谱应用食材添加功能实现方案
1. 修正布局结构
确保横向RecyclerView与EditText上下排列,RecyclerView需指定横向布局方向:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/ingredients_recycler" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:id="@+id/et_ingredient" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入食材后按回车添加"/> </LinearLayout>
2. 处理EditText回车添加逻辑
无需使用TextWatcher,直接监听回车键事件,触发食材添加:
EditText etIngredient = findViewById(R.id.et_ingredient); etIngredient.setOnKeyListener((v, keyCode, event) -> { if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { String ingredient = etIngredient.getText().toString().trim(); if (!ingredient.isEmpty()) { ingredientList.add(ingredient); adapter.notifyItemInserted(ingredientList.size() - 1); etIngredient.setText(""); } return true; } return false; });
3. 完善RecyclerView Adapter实现删除功能
Adapter需绑定数据集并提供删除回调,每个Item包含食材文本和删除按钮:
public class IngredientAdapter extends RecyclerView.Adapter<IngredientAdapter.ViewHolder> { private List<String> ingredientList; private OnDeleteListener deleteListener; public interface OnDeleteListener { void onDelete(int position); } public IngredientAdapter(List<String> list, OnDeleteListener listener) { this.ingredientList = list; this.deleteListener = listener; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_ingredient, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.tvIngredient.setText(ingredientList.get(position)); holder.btnDelete.setOnClickListener(v -> deleteListener.onDelete(position)); } @Override public int getItemCount() { return ingredientList.size(); } static class ViewHolder extends RecyclerView.ViewHolder { TextView tvIngredient; ImageView btnDelete; ViewHolder(@NonNull View itemView) { super(itemView); tvIngredient = itemView.findViewById(R.id.tv_ingredient); btnDelete = itemView.findViewById(R.id.btn_delete); } } }
对应的Item布局(item_ingredient.xml):
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp" android:background="#f5f5f5" android:layout_marginEnd="8dp"> <TextView android:id="@+id/tv_ingredient" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:paddingEnd="8dp"/> <ImageView android:id="@+id/btn_delete" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/ic_delete"/> </LinearLayout>
4. 初始化RecyclerView与数据集
在Activity/Fragment中完成初始化,绑定删除逻辑:
List<String> ingredientList = new ArrayList<>(); IngredientAdapter adapter = new IngredientAdapter(ingredientList, position -> { ingredientList.remove(position); adapter.notifyItemRemoved(position); }); RecyclerView recyclerView = findViewById(R.id.ingredients_recycler); recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); recyclerView.setAdapter(adapter);
常见问题排查
- RecyclerView不显示:确认已设置横向LinearLayoutManager
- 回车无响应:检查按键监听中
event.getAction() == KeyEvent.ACTION_UP的判断,避免重复触发 - 删除失效:确认Adapter的删除回调已正确实现,且调用了
notifyItemRemoved
内容的提问来源于stack exchange,提问作者Emtenan YYC
相关产品推荐
相关产品推荐

