在Android Studio中添加图片按钮的图库图片网格视图

时间:2016-02-03 08:50:41

标签: android gridview

我正在设计发布一些图片。我有以下设计。帮助我如何在gridview项目中添加按钮。将图像添加到gridview。

enter image description here

following is my design

1 个答案:

答案 0 :(得分:0)

我会像这样实现它。

GridItem接口:

public interface GridItem {

    public long getId();
    public String getImgUrl();
}

图像的ActualGridItem:

public class ActualGridItem implements GridItem{

private long id;
private String imgUrl;

public ActualGridItem() {
}

public ActualGridItem(long id, String imgUrl) {
    this.id = id;
    this.imgUrl = imgUrl;
}

public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}

public String getImgUrl() {
    return imgUrl;
}

public void setImgUrl(String imgUrl) {
    this.imgUrl = imgUrl;
}

}

GridAdapter:

public class GridAdapter extends BaseAdapter{

private List<GridItem> items;
private Context context;

public GridAdapter(Context context, GridItem addImageItem) {
    this.items = new ArrayList<GridItem>();
    this.context = context;
    this.items.add(addImageItem); // at this moment we only have + item 
}

public void addItem(GridItem actualItem) {
    int insertLocation = this.items.size() - 2; // Before last item, last always will be +
    this.items.add(insertLocation, actualItem);
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return this.items.size();
}

@Override
public GridItem getItem(int position) {
    return this.items.get(position);
}

@Override
public long getItemId(int position) {
    return this.items.get(position).getId();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = new ImageView(context);
        // Customize image view
    }

    GridItem currentItem = items.get(position);
    // load image with url currentItem.getImgUrl();
    return convertView;
}

}

在您的gridview为的活动或片段中:

GridAdapter adapter = new GridAdapter(this, new GridItem() {

        @Override
        public String getImgUrl() {
            // TODO Auto-generated method stub
            return "+ image url";
        }

        @Override
        public long getId() {
            // TODO Auto-generated method stub
            return -1;
        }
    });

    gridView.setAdpter(adapter);

稍后您可以使用

在适配器中添加新项目
gridView.addItem(actualGridItem);

在onItemClickListener中检查item id是否为-1,如果是,则单击+并执行任何您想要的操作。

我只是全面了解如何实施它。