Recyclerview 独立按钮和计数器
Posted
技术标签:
【中文标题】Recyclerview 独立按钮和计数器【英文标题】:Recyclerview independent buttons and counter 【发布时间】:2020-08-18 23:00:20 【问题描述】:我有问题。 2 个按钮(+ 和 -)具有相同的计数器。例如,当我增加可乐(假设我们想要 2 杯可乐)时,我按下按钮 2 次,一切正常,但只要我想要炸薯条(1 炸薯条和 2 杯可乐),炸薯条的柜台就会增加到3 而它应该是 1 因为我第一次点击它。下图显示了问题。
以下 xml 和 java 类文件重建您看到的图像。或许你能帮帮我,我会很感激的。
OrderActivity.java
package com.nfc.netvision;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class OrderActivity extends AppCompatActivity
RecyclerView recyclerView;
ArrayList<ModelOrder> orderArrayList;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
recyclerView = findViewById(R.id.recyclerview_order_scroll);
orderArrayList = new ArrayList<>();
orderArrayList.add(new ModelOrder(R.drawable.coke, "Coka Cola", "Kaltes Getränml", "6"));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, "Coka Cola", "Kaltes Getränml", "10"));
orderArrayList.add(new ModelOrder(R.drawable.water, "Coka Cola", "Kaltes Getränml", "20"));
orderArrayList.add(new ModelOrder(R.drawable.burger, "Coka Cola", "Kaltes Getränml", "30"));
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
RecyclerView.LayoutManager recLiLayoutManager = layoutManager;
recyclerView.setLayoutManager(recLiLayoutManager);
OrderAdapter adapter = new OrderAdapter(this, orderArrayList);
recyclerView.setAdapter(adapter);
OrderAdapter.java
package com.nfc.netvision;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder>
int value = 0; //Global
private Context mContext;
private ArrayList<ModelOrder> nList;
OrderAdapter(Context context, ArrayList<ModelOrder> list)
mContext = context;
nList = list;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.recyclerview_order_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position)
ModelOrder orderItem = nList.get(position);
ImageView image = holder.item_image;
TextView name, place, price;
name = holder.item_name;
place = holder.item_place;
price = holder.item_price;
image.setImageResource(orderItem.getImage());
name.setText(orderItem.getName());
place.setText(orderItem.getPlace());
price.setText(orderItem.getPrice());
holder.order_item_minus.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
value = value - 1;
holder.order_item_count.setText("" + value);
);
holder.order_item_plus.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
value = value + 1;
holder.order_item_count.setText("" + value);
);
@Override
public int getItemCount()
return nList.size();
public class ViewHolder extends RecyclerView.ViewHolder
ImageView item_image;
TextView item_name, item_place, item_price,order_item_minus,order_item_count, order_item_plus;
public ViewHolder(@NonNull View itemView)
super(itemView);
item_image = itemView.findViewById(R.id.order_item_image);
item_name = itemView.findViewById(R.id.order_item_name);
item_place = itemView.findViewById(R.id.order_item_place);
item_price = itemView.findViewById(R.id.order_item_price);
order_item_minus = itemView.findViewById(R.id.order_item_minus);
order_item_plus = itemView.findViewById(R.id.order_item_plus);
order_item_count = itemView.findViewById(R.id.order_item_count);
recyclerViewer.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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_
android:layout_
android:layout_margin="10dp">
<LinearLayout
android:weightSum="10"
android:layout_
android:layout_>
<ImageView
android:id="@+id/order_item_image"
android:layout_
android:layout_weight="4"
android:layout_
android:scaleType="centerCrop"
android:src="@drawable/coke"/>
<LinearLayout
android:layout_
android:layout_weight="4"
android:layout_
android:orientation="vertical">
<TextView
android:id="@+id/order_item_name"
android:textSize="18sp"
android:layout_
android:layout_
android:textStyle="bold"
android:text="Name"/>
<TextView
android:id="@+id/order_item_place"
android:text="Description"
android:textSize="14sp"
android:layout_
android:layout_/>
</LinearLayout>
<TextView
android:id="@+id/order_item_price"
android:text="€ Preis"
android:layout_
android:layout_weight="2"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#fff"
android:background="@drawable/capsule_order"
android:layout_/>
<LinearLayout
android:layout_
android:layout_
android:layout_gravity="bottom">
<TextView
android:id="@+id/order_item_minus"
android:layout_
android:layout_
android:textSize="25dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/capsule_order"
android:textColor="#fff"
android:textStyle="bold"
android:text="-"/>
<TextView
android:id="@+id/order_item_count"
android:layout_
android:layout_
android:textSize="25dp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:text="00"/>
<TextView
android:id="@+id/order_item_plus"
android:layout_
android:layout_
android:textSize="25dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/capsule_order"
android:textColor="#fff"
android:textStyle="bold"
android:text="+"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
activity_order.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_
android:layout_
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_
android:layout_
android:clipToPadding="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_order_scroll"
android:layout_
android:layout_
>
</androidx.recyclerview.widget.RecyclerView>
</androidx.core.widget.NestedScrollView>
【问题讨论】:
【参考方案1】:因为你总是访问你在 Adapter 中初始化的 value 变量
解决方案是 将 value 添加到您的 ModelOrder 并在模型中启动它
然后访问它
所以..每一行都会不同
OrderActivity.java // 用 counter = 0
初始化每个模型orderArrayList.add(new ModelOrder(R.drawable.coke, "Coka Cola", "Kaltes Getränml", "6",0));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, "Coka Cola", "Kaltes Getränml", "10",0));
orderArrayList.add(new ModelOrder(R.drawable.water, "Coka Cola", "Kaltes Getränml", "20",0));
orderArrayList.add(new ModelOrder(R.drawable.burger, "Coka Cola", "Kaltes Getränml", "30",0));
OrderAdapter.java
删除这一行
int 值 = 0; //全局
holder.order_item_minus.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
orderItem.setCounter(orderItem.getCounter()-1);
holder.order_item_count.setText("" + orderItem.getCounter());
);
holder.order_item_plus.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
orderItem.setCounter(orderItem.getCounter()+1);
holder.order_item_count.setText("" + orderItem.getCounter());
);
【讨论】:
感谢您的评论。有没有可能你可以给我看代码?我会非常感激,因为我是初学者:) @Marcel.david1 \\我的答案已更新..检查一下,我希望它有效 嗨@Marcel.david1,我认为El-Banna 建议与我在下面建议的相同:非常简单,只需在对象的模型类中添加数量变量并将0 设置为默认值。单击按钮更改数量值并设置文本视图【参考方案2】:您必须在 ModelOrder 中添加具有 INT 数据类型的 'quantity' 属性 因此,根据加/减按钮单击增加/减少对象中 'quantity' 的值,并在计数器文本视图中设置 'quantity' 值
【讨论】:
以上是关于Recyclerview 独立按钮和计数器的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView smoothScroll位于中心位置。安卓