android:从图库中删除视图

时间:2011-04-25 13:51:56

标签: android gallery

我在我的应用中使用了图库视图。该应用程序的设计使我可以从该图库中拖放视图。

如何从图库中删除拖动的视图?

2 个答案:

答案 0 :(得分:6)

您从底层适配器中删除它。如果您正确执行此操作,Gallery将自行刷新。否则,请在适配器上调用notifyDataSetChanged()以触发Gallery更新。

答案 1 :(得分:0)

如果覆盖ImageAdapter,您可以通过添加删除或添加项目列表的方法来随意修改内容,或者在示例的情况下,可以动态完全交换列表。我在启动时显示应用横幅,然后更改图库以显示应用所处的模式作为滑块。每当您调用一个修改ImageAdapter中数据集的方法时,请调用imageAdapter.notifyDataSetChanged(),如上面CommonsWare所述:

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