以编程方式从图库添加图像到网格项

时间:2012-05-29 06:21:52

标签: android gridview gallery

我有一个2x5的网格。网格中的每个元素都包含一个图像。当我长按图像时,我应该被带到画廊,在那里我可以选择一张图片并将其分配给该网格项目。怎么做?

1 个答案:

答案 0 :(得分:0)

我认为您可以使用网格视图并将其与View寻呼机结合使用。以下是如何执行此操作的示例。

    public class GridGallery extends BaseAdapter {

    Context context;

    public GridGallery(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return numberOfImages;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        final ImageView view;

        if(convertView == null) {

            view = new ImageView(context);

            view.setScaleType(ImageView.ScaleType.FIT_CENTER);
            view.setLayoutParams(new GridView.LayoutParams(screenWidth/4, screenHeight/4));
            view.setAdjustViewBounds(false);
            view.setPadding(2, 2, 2, 2);

            view.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    // You can set your View Pager adapter here
                    //
                    return false;
                }
            });

        if(view!=null) {
            // Here you can download your images from internet. 
            thumbnailLoader.DisplayImage(urlList.get(position), view);
            notifyDataSetChanged();  //Calling this helped to solve the problem.

            view.setTag(position);
        }

        return view;
    }       
}