gpt4 book ai didi

java - 如何使用列表在我的布局中的工具栏中放置一个按钮?

转载 作者:行者123 更新时间:2023-12-04 03:50:16 25 4
gpt4 key购买 nike

我有一个带有项目的布局。在 MenuActivity 中,我有一个列表,每次我将一个项目添加到列表中时,这个布局都会被复制,我会添加更多的饮料。我希望能够向此布局添加工具栏,而无需为列表中的每个新元素复制自身。如果我添加工具栏,会发生这种情况:

enter image description here

我希望它看起来像这样:

enter image description here

我的代码:

菜单 Activity :

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 是这样的:

enter image description here

<?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>

最佳答案

在此 XML 中,将工具栏保留在行项目中是错误的。您需要从 xml(列表中的单个)中提取工具栏内容。因为无论您在 xml 中有什么项目( View ),它都会被复制。如果您不想复制 View ,请将其取出并将其放在主 XML 中的 recyclerview 之外。作为引用,我附上草图以供您引用。

enter image description here

关于java - 如何使用列表在我的布局中的工具栏中放置一个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64534648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com