我如何知道从recyclerview中选择的任何图像?

时间:2016-09-15 17:19:07

标签: android json

你好我有这个json数据a link

我将jsonArray解析为

这是一块json,

"im:image":[  
           {  
              "label":"http://is1.mzstatic.com/image/thumb/Purple20/v4/87/35/82/87358231-ce91-3d14-b306-95888c23db3c/mzl.gdgtivnk.png/53x53bb-85.png",
              "attributes":{  
                 "height":"53"
              }
           },
           {  
              "label":"http://is5.mzstatic.com/image/thumb/Purple20/v4/87/35/82/87358231-ce91-3d14-b306-95888c23db3c/mzl.gdgtivnk.png/75x75bb-85.png",
              "attributes":{  
                 "height":"75"
              }
           },
           {  
              "label":"http://is3.mzstatic.com/image/thumb/Purple20/v4/87/35/82/87358231-ce91-3d14-b306-95888c23db3c/mzl.gdgtivnk.png/100x100bb-85.png",
              "attributes":{  
                 "height":"100"
              }
           }

我像这张照片一样解析它 enter image description here

但是当我点击这里的任何图片时,我得到了这张图片!

enter image description here

我怎么能解决这个问题,当我点击“Snapchat”时,我得到的图像是为了拍摄?

这是解析json的方法

 public void JsonAppShowData() {
    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( jsonUrl, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {  try {
            JSONArray jsonArray = response.getJSONObject(feedKey).getJSONArray(entryKey);
            AppShowModule appShowModule = new AppShowModule();
            int x = appShowModule.getId();
            for (int i = 0; i<jsonArray.length();i++)
            {
                JSONArray imageArray = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey);
                for (int j = 0; j < imageArray.length(); j++) {
                    String image = imageArray.getJSONObject(j).getString(labelKey).toString();
                    imageUrls.add(image);
                    appShowModule.setAllimage(imageUrls);
                    appShowModules.add(appShowModule);
                }}
                imageRecyclerViewadapter = new ImageListAdapter(appShowModules, getContext(), imageUrls);
                AppRecyclerView.setAdapter(imageRecyclerViewadapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }}
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e( "LOG", error.toString() );
        }
    } );

这是图像列表加载器

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
    List<AppShowModule> appShowModules;
    List<String> imageUrl;
    AppShowModule appShowModule;
    String x;
    Context context;
    public ImageListAdapter(List<AppShowModule> appShowModules, Context context ,List<String>imageUrls
    ){
        super();
        this.imageUrl =imageUrls;
        this.appShowModules = appShowModules;
        this.context = context;}
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.imagelayout, parent,false );
        ViewHolder viewHolder = new ViewHolder( v );
        return viewHolder;}
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AppShowModule appShowModule = appShowModules.get(position);
         x = appShowModule.getAllimage().get(position);
        Picasso.with(context).load(x).into(holder.appImage);
    }
    public int getItemCount() {
        return imageUrl.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView appImage;
        public ViewHolder(View itemView) {
            super(itemView);
            appImage = (ImageView) itemView.findViewById(R.id.appImage);
            appImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent= new Intent(context, ImageShow.class);
                    intent.putExtra("image", x);
                    context.startActivity(intent);
                }});
        }

        }
}

这是点击时显示的活动

appImage=(ImageView)findViewById(R.id.appImage);
    Picasso.with(context).load(x).into(appImage);

2 个答案:

答案 0 :(得分:0)

你不能直接在ViewHolder中使用变量'x',因为它不会点击所点击项目的图像链接。

要获得正确的链接,您必须使用 getAdapterPosition()。如果您的点击响应取决于单击项目的位置,您只需从ViewHolder中调用getAdapterPosition()即可。 Beow我已经展示了如何在ViewHolder中使用它。

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnItemClickListener {
public MyViewHolder(View view) {
  super(view);

  view.setOnClickListener(this);
}

@Override
public void OnClick(View view) {
  // Get the item clicked
  // For this example, I'm assuming your data source is of type `List<MyObject>`
  MyObject myObject = mDataSource.get(getAdapterPosition());
  // Then you can do any actions on it, for example - 
  myObject.setChecked();
   }
}

答案 1 :(得分:0)

您可以在适配器中为单击的项目

定义回调
public interface OnItemClickListener {
    void onItemClicked(YourItem item);
}

在适配器中为调用片段或活动公开一个方法来设置监听器

 public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
}

在绑定yourviewholder时,您可以将事件报告给片段或活动&amp;执行业务逻辑

 @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { 
        //your binding code
        if (onItemClickListener != null) {
          onItemClickListener.onItemClicked(item);
        }
    }

在您的片段或活动中

adapter.setOnItemClickListener(new YourAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(YourItem item) {
                //Business logic goes here
            }
        });