android:从图库中删除视图

发布于 2024-11-03 05:56:07 字数 68 浏览 1 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-11-10 05:56:07

您将其从底层适配器中删除。如果您正确执行此操作,图库将自行刷新。否则,请在适配器上调用 notifyDataSetChanged() 以触发 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.

枯寂 2024-11-10 05:56:07

如果您覆盖 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;
        }
}

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;
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文