[英]How to put a button in toolbar in my layout with a list?
我有一個帶有項目的布局。 在 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);
}
菜單型號:
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;
}
菜單適配器:
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);
}
}
}
這就是我的 xml 的樣子:
<?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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.