October 24, 2024
Chicago 12, Melborne City, USA
Android

When I add buttons in a RecyclerView, and the the top button is pressed, why do other buttons at the bottom of the view also get automatically presed?


When I click the buttons on the view, other buttons at the bottom on the view automatically get pressed when I scroll. How can I ensure that only one button is pressed, and maintain the state of the previously pressed buttons?

The adapter and the xml are attached`

public class SingleAdapter extends RecyclerView.Adapter<SingleAdapter.SingleViewHolder> {

private Context context;
private ArrayList<Employee> employees;

// if checkedPosition = -1, there is no default selection
// if checkedPosition = 0, 1st item is selected by default
private int checkedPosition = 0;

public SingleAdapter(Context context, ArrayList<Employee> employees) {
    this.context = context;
    this.employees = employees;
}

public void setEmployees(ArrayList<Employee> employees) {
    this.employees = new ArrayList<>();
    this.employees = employees;
    notifyDataSetChanged();
}

@NonNull
@Override
public SingleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.item_employee, viewGroup, false);
    return new SingleViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull SingleViewHolder singleViewHolder, int position) {
    singleViewHolder.bind(employees.get(position));
}

@Override
public int getItemCount() {
    return employees.size();
}

class SingleViewHolder extends RecyclerView.ViewHolder {

    private TextView textView;
    private ImageView imageView;
    private Button yesbutton;
    private Button nobutton;

    SingleViewHolder(@NonNull View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.textView);
        imageView = itemView.findViewById(R.id.imageView);
        yesbutton = itemView.findViewById(R.id.yesbutton);
        nobutton = itemView.findViewById(R.id.nobutton);
    }

    void bind(final Employee employee) {
        if (checkedPosition == -1) {
            imageView.setVisibility(View.GONE);
            yesbutton.setOnClickListener(null);
            nobutton.setOnClickListener(null);
        } else {
            if (checkedPosition == getAdapterPosition()) {
                imageView.setVisibility(View.VISIBLE);
            } else {
                imageView.setVisibility(View.GONE);
                yesbutton.setOnClickListener(null);
                nobutton.setOnClickListener(null);
            }
        }
        textView.setText(employee.getName());

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageView.setVisibility(View.VISIBLE);
                if (checkedPosition != getAdapterPosition()) {
                    notifyItemChanged(checkedPosition);
                    checkedPosition = getAdapterPosition();
                }
            }
        });

        nobutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                employee.setYesButtonColor(Color.RED);
                employee.setNoButtonColor(Color.GREEN);

                yesbutton.setBackgroundColor(employee.getYesButtonColor());
                nobutton.setBackgroundColor(employee.getNoButtonColor());
            }
        });

        yesbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                employee.setYesButtonColor(Color.GREEN);
                employee.setNoButtonColor(Color.RED);

                yesbutton.setBackgroundColor(employee.getYesButtonColor());
                nobutton.setBackgroundColor(employee.getNoButtonColor());

                if (checkedPosition != getAdapterPosition()) {
                    if(employee.getYesButtonColor() != null) {
                        yesbutton.setBackgroundColor(employee.getYesButtonColor());
                    }
                    if(employee.getNoButtonColor() != null) {
                        nobutton.setBackgroundColor(employee.getNoButtonColor());
                    }
                }
            }
        });
    }
}

public Employee getSelected() {
    if (checkedPosition != -1) {
        return employees.get(checkedPosition);
    }
    return null;
}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SingleSelectionActivity">

    <Button
        android:id="@+id/btnGetSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_gravity="center"
        android:text="Get Selected" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.recyclerview.widget.RecyclerView>

</LinearLayout>



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video