开发者

android: remove a view from a gallery

开发者 https://www.devze.com 2023-02-28 07:55 出处:网络
I\'m using a Gallery view in my app. The app is designed so that I can drag and drop a view from that Gallery.

I'm using a Gallery view in my app. The app is designed so that I can drag and drop a view from that Gallery.

How can I remove the dragged v开发者_StackOverflowiew from the Gallery?


You remove it from the underlying adapter. If you do this correctly, the Gallery will refresh itself. Otherwise, call notifyDataSetChanged() on the adapter to trigger a Gallery update.


If you override ImageAdapter you can modify the contents at will by adding methods to delete or add items to your image list(s) or in the case of the example, completely swap lists on the fly. I am displaying a app banner at startup, and then changing the Gallery to display the mode the app is in as a slider. Whenever you call a method that modifies the dataset in the ImageAdapter, call imageAdapter.notifyDataSetChanged() as CommonsWare says above :

    // in onCreate
    _gallery = (Gallery) this.findViewById(R.id.gallery_header);
    _imageAdapter = new ImageAdapter(getApplicationContext(),screen_width,screen_height);
    _imageAdapter.setBannerMode(true);        
    _gallery.setAdapter(_imageAdapter);


    // the main activity, in my case in a message handler.

    _imageAdapter.setBannerMode(false);
    _imageAdapter.notifyDataSetChanged();
    _gallery.setSelection(0,true);

    // this is my extended image adapter class

   import android.content.Context;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.BaseAdapter;
   import android.widget.Gallery;
   import android.widget.ImageView;
   import android.widget.ImageView.ScaleType;

public class ImageAdapter extends BaseAdapter
{
    private Context _context = null;
    private int[] imageIds = { R.drawable.add_banner,R.drawable.subtract_banner,R.drawable.multiply_banner,R.drawable.divide_banner };
    private int[] bannerIds = { R.drawable.mathpiggie_banner };
    private static boolean bannerEnabled = true;

    int _screen_width;
    int _screen_height;

    public ImageAdapter(Context context, int screen_width, int screen_height) {
        this._context = context; 
        _screen_width = screen_width;
        _screen_height = screen_height; 
    }

    public void setBannerMode(boolean val)
    {
        bannerEnabled = val;
    }

    @Override
    public int getCount()
        {
            if (bannerEnabled)
                return bannerIds.length;
            else
                return imageIds.length;
        }

    @Override
    public Object getItem(int index)
        {
            if (bannerEnabled)
                    return bannerIds[index];
            else
                return imageIds[index];
        }

    @Override
    public long getItemId(int index)
        {
            return index;
        }

    @Override
    public View getView(int postion, View view, ViewGroup group)
        {
            ImageView imageView = new ImageView(_context);
            if (bannerEnabled)
                imageView.setImageResource(bannerIds[postion]);
            else
                imageView.setImageResource(imageIds[postion]);

             return imageView;
        }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消