android: remove a view from a gallery

爷,独闯天下 提交于 2019-12-21 20:35:23

问题


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 view from the Gallery?


回答1:


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.




回答2:


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;
        }
}


来源:https://stackoverflow.com/questions/5779023/android-remove-a-view-from-a-gallery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!