如何确定在recycelview布局上单击了哪个卡片视图?



例如,我有一个UI,它由每个recycleView "position"如下所示:

形象在使用recycleview时。,我可以通过使用getAdapterPosition()来确定用户单击屏幕的位置。onClick方法,但我无法确定它是哪个卡视图,即左或右卡视图(例如在这个图像示例中-给定位置为0,我无法确定它是否是"podcast"或"charts"

如何确定用户单击了哪个cardview -i。向左还是向右?

下面是我的代码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());

}
}
}

当您在RecyclerView中获得位置时,您必须注意您在ViewHolder中获得ViewHolder而不是View Elements的位置。所以对于你的问题,你能做的就是为你想要的View实现onClickListener,就像这样:

ViewHolder(View itemView) {
cardColourRight = itemView.findViewById(R.id.cardViewRight);
cardColourLeft = itemView.findViewById(R.id.cardViewLeft);
//add these
cardColourRight.setOnClickListener(this);
cardColourLeft.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view == R.id.cardViewLeft){
//User has clicked Left
}
else if(view == R.id.cardViewRight){
//User has clicked Right
}
}

相关内容

  • 没有找到相关文章

最新更新