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

时间:2015-12-24 01:58:49

标签: android android-recyclerview recycler-adapter fitbit

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

我还需要第一行,即那个说#34;本周"即使页面向下滚动也要粘在上面。

5 个答案:

答案 0 :(得分:6)

您应该使用getItemViewType(int)使用不同视图类型的概念。然后在onCreateViewHolder(ViewGroup, int)上,您可以检查应该膨胀/创建的类型。

示例:

@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
     }
}

答案 1 :(得分:2)

使用StickyHeaderRecyclerView library

使用起来非常简单

答案 2 :(得分:0)

您可以在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


      }

    }

对于您的粘性&#34;此视图弱&#34;,您可以将其添加到RecyclerView的顶部,然后通过滚动回收器视图的事件处理它

答案 3 :(得分:0)

有两种方法可以实现此类RecyclerView

  1. 在每个布局中添加标题,并根据您的要求隐藏/显示(最好)。
  2. 或者为标题和内容使用两种不同的布局(不可取,因为它可能会导致适配器中项目总数出现问题)。
  3. 在自定义POJO / GetterSetter类中,为headerStatus添加一个字段(boolean或int),以标识是否显示标题。

    现在在适配器覆盖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,您需要查看标题并在其中设置适当的数据。 对于案例2,您需要扩充该标题布局并在其中添加适当的数据。

答案 4 :(得分:0)

如果你想以“正确”的方式进行,没有黑客,你应该编写自己的LayoutManager,并手动处理这些情况。它并不像听起来那么难,但需要付出一些努力。

相关问题