怎样在我的布局中的工具栏中放置一个按钮,同时还有一个列表?

huangapple go评论144阅读模式
英文:

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&lt;MenuModel&gt; 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&lt;&gt;();
    modelList.add(new MenuModel(&quot;Green Tea&quot;, getString(R.string.greentea), R.drawable.greentea ));
    modelList.add(new MenuModel(&quot;Latte&quot;, getString(R.string.latte), R.drawable.late));
    modelList.add(new MenuModel(&quot;Orange Smoothie&quot;, getString(R.string.orangesmoothie), R.drawable.orange));
    modelList.add(new MenuModel(&quot;Orange Vanilla&quot;, getString(R.string.orangevanilla), R.drawable.orangevanilla));
    modelList.add(new MenuModel(&quot;Cappucino&quot;, getString(R.string.cappcuni), R.drawable.cappcunio));
    modelList.add(new MenuModel(&quot;Thai Tea&quot;, getString(R.string.thaitea), R.drawable.thaitea));
    modelList.add(new MenuModel(&quot;Tea&quot;, getString(R.string.tea), R.drawable.tea));
    modelList.add(new MenuModel(&quot;Bubble Tea&quot;, getString(R.string.bubbletea), R.drawable.milk));
    modelList.add(new MenuModel(&quot;Matcha&quot;, 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&lt;MenuAdapter.ViewHolder&gt; {

List&lt;MenuModel&gt; modelList;
Context context;

public MenuAdapter(Context context, List&lt;MenuModel&gt; 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 -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    android:layout_height=&quot;wrap_content&quot;
android:background=&quot;@color/color4&quot;
    tools:context=&quot;.MenuScreen.MenuActivity&quot;&gt;

&lt;androidx.appcompat.widget.Toolbar
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;
    &lt;Button
        android:layout_width=&quot;wrap_content&quot;
        android:layout_marginLeft=&quot;320dp&quot;
        android:text=&quot;MyButton&quot;
        android:layout_height=&quot;match_parent&quot;&gt;&lt;/Button&gt;

&lt;/androidx.appcompat.widget.Toolbar&gt;
    &lt;androidx.cardview.widget.CardView
        app:cardBackgroundColor=&quot;@color/color1&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;150dp&quot;
        android:layout_marginLeft=&quot;11dp&quot;
        android:layout_marginRight=&quot;11dp&quot;
        android:layout_marginTop=&quot;7dp&quot;
        android:layout_marginBottom=&quot;7dp&quot;
        app:cardElevation=&quot;10dp&quot;
        app:cardCornerRadius=&quot;10dp&quot;&gt;

        &lt;RelativeLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;&gt;


            &lt;ImageView
                android:layout_width=&quot;88dp&quot;
                android:layout_height=&quot;88dp&quot;
                android:id=&quot;@+id/coffeeImage&quot;
                android:layout_marginTop=&quot;25dp&quot;
                android:src=&quot;@drawable/greentea&quot;/&gt;

            &lt;TextView
                android:textColor=&quot;#ffffff&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                tools:text=&quot;&quot;
                android:textSize=&quot;12sp&quot;
                android:layout_marginRight=&quot;12dp&quot;
                android:layout_below=&quot;@+id/coffeeName&quot;
                android:layout_marginLeft=&quot;12dp&quot;
                android:id=&quot;@+id/description&quot;
                android:layout_toRightOf=&quot;@+id/coffeeImage&quot;/&gt;
            &lt;TextView
                android:textColor=&quot;#ffffff&quot;

                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginLeft=&quot;12dp&quot;
                android:layout_marginTop=&quot;25dp&quot;
                android:id=&quot;@+id/coffeeName&quot;
                android:textStyle=&quot;italic&quot;
                android:textSize=&quot;26sp&quot;
                android:layout_toRightOf=&quot;@+id/coffeeImage&quot;
                tools:text=&quot;Green Tea&quot;/&gt;

            &lt;TextView
                android:textColor=&quot;#ffffff&quot;
                android:layout_width=&quot;wrap_content&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_below=&quot;@+id/description&quot;
                android:layout_toRightOf=&quot;@+id/coffeeImage&quot;
                android:id=&quot;@+id/clickheretoorder&quot;
                android:layout_marginLeft=&quot;12dp&quot;
                android:layout_marginTop=&quot;4dp&quot;
                tools:text=&quot;Click  to Order&quot;/&gt;




        &lt;/RelativeLayout&gt;

    &lt;/androidx.cardview.widget.CardView&gt;


&lt;/LinearLayout&gt;

<!-- 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.

怎样在我的布局中的工具栏中放置一个按钮,同时还有一个列表?

huangapple
  • 本文由 发表于 2020年10月26日 17:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64534648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定