问题描述
例如,我有一个 UI,其中每个 recycleView“位置”由两个 CardView 组成,如下所示:
在使用 recycleview.view 持有者时,我可以通过使用“getAdapterPosition()”onClick 方法来确定用户单击屏幕的位置,但我无法确定它是哪个卡片视图,即左侧或右侧卡片视图(例如在此 Image 示例中 - 鉴于位置为 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,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());
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)