Android GridView - update View based on position

后端 未结 3 1424
情话喂你
情话喂你 2021-01-03 07:01

I have a GridView, using a custom adapter (myAdapter extends BaseAdapter), where each item in the grid holds an ImageButton. getView() is working fine.

However, from

相关标签:
3条回答
  • 2021-01-03 07:32

    If u want to update the image for one particular item in the grid, what u can do is...have an onClickListener for all the images in the grid.

    Now when u will click on any of the image, get the position of the image and change ur background resource for that particular image.

    Example:

    While adding the image: imageView.setOnClickListener(imageListener);

    And the listener will be something like dis:

    private OnClickListener imageListener = new OnClickListener() {
            public void onClick(View v) {
                int id = v.getId();
                ImageView imgView = imageArray.get(id);
                imgView .setBackgroundResource(R.drawable.newImage);
        };
    

    I hope u find it helpful.

    0 讨论(0)
  • 2021-01-03 07:33

    That's also my last problem. Here my solution I use data Model and adapter for my RecyclerView Firstly, register your new data to your model

      DataModel detail = new DataModel(id, name, sat, image);
    

    after that, use set to replace old value with the new one

      mData.set(index, detail);
    

    finally, refresh your adapter

      if(adapter!=null)
      adapter.notifyItemChanged(index);
    
    0 讨论(0)
  • 2021-01-03 07:46

    In case of Images in a GridView I did this and it works also:

    gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View imgView, int position, long id) {
        ImageView imageView = (ImageView) imgView;
        imageView.setImageResource(R.drawable.newImage);
            }
        });    }
    
    0 讨论(0)
提交回复
热议问题