Show blank view in Android gallery

People -

I am trying to implement a Gallery widget that displays an ArrayList of images, and I started with Hello, an example gallery on the dev site. This part works great.

I need the gallery to display an empty view (a special view when the ArrayList has no content), but I cannot get Gallery to do this. I have done this with ListView and other adapters in the past, but I cannot get it to work with Gallery. What do I need to override / implement in an adapter, gallery, or both to display an empty view? This is my adapter code:

public class ImageAdapter extends BaseAdapter {

     int mGalleryItemBackground;
     private Context mContext;
     private ArrayList<Drawable> images;

     public ImageAdapter(Context c) {
      mContext = c;
      TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
      mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
      a.recycle();

      images = new ArrayList<Drawable>();
     }

     public void addImage(Drawable d) {
      images.add(d);
     }

     public boolean isEmpty() {
      return getCount() == 0;
     }

     public int getCount() {
      return images.size();
     }

     public Drawable getItem(int position) {
      return images.get(position);
     }

     public long getItemId(int position) {
      return position;
     }

     public View getView(int position, View contentView, ViewGroup parent) {
      ImageView i = new ImageView(mContext);
      i.setImageDrawable(images.get(position));

      i.setLayoutParams(new Gallery.LayoutParams(160, 120));
      i.setScaleType(ImageView.ScaleType.FIT_XY);
      i.setBackgroundResource(mGalleryItemBackground);

      return i;
     }
}

ArrayList, getCount() ( 0), Gallery isEmpty, getEmptyView() , , BaseAdapter, ?

!

+3
1

:

setEmtpyView AdapterView

, ( Gallery/AdapterView, ) AdapterView ( View. GONE View.VISIBLE). , , , .

( TextView) setEmptyView(), . TextView LinearLayout, Activity, , AdapterView View.VISIBLE.

+1

Source: https://habr.com/ru/post/1758063/


All Articles