如何在适配器中定义imageview?

时间:2017-03-31 07:48:00

标签: java android

我在Android中的查看器持有者的Recyclerview适配器中定义了一个imageview。我想以编程方式访问我的活动以设置图像。我怎么做?

1 个答案:

答案 0 :(得分:0)

假设您有一个适配器ImageRecyclerAdapter类:

public class ImageRecyclerAdapter extends RecyclerView.Adapter<ViewHolder>{

     public interface ImageViewInterface {

           void onImageViewLoaded(ImageView imageView);
         } 

final ImageViewInterface imageViewInterface;

public ImageRecyclerAdapter(ImageViewInterface imageViewInterface) {

    this.imageViewInterface = imageViewInterface;

}

//in the onBindViewHolder method

public void onBindViewHolder(final ViewHolder holder, int position) {

   imageViewInterface.onImageViewLoaded(holder.imageView);

  }
}

然后在您的活动中,您可以像

一样实例化并使用它
ImageRecyclerAdapter imageRecyclerAdapter = new ImageRecyclerAdapter(new ImageRecyclerAdapter.ImageViewInterface() {

        @Override
        public void onImageViewLoaded(ImageView imageView) {
            //do what ever with your image view
        }
    });