英文:
How to put a button in toolbar in my layout with a list?
问题
以下是您提供的代码翻译后的部分:
MenuActivity:
public class MenuActivity extends AppCompatActivity {
// ... 其他部分未翻译 ...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
// 创建一个ArrayList
modelList = new ArrayList<>();
modelList.add(new MenuModel("绿茶", getString(R.string.greentea), R.drawable.greentea));
modelList.add(new MenuModel("拿铁", getString(R.string.latte), R.drawable.late));
// ... 添加其他菜单项 ...
// 初始化RecyclerView
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 设置适配器
mAdapter = new MenuAdapter(this, modelList);
recyclerView.setAdapter(mAdapter);
}
}
MenuModel:
public class MenuModel {
String mFoodName;
String mFoodDetail;
int mFoodPhoto;
public MenuModel(String mFoodName, String mFoodDetail, int mFoodPhoto) {
this.mFoodName = mFoodName;
this.mFoodDetail = mFoodDetail;
this.mFoodPhoto = mFoodPhoto;
}
// getter 和 setter 方法未翻译
}
MenuAdapter:
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder> {
// ... 其他部分未翻译 ...
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.menu_list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// 获取菜单项信息并设置到视图中
String drinkName = modelList.get(position).getmFoodName();
String drinkDescription = modelList.get(position).getmFoodDetail();
int imageResource = modelList.get(position).getmFoodPhoto();
holder.mDrinkName.setText(drinkName);
holder.mDrinkDescription.setText(drinkDescription);
holder.imageView.setImageResource(imageResource);
}
// ... 其他部分未翻译 ...
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// ... 其他部分未翻译 ...
}
}
XML布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:background="@color/color4"
tools:context=".MenuScreen.MenuActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_marginLeft="320dp"
android:text="MyButton"
android:layout_height="match_parent"></Button>
</androidx.appcompat.widget.Toolbar>
<!-- 其他部分未翻译 -->
</LinearLayout>
英文:
I have a layout with an item. In MenuActivity I have a list, every time I add an item to the list, this layout is duplicated and I add more drinks. I would like to be able to add a toolbar to this layout without duplicating itself for each new element in the list. If I add a toolbar, this happens:
And I want it to look like this:
My code:
MenuActivity:
public class MenuActivity extends AppCompatActivity {
List<MenuModel> modelList;
RecyclerView recyclerView;
MenuAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
// creating an arraylist
modelList = new ArrayList<>();
modelList.add(new MenuModel("Green Tea", getString(R.string.greentea), R.drawable.greentea ));
modelList.add(new MenuModel("Latte", getString(R.string.latte), R.drawable.late));
modelList.add(new MenuModel("Orange Smoothie", getString(R.string.orangesmoothie), R.drawable.orange));
modelList.add(new MenuModel("Orange Vanilla", getString(R.string.orangevanilla), R.drawable.orangevanilla));
modelList.add(new MenuModel("Cappucino", getString(R.string.cappcuni), R.drawable.cappcunio));
modelList.add(new MenuModel("Thai Tea", getString(R.string.thaitea), R.drawable.thaitea));
modelList.add(new MenuModel("Tea", getString(R.string.tea), R.drawable.tea));
modelList.add(new MenuModel("Bubble Tea", getString(R.string.bubbletea), R.drawable.milk));
modelList.add(new MenuModel("Matcha", getString(R.string.match), R.drawable.match));
// recyclerview
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(null));
// adapter
mAdapter = new MenuAdapter(this, modelList);
recyclerView.setAdapter(mAdapter);
}
MenuModel:
public class MenuModel {
String mFoodName;
String mFoodDetail;
int mFoodPhoto;
public MenuModel(String mFoodName, String mFoodDetail, int mFoodPhoto) {
this.mFoodName = mFoodName;
this.mFoodDetail = mFoodDetail;
this.mFoodPhoto = mFoodPhoto;
}
public String getmFoodName() {
return mFoodName;
}
public void setmFoodName(String mFoodName) {
this.mFoodName = mFoodName;
}
public String getmFoodDetail() {
return mFoodDetail;
}
public void setmFoodDetail(String mFoodDetail) {
this.mFoodDetail = mFoodDetail;
}
public int getmFoodPhoto() {
return mFoodPhoto;
}
public void setmFoodPhoto(int mFoodPhoto) {
this.mFoodPhoto = mFoodPhoto;
}
MenuAdapter:
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder> {
List<MenuModel> modelList;
Context context;
public MenuAdapter(Context context, List<MenuModel> modelList) {
this.context = context;
this.modelList = modelList;
}
@Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.menu_list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder( ViewHolder holder, int position) {
// here we will find the position and start setting the output on our views
String nameofFood = modelList.get(position).getmFoodName();
String detailofFood = modelList.get(position).getmFoodDetail();
int images = modelList.get(position).getmFoodPhoto();
holder.mDrinkName.setText(nameofFood);
holder.mDrinkDescription.setText(detailofFood);
holder.imageView.setImageResource(images);
}
@Override
public int getItemCount() {
return modelList.size();
}
// in order to make our views responsive we can implement onclicklistener on our recyclerview
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// here we will find the views on which we will inflate our data
TextView mDrinkName, mDrinkDescription;
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
mDrinkName = itemView.findViewById(R.id.coffeeName);
mDrinkDescription = itemView.findViewById(R.id.description);
imageView = itemView.findViewById(R.id.coffeeImage);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// lets get the position of the view in list and then work on it
int position = getAdapterPosition();
if (position == 0) {
Intent intent = new Intent(context, InfoActivity.class);
context.startActivity(intent);
}
if (position == 1) {
Intent intent2 = new Intent(context, LatteActivity.class);
context.startActivity(intent2);
}
}
}
This is how my xml is:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:background="@color/color4"
tools:context=".MenuScreen.MenuActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_marginLeft="320dp"
android:text="MyButton"
android:layout_height="match_parent"></Button>
</androidx.appcompat.widget.Toolbar>
<androidx.cardview.widget.CardView
app:cardBackgroundColor="@color/color1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
app:cardElevation="10dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="88dp"
android:layout_height="88dp"
android:id="@+id/coffeeImage"
android:layout_marginTop="25dp"
android:src="@drawable/greentea"/>
<TextView
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text=""
android:textSize="12sp"
android:layout_marginRight="12dp"
android:layout_below="@+id/coffeeName"
android:layout_marginLeft="12dp"
android:id="@+id/description"
android:layout_toRightOf="@+id/coffeeImage"/>
<TextView
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="25dp"
android:id="@+id/coffeeName"
android:textStyle="italic"
android:textSize="26sp"
android:layout_toRightOf="@+id/coffeeImage"
tools:text="Green Tea"/>
<TextView
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/description"
android:layout_toRightOf="@+id/coffeeImage"
android:id="@+id/clickheretoorder"
android:layout_marginLeft="12dp"
android:layout_marginTop="4dp"
tools:text="Click to Order"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<!-- end snippet -->
答案1
得分: 1
在这个XML中,你正在犯一个错误,即将工具栏保留在行项目内部。你需要将工具栏的内容从XML中提取出来(针对列表的单个项目)。因为无论你的XML中有什么项目(视图),它都会被复制。
如果你不希望某个视图被复制,请将其移到主XML中的RecyclerView之外。供参考,我附上了草图以供帮助。
英文:
In this XML, you are making a mistake by keeping toolbar inside a row item. You need to pull out the tool bar stuff out of the xml (the single for list).Because it will be duplicated whatever item(view) you have in your xml.
If you don't a view to be duplicated get it out and place that out side the recyclerview in your main XML. For reference, I am attaching sketch for your help.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论