如何从recyclerview位置获取图像点击数据并将其设置为另一个imageview?

时间:2018-01-08 10:20:23

标签: android

我有一个应用程序,其中包含recyclerviewimageview以及文字。当我点击基于imageview位置的项目时,我想要该图像视图的图像并将其设置为另一个Imageview。要做到这一点,我已经interface并在Activity中实现了它的方法,但需要知道我该怎么做

适配器代码:

  public interface OnItemClicked {
    void onItemClicked(int position);
}

public AvatarListAdapter(Context context, int[] arrayList) {
    this.mContext = context;
    this.dataModel = arrayList;
}

@Override
public AvatarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_items, parent, false);
    return new AvatarListAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(AvatarListAdapter.ViewHolder holder, final int position) {
    holder.mAvatarImage.setImageResource(dataModel[position]);
    holder.mAvatarImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onClick.onItemClicked(position);
        }
    });
}

public void setOnClick(OnItemClicked onClick) {
    this.onClick = onClick;
}

@Override
public int getItemCount() {
    return dataModel.length;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    private CircleImageView mAvatarImage;

    public ViewHolder(View itemView) {
        super(itemView);
        mAvatarImage = itemView.findViewById(R.id.img_avatar);
    }
}
}

活动代码:

  private void initializeActions() {
    int numberOfColumns = 3;
    mAvatarImages.setLayoutManager(new GridLayoutManager(AvatarProfileImage.this, numberOfColumns));
    mAdapter = new AvatarListAdapter(AvatarProfileImage.this, avatar);
    mAvatarImages.setAdapter(mAdapter);
    mAdapter.setOnClick(this);
}

@Override
public void onItemClicked(int position) {
    String pos = "You clicked at position " + position;
    Toast.makeText(getApplicationContext(), "You clicked position" + pos, Toast.LENGTH_LONG).show();
    mAvatarImages.setBackgroundResource(avatar[position]);
}

3 个答案:

答案 0 :(得分:1)

您可以按OnClick()的位置获取数据。当您使用本地drawable图像资源时,一种方法是修改您的viewHolder,如下所示。这只是一种方法,您也可以使用{{1 }}

ViewTag

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ private Circle mAvatarImage; public ViewHolder(View itemView) { super(itemView); mAvatarImage = itemView.findViewById(R.id.img_avatar); mAvatarImage.setOnClickListener(this); } @Override public void onClick(View v) { int postion =getAdapterPosition(); int image =arrayList[postion]; // Use this image to forward } } 移除OnClick

答案 1 :(得分:1)

实施RecycleView onClick()方法并在onclick position.have看

时获取指定数据
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private Circle mAvatarImage;

    public ViewHolder(View itemView) {
        super(itemView);
        mAvatarImage = itemView.findViewById(R.id.img_avatar);
        mAvatarImage.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        int position=arrayList[getAdapterPosition()];
        String imagepath = arraylist.get(position).getImagePath       
        //you can use imagepath anywhere you want either be put in bundle or intent call

    }
}

答案 2 :(得分:0)

适配器代码应如下所示

    private final OnItemClickListener listenerOnItemClick;

    public interface OnItemClickListener {
        void onItemClick(int position); }

    public AvatarListAdapter(Context context, int[] arrayList, OnItemClickListener listenerOnItemClick) {
        this.mContext = context;
        this.dataModel = arrayList;     
        this.listenerOnItemClick=listenerOnItemClick;
    }

    @Override 
    public AvatarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_items, parent, false);
        return new AvatarListAdapter.ViewHolder(view); }

    @Override 
    public void onBindViewHolder(AvatarListAdapter.ViewHolder holder, final int position) {
        holder.mAvatarImage.setImageResource(dataModel[position]);
        holder.mAvatarImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.onItemClicked(position);
            }
        }); }

        public void setOnClick(OnItemClicked onClick) {
        this.onClick = onClick; }

        @Override
        public int getItemCount() {
        return dataModel.length; }

        public class ViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView mAvatarImage;

        public ViewHolder(View itemView) {
            super(itemView);
            mAvatarImage = itemView.findViewById(R.id.img_avatar);
        } }

活动代码

       AvatarListAdapter.OnItemClickListener onItemClickListener = new AvatarListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //change your Image here
            String pos = "You clicked at position " + position;
            Toast.makeText(getApplicationContext(), "You clicked position" + pos, Toast.LENGTH_LONG).show();
            mAvatarImages.setBackgroundResource(avatar[position]);
            }
        };




     private void initializeActions() {
        int numberOfColumns = 3;
        mAvatarImages.setLayoutManager(new GridLayoutManager(AvatarProfileImage.this, numberOfColumns));
        mAdapter = new AvatarListAdapter(AvatarProfileImage.this, avatar,onItemClickListener );
        mAvatarImages.setAdapter(mAdapter);
        mAdapter.setOnClick(this);
    }