I'm trying to change the background color of the clicked item in RecyclerView from the adapter and it works, but the problem is when I click on position 1 it changes the color of position 1 and 7, and when I click on position 2 it changes the color of position 2 and 8 and so on ...
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.viewHolder> {
private ArrayList<String> name = new ArrayList<>();
private Context context;
boolean added = false;
public RecyclerViewAdapter(ArrayList<String> name, Context context) {
this.name = name;
this.context = context;
}
@Override
public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal_listview, parent, false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(final viewHolder holder, final int position) {
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final Dialog dialog = new Dialog(view.getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.add_item_dialog_small);
Window window = dialog.getWindow();
window.setLayout(500, 450);
Button addToList = (Button) dialog.findViewById(R.id.addToList);
addToList.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v) {
holder.cardView.setBackgroundColor(R.color.layer4);
dialog.dismiss();
}
});
dialog.show();
}
});
}
Edit: here is the cardView :
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="300dp"
android:layout_height="320dp"
android:layout_margin="10dp"
android:background="@color/layer2"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:background="@color/layer3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="@+id/textViewItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Item"
android:textColor="@color/add_button"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/item_number"
android:textColor="@color/text_view_color"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewItemNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:textColor="@color/second_color"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>