如何在 android 中的 RecyclerView 项目之间添加动态视图?

How to add a dynamic view in between items of RecyclerView in android?

我需要在 RecyclerView 的项目之间添加一个小条带。此条带可以位于列表中不同数量的项目之后。这需要动态完成。 我需要实现类似 FitBit 所做的事情:

我还需要第一行,即说 "This Week" 的那一行,即使页面向下滚动也能保持在顶部。

您应该使用不同视图类型的概念 getItemViewType(int). Then on onCreateViewHolder(ViewGroup, int) 您可以检查您应该使用哪种类型 inflate/create。

示例:

@Override
public int getItemViewType(int position) {
    // you should return the view type, based on your own dynamic logic
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     switch (viewType) {
         // handle each view type accordingly
     }
}

您可以在 RecyclerView 中使用多个视图类型的概念,只需使用 getItemViewType(),并注意 onCreateViewHolder() 中的 viewType 参数。

例如,您可以使用以下模型:

    public class Data{
           int field1;
           float filed2;
           int rowType // 1,2,2,...N this will fill by you whenever you will    
                       //creating arraylist for your recyclerview
     }


    public class Custome Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    ArrayList<Data> mItems;

    class ViewHolderRowType1 extends RecyclerView.ViewHolder {
        ...
    }

    class ViewHolderRowType2 extends RecyclerView.ViewHolder {
        ...
    }

    ....
    class ViewHolderRowTypeN extends RecyclerView.ViewHolder {
        ...
    }

    @Override
    public int getItemViewType(int position) {
        return mItems.get(position).rowType; 
        //or
        //return positon%2; // This will based on your condition        
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolderRowType0(...);
             case 1: return new ViewHolderRowType1(...);
             ...
             case N: return new ViewHolderRowTypeN(...);
         }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder vh, int position) {

    //Just check which view type is going to bind and then fill the data accordingly in your rows


      if(vh instanceof ViewHolderRowType1){
       // Fill the data for first view type


      } else if (vh instanceof ViewHolderRowType2) {
       // Fill the data for second view type


      } else if (vh instanceof ViewHolderRowTypeN){
       // Fill the data for Nth view type


      }

    }

对于你的粘性"this view weak",你可以将它添加到你的RecyclerView顶部,然后通过RecyclerView

的滚动事件来处理它

有两种实现方式RecyclerView

  1. 在每个布局中添加 header 并根据您的要求添加 hide/show(最好)。
  2. 或者对 header 和内容使用两种不同的布局(不可取,因为它会导致适配器中的项目总数出现问题)。

在您的自定义 POJO / GetterSetter class 中,为 headerStatus 添加一个字段(boolean 或 int),以确定是否显示 header。

现在在适配器覆盖中 public int getItemViewType(int position)

static final int TYPE_ITEM = 0;
static final int TYPE_SEPARATOR = 1;
@Override
public int getItemViewType(int position) {
 if (mData.get(position).getHeaderStatus() == 0)
  return TYPE_ITEM;
 else
  return TYPE_SEPARATOR;
}

现在在 getView() 中展开布局时,您可以通过

检查行类型

int rowType = getItemViewType(position);

对于情况 1,您需要看到 header 并在其中设置适当的数据。 对于情况 2,您需要扩充 header 布局并在其中添加适当的数据。

使用StickyHeaderRecyclerView library

非常好用

如果你想以 "proper" 的方式做到这一点,没有 hack,你应该编写自己的 LayoutManager,并手动处理这些情况。这并不像听起来那么难,但需要一些努力。