目前我已经出现了一个使用选项卡片段和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();
}


}

最佳答案

我猜绿色图标不是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();
    }
});

10-07 16:32
查看更多