简体   繁体   English

如何在网格 recyclerview 中创建加号图标以添加图像?

[英]How to create plus icon in grid recyclerview for adding images?

在此处输入图片说明

How do I create something like this using gridlayoutManager In a reyclerview ?如何在reyclerview使用gridlayoutManager创建这样的东西? How to do this in an efficient way, so that whenever I click on the plus image I can fetch a new image from my device so that the image shows up in the grid at bottom or in a new column?如何以有效的方式执行此操作,以便每当我单击加号图像时,我都可以从我的设备中获取新图像,以便图像显示在底部的网格或新列中?

You can create two layouts : one with the + icon and other where image will be displayed for your grid adapter.您可以创建两种布局:一种带有 + 图标,另一种是为您的网格适配器显示图像。 Then you can check for the position and return the layout you want.然后您可以检查位置并返回您想要的布局。

      private static final int VIEW_TYPE_ONE = 1;
      private static final int VIEW_TYPE_TWO = 2;

      // Determines the appropriate ViewType according to the position
@Override
public int getItemViewType(int position) {

    if (position == 0 ) {

        return VIEW_TYPE_ONE;
    } else {

        return VIEW_TYPE_TWO;
    }
}

Then in your onCreateviewHolder:然后在您的 onCreateviewHolder 中:

     // Inflates the appropriate layout according to the ViewType.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;

    if (viewType == VIEW_TYPE_ONE) {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.your_plus_icon_layout, parent, false);
        return new PlusIconViewHolder(view);
    } else if (viewType == VIEW_TYPE_MESSAGE_TWO) {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.your_normal_image_layout, parent, false);
        return new NormalViewHolder(view);
    }

    return null;
}

In your onBindViewHolder,在您的 onBindViewHolder 中,

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case VIEW_TYPE_ONE:
             holder.bind();
            break;
        case VIEW_TYPE_MESSAGE_TWO:
             holder.bind(imageList.get(position -1));
    }
}

Note that i have send position - 1 item from the list.Its because images will be shown from 1 index because index 0 is occupied by your + icon.请注意,我已发送位置 - 列表中的 1 个项目。这是因为图像将从 1 个索引显示,因为索引 0 被您的 + 图标占用。 And yes you have to create two veiwholders too for their respective functionalities.是的,您也必须为各自的功能创建两个 veiwholder。

You need to create an adapter that supports two types of items - photo one and '+' one.您需要创建一个支持两种类型项目的适配器 - 照片一种和“+”一种。 Then add special '+' item at first position in the list that you provide to the adapter.然后在您提供给适配器的列表中的第一个位置添加特殊的“+”项。

Check this link: “ A RecyclerView with multiple item types ” by Vitaly Vivchar.检查此链接:Vitaly Vivchar 的“具有多种项目类型的 RecyclerView ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM