For instance, I have a UI which consists of two CardView per recycleView "position" as shown below:
Whilst using the recycleview.view holder, I can determine the position at which the user clicked the screen by using the "getAdapterPosition()" onClick method but I am not able to determine which card view it was, i.e left or the right card view ( for instance in this Image example - given the position was 0, I am not able to determine if it was either "podcast" or "charts")
How do I determine which cardview the user has clicked -i.e left or right?
Below is my code for the RecycleViewAdpater:
public class DisplayGenresRecyclerViewAdapter extends RecyclerView.Adapter<DisplayGenresRecyclerViewAdapter.ViewHolder> {
// Cursor cursor = null;
Context context;
ArrayList<SuperGenreDouble> uniqueGenre;
private LayoutInflater inflater;
private ItemClickListener clickListener;
public DisplayGenresRecyclerViewAdapter(Context context, ArrayList<SuperGenreDouble> uniqueGenre) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.uniqueGenre = uniqueGenre;
}
public static String toTitleCase(String givenString) {
String[] arr = givenString.split(" ");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
sb.append(Character.toUpperCase(arr[i].charAt(0)))
.append(arr[i].substring(1)).append(" ");
}
return sb.toString().trim();
}
public static String removeSpacing(String givenString) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < givenString.length(); i++) {
if (!givenString.contains(" ")) {
sb.append(givenString.charAt(i));
}
}
return sb.toString().trim();
}
public static int getDominantColor(Bitmap bitmap) {
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
final int color = newBitmap.getPixel(0, 0);
newBitmap.recycle();
return color;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.display_super_genres_recycle_view_container, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String path;
String genreRight = uniqueGenre.get(position).superGenre1;
String imageArtistRight = uniqueGenre.get(position).superGenreImageUrl1;
String genreLeft = uniqueGenre.get(position).superGenre2;
String imageArtistLeft = uniqueGenre.get(position).superGenreImageUrl2;
if (genreLeft != null) {
holder.textViewLeft.setText(toTitleCase(genreLeft));
try {
// get the image
Picasso.get().load(imageArtistLeft).into(holder.imageGenreLeft, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
// set the color
Bitmap bitmap = ((BitmapDrawable) holder.imageGenreLeft.getDrawable()).getBitmap();
int color = getDominantColor(bitmap);
holder.cardColourLeft.setBackgroundColor(color);
holder.textViewLeft.setTextColor(lightenColor(color));
}
@Override
public void onError(Exception e) {
}
});
} catch (Exception e) {
}
}
if (genreRight != null) {
holder.textViewRight.setText(toTitleCase(genreRight));
try {
// get the image
Picasso.get().load(imageArtistRight).into(holder.imageGenreRight, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
// set the color
Bitmap bitmap = ((BitmapDrawable) holder.imageGenreRight.getDrawable()).getBitmap();
int color = getDominantColor(bitmap);
holder.cardColourRight.setBackgroundColor(color);
holder.textViewRight.setTextColor(lightenColor(color));
}
@Override
public void onError(Exception e) {
}
});
} catch (Exception e) {
}
}
}
@ColorInt
int lightenColor(@ColorInt int color) {
return Color.rgb(255-(color >> 8)&0xFF, 255-(color >> 8)&0xFF, 255-(color >> 8)&0xFF);
}
@Override
public int getItemCount() {
return uniqueGenre.size();
}
void setClickListener(ItemClickListener itemClickListener) {
Log.d("onBindViewHolder", "onBindViewHolder: length");
this.clickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textViewLeft;
TextView textViewRight;
ImageView imageGenreLeft;
ImageView imageGenreRight;
CardView cardColourRight;
CardView cardColourLeft;
ViewHolder(View itemView) {
super(itemView);
textViewLeft = itemView.findViewById(R.id.textViewLeft);
textViewRight = itemView.findViewById(R.id.textViewRight);
imageGenreLeft = itemView.findViewById(R.id.imageGenreLeft);
imageGenreRight = itemView.findViewById(R.id.imageGenreRight);
cardColourRight = itemView.findViewById(R.id.cardViewRight);
cardColourLeft = itemView.findViewById(R.id.cardViewLeft);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Log.d("myPos", getAdapterPosition());
}
}
}