Recyclerview独立按钮和计数器

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

Recyclerview independent buttons and counter

问题

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);
    }
}
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_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <!-- Contents of the XML layout -->

</androidx.cardview.widget.CardView>
<!-- activity_order.xml -->
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview_order_scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- RecyclerView content -->
        </androidx.recyclerview.widget.RecyclerView>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
英文:

I have a problem. The 2 buttons (+ and -) have the same counter. When I for example increase coke (lets say we want 2 cokes) i press the button 2 times and everything works, but as soon as i want french fries ( 1 french fries in addition to the 2 cokes) the counter at french fries increases to 3 whereas it should be 1 because i clicked it the first time. The following picture showes the problem.

Recyclerview独立按钮和计数器

The following xml and java classfile reconstruct the image you are seeing. Maybe you can help me out, i would be very thankful.

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&lt;ModelOrder&gt; 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&lt;&gt;();
orderArrayList.add(new ModelOrder(R.drawable.coke, &quot;Coka Cola&quot;, &quot;Kaltes Getr&#228;nml&quot;, &quot;6&quot;));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, &quot;Coka Cola&quot;, &quot;Kaltes Getr&#228;nml&quot;, &quot;10&quot;));
orderArrayList.add(new ModelOrder(R.drawable.water, &quot;Coka Cola&quot;, &quot;Kaltes Getr&#228;nml&quot;, &quot;20&quot;));
orderArrayList.add(new ModelOrder(R.drawable.burger, &quot;Coka Cola&quot;, &quot;Kaltes Getr&#228;nml&quot;, &quot;30&quot;));
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&lt;OrderAdapter.ViewHolder&gt; {
int value = 0; //Global
private Context mContext;
private ArrayList&lt;ModelOrder&gt; nList;
OrderAdapter(Context context, ArrayList&lt;ModelOrder&gt; 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(&quot;&quot; + value);
}
});
holder.order_item_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
value = value + 1;
holder.order_item_count.setText(&quot;&quot; + 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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.cardview.widget.CardView
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:layout_height=&quot;wrap_content&quot;
android:layout_margin=&quot;10dp&quot;&gt;
&lt;LinearLayout
android:weightSum=&quot;10&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/order_item_image&quot;
android:layout_width=&quot;0dp&quot;
android:layout_weight=&quot;4&quot;
android:layout_height=&quot;128dp&quot;
android:scaleType=&quot;centerCrop&quot;
android:src=&quot;@drawable/coke&quot;/&gt;
&lt;LinearLayout
android:layout_width=&quot;0dp&quot;
android:layout_weight=&quot;4&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;TextView
android:id=&quot;@+id/order_item_name&quot;
android:textSize=&quot;18sp&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:textStyle=&quot;bold&quot;
android:text=&quot;Name&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/order_item_place&quot;
android:text=&quot;Description&quot;
android:textSize=&quot;14sp&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;/&gt;
&lt;/LinearLayout&gt;
&lt;TextView
android:id=&quot;@+id/order_item_price&quot;
android:text=&quot;€ Preis&quot;
android:layout_width=&quot;0dp&quot;
android:layout_weight=&quot;2&quot;
android:layout_gravity=&quot;center&quot;
android:gravity=&quot;center&quot;
android:textColor=&quot;#fff&quot;
android:background=&quot;@drawable/capsule_order&quot;
android:layout_height=&quot;30dp&quot;/&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;bottom&quot;&gt;
&lt;TextView
android:id=&quot;@+id/order_item_minus&quot;
android:layout_width=&quot;28dp&quot;
android:layout_height=&quot;28dp&quot;
android:textSize=&quot;25dp&quot;
android:layout_gravity=&quot;center&quot;
android:gravity=&quot;center&quot;
android:background=&quot;@drawable/capsule_order&quot;
android:textColor=&quot;#fff&quot;
android:textStyle=&quot;bold&quot;
android:text=&quot;-&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/order_item_count&quot;
android:layout_width=&quot;28dp&quot;
android:layout_height=&quot;28dp&quot;
android:textSize=&quot;25dp&quot;
android:layout_gravity=&quot;center&quot;
android:gravity=&quot;center&quot;
android:textColor=&quot;#000&quot;
android:textStyle=&quot;bold&quot;
android:text=&quot;00&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/order_item_plus&quot;
android:layout_width=&quot;28dp&quot;
android:layout_height=&quot;28dp&quot;
android:textSize=&quot;25dp&quot;
android:layout_gravity=&quot;center&quot;
android:gravity=&quot;center&quot;
android:background=&quot;@drawable/capsule_order&quot;
android:textColor=&quot;#fff&quot;
android:textStyle=&quot;bold&quot;
android:text=&quot;+&quot;/&gt;
&lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;

activity_order.xml

&lt;androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;androidx.core.widget.NestedScrollView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:clipToPadding=&quot;true&quot;
app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/recyclerview_order_scroll&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
&gt;
&lt;/androidx.recyclerview.widget.RecyclerView&gt;
&lt;/androidx.core.widget.NestedScrollView&gt;

答案1

得分: 1

因为您总是访问在适配器中初始化的 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 value = 0; //Global
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());
    }
});
英文:

because you always access the value variable that you initialized in Adapter

solution is to
add value to your ModelOrder and initiate it in model

then access it

so .. it will be different for every row

OrderActivity.java // initialize every model with counter = 0

orderArrayList.add(new ModelOrder(R.drawable.coke, &quot;Coka Cola&quot;, &quot;Kaltes Getr&#228;nml&quot;, &quot;6&quot;,0));
orderArrayList.add(new ModelOrder(R.drawable.fastfood, &quot;Coka Cola&quot;, &quot;Kaltes Getr&#228;nml&quot;, &quot;10&quot;,0));
orderArrayList.add(new ModelOrder(R.drawable.water, &quot;Coka Cola&quot;, &quot;Kaltes Getr&#228;nml&quot;, &quot;20&quot;,0));
orderArrayList.add(new ModelOrder(R.drawable.burger, &quot;Coka Cola&quot;, &quot;Kaltes Getr&#228;nml&quot;, &quot;30&quot;,0));

OrderAdapter.java

remove this line

> int value = 0; //Global

holder.order_item_minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
orderItem.setCounter(orderItem.getCounter()-1);
holder.order_item_count.setText(&quot;&quot; + 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(&quot;&quot; + orderItem.getCounter());
}
});

答案2

得分: 0

你需要在你的 ModelOrder 中添加一个属性,名为 'quantity',其数据类型为整数(INT)。然后,根据加号和减号按钮的点击,对一个对象的 'quantity' 值进行增加/减少操作,并将 'quantity' 的值设置在你的计数文本视图中。

英文:

You've to add 'quantity' attribute which has INT data type in your ModelOrder
So, increment/decrement value of 'quantity' in an object according to plus/minus button click, and set 'quantity' value in your counter textview

huangapple
  • 本文由 发表于 2020年5月4日 21:36:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61593571.html
匿名

发表评论

匿名网友

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

确定