目前我已经出现了一个使用标签片段和RecyclerView的屏幕,如下所示。我想知道我将如何处理按钮点击该红色圆圈。每次我点击,我都会得到我想要添加到购物车中的产品的详细信息。由于我出来的屏幕是使用RecyclerView,所以我很困惑如何获得我想要的产品的所有细节。 enter image description here
适配器代码
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView row_image;
TextView title;
TextView description;
public ViewHolder(View v, Context c) {
super(v);
mContext = c;
row_image = (ImageView) v.findViewById(R.id.row_image);
title = (TextView) v.findViewById(R.id.row_title);
description = (TextView) itemView.findViewById(R.id.row_description);
}
}
public CatalogPageAdapter(List<CatalogViewData> dataset) {
mDataset = dataset;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.catalog_layout, parent, false);
return new ViewHolder(v,parent.getContext());
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, int i) {
final CatalogViewData fakePageVH = mDataset.get(i);
String points = Double.toString(Double.parseDouble(fakePageVH.getDescription()));
viewHolder.title.setText(fakePageVH.getTitle());
viewHolder.description.setText(points);
String var = fakePageVH.getImage();
new getImage(var, viewHolder).execute();
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Title: " + fakePageVH.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
public class getImage extends AsyncTask<String, String, String> {
String imageUrl;
ViewHolder holder;
Bitmap downloadedBitmap;
Bitmap scaled;
public getImage(String url, ViewHolder h){
imageUrl = url;
holder = h;
}
@Override
protected String doInBackground(String... args) {
try {
URL url = new URL(imageUrl);
downloadedBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
scaled = Bitmap.createScaledBitmap(downloadedBitmap, 150, 100, true);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "done";
}
@Override
protected void onPostExecute(String result) {
holder.row_image.setImageBitmap(scaled);
}
}
@Override
public int getItemCount() {
return mDataset == null ? 0 : mDataset.size();
}
}
答案 0 :(得分:2)
我猜绿色图标不是ImageView
,否则将是一个愚蠢的问题。添加一个空的ImageView
,将其完全放在您的圈子上。然后将其添加到您的查看者中并使用viewHolder.dummyImageView.setOnClickListener
。
修改的
的xml:
<ImageView
android:id="@+id/dummyImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
VievHolder:
public ViewHolder(View v, Context c) {
super(v);
dummyImage = (ImageView) v.findViewById(R.id.dummyImageView);
}
OnClickListener:
ImageView dummyImage;
viewHolder.dummyImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Click!", Toast.LENGTH_SHORT).show();
}
});