替换单个图像时,所有其他ImageView都将在Gridview中被替换

时间:2017-01-31 14:55:27

标签: java android gridview android-recyclerview adapter

gridview有3个单元格,每个单元格都有ImageView和ImageButton来添加图库或相机中的图像。

但是当我试图在gridview的点击单元格中设置所选图像时,它会替换所有单元格,而不是单击一个单元格。

这是gridview的适配器

public class Adapter_For_GridView extends RecyclerView.Adapter<Adapter_For_GridView.ViewHolder>{

    public static final String TAG="===GridView Pics===";
    ViewClickCallback viewClickCallback;
    Context context;
    List<UserGrid> userGridList;
    UserGrid userGridWrapper;
    String ImageData;

    public Adapter_For_GridView(ViewClickCallback viewClickCallback,Context context) {
        this.viewClickCallback=viewClickCallback;
        this.context=context;
    }

    @Override
    public Adapter_For_GridView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.user_profile_photo_single_item,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        Log.d(TAG,"On Create View Holder ");
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(Adapter_For_GridView.ViewHolder holder, int position) {
        userGridWrapper=userGridList.get(position);

        Log.d(TAG,"On Bind View Holder- Value of isUpload "+userGridWrapper.isUpload());
        Log.d(TAG,"On Bind View Holder Image Data "+ImageData);
        Log.d(TAG,"On Bind View Holder Position "+position+" "+"User Grid Wrapper "+userGridWrapper);
        Log.d(TAG,"On Bind View Holder User Grid Wrapper Get Image Path "+userGridWrapper.getImagePath());

        if (ImageData!=null){
            Log.d(TAG,"Image Data is Not Null  "+ImageData);
            userGridWrapper.setUpload(true);
            if (userGridWrapper.isUpload()){
                holder.imageButton.setImageResource(R.drawable.ic_clear_black_24dp);
                Picasso.with(context).load(ImageData)
                        .into(holder.imageView);
            }
        }
        else{
            holder.imageButton.setImageResource(R.drawable.ic_add_black_24dp);
        }


    }

    @Override
    public int getItemCount() {
        Log.d(TAG,"Value at GetItemCount of GridList "+userGridList.size());
        if (userGridList!=null){
           return userGridList.size();
        }
        return 2;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView imageView;
        ImageButton imageButton;


        public ViewHolder(View itemView) {
            super(itemView);
            Log.d(TAG,"On View Holder");
            itemView.setOnClickListener(this);
            imageView= (ImageView) itemView.findViewById(R.id.User_Pics);
            imageButton= (ImageButton) itemView.findViewById(R.id.imageButton);
            imageButton.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            Log.d(TAG,"Image Button Clicked "+getAdapterPosition()+" Get Layout Position  "+getLayoutPosition());
            if (viewClickCallback!=null){
                Log.d(TAG,"ViewClickcalleback is not null ");
               viewClickCallback.viewClicked(getAdapterPosition()+1);
            }

        }
    }

    public void setUserGridList(List<UserGrid> userList ){
        this.userGridList=userList;
    }

    public void setImagePath(String path,int position){ //GETTING IMAGE HERE FROM ONACTIVITYRESULT TO THIS ADAPTER
        this.ImageData=path;
        Log.d(TAG,"Position of Grid "+position); //POSITION OF CLICKED ITEM
        Log.d(TAG,"Set Image Path "+path);    //IMAGE PATH OF SELECTED IMAGE
        notifyDataSetChanged();
        }
    }

来自onactivityresult的LogCat

01-31 18:13:47.917 9146-9146/com.example.com.pro_working1 D/===User Profile===: User Profile On Activity Result Calling 25 -1
01-31 18:13:47.927 9146-9146/com.example.com.pro_working1 D/===User Profile===: On Activity Result Calling--Request Code 25
01-31 18:13:47.927 9146-9146/com.example.com.pro_working1 D/===User Profile===: Intent Data Intent { dat=content://media/external/images/media/19276 typ=image/jpeg flg=0x1 }
01-31 18:13:47.927 9146-9146/com.example.com.pro_working1 D/===User Profile===: On Activity Result Position 0
01-31 18:13:48.687 9146-9146/com.example.com.pro_working1 D/===User Profile===: Image Name content://media/external/images/media/19276
01-31 18:13:48.687 9146-9146/com.example.com.pro_working1 D/===User Profile===: Image /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg
01-31 18:13:48.687 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Position of Grid 0
01-31 18:13:48.687 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Set Image Path /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg
01-31 18:13:48.777 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3
01-31 18:13:48.777 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3
01-31 18:13:48.777 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder- Value of isUpload false
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Image Data /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Position 0 User Grid Wrapper com.example.com.pro_working1.User_Gallery.UserGrid@41c239c8
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder User Grid Wrapper Get Image Path null
01-31 18:13:48.787 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Image Data is Not Null  /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder- Value of isUpload false
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Image Data /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Position 1 User Grid Wrapper com.example.com.pro_working1.User_Gallery.UserGrid@41c23e88
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder User Grid Wrapper Get Image Path null
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Image Data is Not Null  /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Value at GetItemCount of GridList 3
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder- Value of isUpload false
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Image Data /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder Position 2 User Grid Wrapper com.example.com.pro_working1.User_Gallery.UserGrid@41c241a8
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: On Bind View Holder User Grid Wrapper Get Image Path null
01-31 18:13:49.557 9146-9146/com.example.com.pro_working1 D/===GridView Pics===: Image Data is Not Null  /storage/sdcard0/pictures/Social/1/Spread-Unconditional-Love_www.example.com-101.jpg

2 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您使用单个varibale ImageData 来存储所选图像的路径。因此,当在gridview中重新创建视图时,此varibale值将相同,这将使下面的代码片段中的第一个条件为真。

if (ImageData!=null){
    Log.d(TAG,"Image Data is Not Null  "+ImageData);
    userGridWrapper.setUpload(true);
    if (userGridWrapper.isUpload()){
        holder.imageButton.setImageResource(R.drawable.ic_clear_black_24dp);
        Picasso.with(context).load(ImageData)
                .into(holder.imageView);
    }
}
else{
    holder.imageButton.setImageResource(R.drawable.ic_add_black_24dp);
}

现在它已进入第一个if条件,您在检查其值之前将上传值设置为true。这将再次重新开始真实。因此,它使用相同的数据填充所有视图。

<强>解决方案

将以下方法更改为:

public void setImagePath(String path,int position){ //GETTING IMAGE HERE FROM ONACTIVITYRESULT TO THIS ADAPTER
this.ImageData=path;
userGridList.get(position).setUpload(true);
Log.d(TAG,"Position of Grid "+position); //POSITION OF CLICKED ITEM
Log.d(TAG,"Set Image Path "+path);    //IMAGE PATH OF SELECTED IMAGE
notifyDataSetChanged();
}

同时更改填充图像的条件检查:

if (userGridWrapper.isUpload()){
    holder.imageButton.setImageResource(R.drawable.ic_clear_black_24dp);
    Picasso.with(context).load(ImageData)
            .into(holder.imageView);

}
else{
holder.imageButton.setImageResource(R.drawable.ic_add_black_24dp);
}

答案 1 :(得分:0)

问题是你只有一个路径作为一个字段。您使用this.ImageData=path;分配新值。致电notifyDataSetChanged();后,您的适配器将刷新其数据,并为每个可见项目调用onBindViewHolder()。然后它将为每个调用加载ImageData路径的新图像。

您需要的是路径列表。您还应该检查是否已经加载了图像。如果它是相同的图像,您将不必再次加载它。也许你可以为此实现一个小缓存。