[英]How do I determine which card view is clicked on the recycelview layout?
例如,我有一個 UI,它由每個 recycleView“位置”兩個 CardView 組成,如下所示:
在使用 recycleview.view 持有人的同時,我可以通過使用“getAdapterPosition()”onClick 方法確定用戶單擊屏幕的 position,但我無法確定它是哪個卡視圖,即左卡視圖或右卡視圖(例如在此圖像示例中 - 鑒於 position 為 0,我無法確定它是“播客”還是“圖表”)
如何確定用戶單擊了哪個卡片視圖 - 即向左還是向右?
下面是我的 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
中獲取 position 時,您必須注意您獲取的是 ViewHolder 的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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.