How to get data and change the background color of selected adapter item?

I am trying to change the bgcolor of recyclerview item along with getting the values at that position.I was able to get those values and change the bgcolor,but I was not able to do them properly.i am facing issues like: 1.first position item's background is always changed: i want to change it when i click on it. 2.if i am able to change the bgcolor accordingly then values are not getting fetched.

Here is my code: Adapter class for recyclerview

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyAd> {
private static Clickitem clickitem;
private ArrayList<String> names = new ArrayList<>();
private Context context;
private int row ;
String defaulter;
List<Item> items;
boolean isSeleceted = false;
private int selectedPosition = -1;
private String onlygps, onlysid, onlygpsnew, onlygpsnewer;
private SparseBooleanArray selectedItems = new SparseBooleanArray();


@NonNull
@Override
public MyAd onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(context).inflate(R.layout.list_recycler, viewGroup, false);
    return new MyAd(view);
}

MyAdapter(ArrayList<String> names, Context context) {
    this.names = names;
    this.context = context;
    clickitem = (Clickitem) context;
}

public static Clickitem getClickitem() {
    return clickitem;
}

public static void setClickitem(Clickitem clickitem) {
    MyAdapter.clickitem = clickitem;
}

@Override
public void onBindViewHolder(@NonNull final MyAd myAd, final int i) {

    String full = names.get(i);
    onlygps = full.substring(0, full.indexOf("/"));
    onlysid = full.substring(full.lastIndexOf("/") + 1);
    myAd.GPSname.setText(onlygps);
    myAd.logs.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String full = names.get(i);
            onlygpsnew = full.substring(0, full.indexOf("/"));
            Intent intent = new Intent(context, LogsHistory.class);
            intent.putExtra("GPS", onlygpsnew);
            context.startActivity(intent);

        }
    });
    if (row == i) {
        myAd.layout.setBackgroundColor(Color.YELLOW);
    }  else {
        myAd.layout.setBackgroundColor(Color.WHITE);
    }

    myAd.layout.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           String t = names.get(i);

                                           notifyDataSetChanged();
                                           clickitem.onitemsclicklistener(i, v);
                                           String fuller = names.get(i);
                                           onlygpsnewer = fuller.substring(0, fuller.indexOf("/"));
                                           Toast.makeText(context, onlygpsnewer + " " + "Selected", Toast.LENGTH_SHORT).show();


                                       }

                                   }
    );

}

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

public class MyAd extends RecyclerView.ViewHolder {
    TextView GPSname;
    LinearLayout layout, logs;
    TextView et;

    public MyAd(@NonNull View itemView) {
        super(itemView);
        GPSname = itemView.findViewById(R.id.gpsname);
        layout = itemView.findViewById(R.id.containerlayoiut);
        logs = itemView.findViewById(R.id.logs);
        et = itemView.findViewById(R.id.check);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickitem.onitemsclicklistener(getAdapterPosition(), view);
            }
        });


    }
}

public void setItemClick(MyAdapter.Clickitem click) {
    MyAdapter.clickitem = click;
}//setClick

public interface Clickitem {
    void onitemsclicklistener(int position, View view);
}

1 answer

  • answered 2019-04-15 06:11 Gökberk Yağcı

    Integer default value is 0 so your row value will change background color of first row when list showed.

    You can use

    private int row = -1 ;
    

    Instead of

    private int row ;
    

    Also you need to change row value with user clicked to row.

    myAd.layout.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                       ...
                                       row = myAd.adapterPosition();
                                       notifyDataSetChanged();
    

    Another way use selector like this. I can be more readable and easy.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/blue" android:state_pressed="true"/>
    <item android:drawable="@color/AliceBlue" android:state_focused="true"/>  
    <item android:drawable="@color/Azure"/>
    </selector>