在项目上单击“回收器视图的监听器”以显示图像

时间:2017-01-18 05:58:04

标签: android android-imageview

我使用RecyclerView在列表中从我的服务器获取图像。当我点击RecyclerView中的某个项目时,我希望该项目中的图片能够打开一个新活动。我怎样才能得到那张照片?在此先感谢。

RecyclerViewAdapter.java

public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context) {

        super();
        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);

        ViewHolder viewHolder = new ViewHolder(v,context,getDataAdapter);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder Viewholder, int position) {

        GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);

        imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

        imageLoader1.get(getDataAdapter1.getImageServerUrl(),
                ImageLoader.getImageListener(
                        Viewholder.networkImageView,//Server Image
                        R.mipmap.ic_launcher,//Before loading server image the default showing image.
                        android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
                )
        );


      Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
        Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());

    }

    @Override
    public int getItemCount() {

        return getDataAdapter.size();
    }

  //  public static
    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

      public TextView ImageTitleNameView;
     public NetworkImageView networkImageView;

    List<GetDataAdapter> getDataAdapter ;//= new List<GetDataAdapter>();
        Context context;

        public ViewHolder(View itemView,Context context,List<GetDataAdapter> getDataAdapter) {

            super(itemView);
            this.getDataAdapter=getDataAdapter;
            this.context=context;
            itemView.setOnClickListener(this);

            ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);

           // networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1);
            networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1);

        }
 @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            GetDataAdapter getDataAdapters=this.getDataAdapter.get(position);
            Intent intent=new Intent(this.context,SecondPage.class);
            intent.putExtra("image_title",getDataAdapters.getImageTitleName());
            intent.putExtra("image_url",getDataAdapters.getImageServerUrl());
            this.context.startActivity(intent);

        }
    }
}

SecondPage.java

 imageView=(ImageView)findViewById(R.id.imge);

    Intent i = getIntent();

      int resid =i.getIntExtra("image_url", 0);

     imageView.setImageResource(Integer.parseInt("image_url"+getIntent().getStringExtra("image_url")));

    imageView.setImageResource(resid);

1 个答案:

答案 0 :(得分:1)

如果您的图像存储在ArrayList中,请通过intent传递RecyclerView元素的位置,并将src中的ImageView SecondPage.java设置为:

<强> OnClick

myIntent.putExtra("imageIndex", position);

<强> SecondPage.java

imageView=(ImageView)findViewById(R.id.imge);

Intent i = getIntent();

int imgIndex = i.getIntExtra("imageIndex", 0);

imageView.setImageResource(ArrayList.get(imgIndex));

如果您通过意图使用Picasso传递ImageUrl以在ImageView中显示图像:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

确保将Picasso库添加到项目gradle中:

compile 'com.squareup.picasso:picasso:2.5.2'