karthik kolanji
karthik kolanji

Reputation: 2104

How to show default images when no item in Gridview?

I am using custom Gridview which looks like Shelf. I am showing 3 columns When there is 5 items from the server , the 6th position of the Gridview is empty

I wanted to show the rack line image in it

Grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<com.andexert.library.RippleView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ripple_grid"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:background="@drawable/im_ninja_delights_rack"
    android:paddingBottom="20dp"
    app:rv_centered="true"
    app:rv_framerate="5"
    app:rv_rippleDuration="50">
    <ImageView
        android:id="@+id/image_ninja_delights"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:scaleType="fitXY"
        android:src="@drawable/chocolates_image"
        android:layout_marginTop="15dp"
        android:visibility="visible"></ImageView>
</com.andexert.library.RippleView>

enter image description here

im_ninja_delights_rack.png

enter image description here

Upvotes: 0

Views: 893

Answers (4)

Sunil
Sunil

Reputation: 3793

i was working on same condition use this

Adapter class

1.

public int getCount()
{

    return mItems.size()+1;
}

2.

  public View getView(int position, View convertView, ViewGroup parent)
        {

            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View gridView = inflater.inflate(R.layout.test_grid_layout, null);
            ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
            if(position == mItems.size())
            {
                imageView.setImageResource(R.drawable.blank_img);
            }
            else
                {
                imageView.setImageURI(Uri.parse(mItems.get(position)));
            }
            return gridView;
        }

enter image description here

last one is blank images

Upvotes: 1

linhtruong
linhtruong

Reputation: 441

1

Did you mean that?

My solution here is adding some fake items to fill out the line with rack and disable onClick for them.

Upvotes: -2

Omkar Marne
Omkar Marne

Reputation: 45

If you are trying to show 3 images per row, Make sure that your image list is multiple of 3.

then, set a default image to the imageviews that you are using in inflated xml.

do something similar to this,

in your activity, call adapter constructor as,

    gridView.setAdapter(new BaseAdapterClass(context, actual_data_list, actual_data_list.size() + number_to_make_size_multiple_of_3));

then, in your Adapter class,

    public int getCount(){
        return multiple_of_3_passed_from_activity;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if(position < actual_data_list.size()) {
            // Set your image to imageview
        }

        return convertView;
    }

Upvotes: 0

user5068137
user5068137

Reputation:

Use piccaso library to set placeholder for your imageView

like this

Picasso.with(this)
.load("IMAGE URL")
.placeholder(R.drawable.default_image) // optional
.error(R.drawable.error_image)         // optional
.into(imageView);

Upvotes: 2

Related Questions