0

I want to create a grid view with add button at the end of the grid after displaying the list of images as show in the pic below. I am able to fetch the list of images from network and add to the grid. However, unable to add the button at the end.

Image for the add button is available in the R.drawable folder. Appreciate any suggestion to achieve this.

Here is my adapter code.

public class PhotosGridAdapter extends ArrayAdapter<PhotoAlbum.Edge> {


    public PhotosGridAdapter(Context context, List<PhotoAlbum.Edge> photos) {
        super(context, 0, photos);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        PhotoAlbum.Edge  photoItem = getItem(position);

        if(convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item_photo, parent, false);
        }
        ImageView ivPhoto = (ImageView) convertView.findViewById(R.id.grid_image);
        Picasso.with(getContext()).load(photoItem.node.mediaItem.url).into(ivPhoto);
        return convertView;
    }
}

image

1
  • add an item at the end of array and then set + button for last position item Commented Sep 28, 2017 at 6:00

1 Answer 1

1

In your get count method return photos.size() + 1;

public int getCount() {
    return photos.size() + 1;        
}

and in your getView() method

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item_photo, parent, false);
        }
        ImageView ivPhoto = (ImageView) convertView.findViewById(R.id.grid_image);

        if(position == photos.size()){
            ivPhoto.setImageResource(R.drawable.ic_your_plus_icon_here);
            return convertView;
        }
        PhotoAlbum.Edge  photoItem = getItem(position);


        Picasso.with(getContext()).load(photoItem.node.mediaItem.url).into(ivPhoto);
        return convertView;
    }

Hope this helps..!!!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot mudit_sen, that helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.