简体   繁体   English

如何在recyclerview内的特定位置添加分隔符?

[英]How to add separator at particular position inside recyclerview?

I have a list of taskLists. 我有一个任务列表列表。 To show the list I have used recyclerview. 为了显示列表,我使用了recyclerview。 I have 1st 3 items as today , tomorrow and later in my list. 我今天,明天和后面的列表中有前3项。 I want to add one separator after 1st 3 items in recycler view. 我想在回收站视图中的前3项之后添加一个分隔符。 How can I do this? 我怎样才能做到这一点?

Adapter : 适配器:

    public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ItemViewHolder>{

    ArrayList<ListData> item;
    public static final int TYPE1=1;
    Context conext;


  public   ListAdapter(Context context, ArrayList<ListData> item) {
        this.conext=context;
        this.item=item;

    }
    public interface OnItemClickListener {
        void onItemClick(ListData listData);
    }
    @Override
    public int getItemCount() {
        return item.size();

    }
    public void remove(int position) {
        item.remove(position);
        notifyItemRemoved(position);
    }

   // @Override
   // public int getItemViewType(int position) {
       // return item.get(position).getExpenseType();// Assume that this return 1 0r 2
  //  }

    @Override
    public void onBindViewHolder(ItemViewHolder itemViewHolder,final int i) {

        itemViewHolder.listName.setText(item.get(i).getTitle());

    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup,int viewType) {

        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.list_layout, viewGroup, false);
        return new ItemViewHolder(itemView,viewType);

    }


    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView listName;


        ItemViewHolder(View itemView, int viewType) {
            super(itemView);

            listName  = (TextView)itemView.findViewById(R.id.listData);

        }
    }
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

}

Can anyone help with this how can I put separator after 3 items in list? 任何人都可以帮助这个如何在列表中的3个项目之后放置分隔符? Thank you.. 谢谢..

You should define 2 types of RecyclerView rows: 您应该定义两种类型的RecyclerView行:

...YourRecyclerAdapter extends RecyclerView.Adapter<BaseViewHolder>

public static final int COMMON = 1;
public static final int SEPARATOR = 2;

Override getItemViewType method of your Adapter : 覆盖Adapter getItemViewType方法:

@Override
public int getItemViewType(int position) {
    if (position%10 == 0) //each 10 row is separator (change it!)
        return SEPARATOR;
    else return COMMON;
}

Change onCreateViewHolder method of your Adapter : 更改Adapter onCreateViewHolder方法:

@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == COMMON)
        return new ItemViewHolder(LayoutInflater.from(activity).inflate(R.layout.list_layout, parent, false));
    else
        return new SeparatorHolder(LayoutInflater.from(activity).inflate(R.layout.separator_item, parent, false));
}

@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
     if (getItemViewType(position) == COMMON) {
          //do stuff
     } else {
     }
}

ItemViewHolder extends BaseViewHolder ItemViewHolder扩展了BaseViewHolder
SeparatorHolder extends BaseViewHolder SeparatorHolder扩展了BaseViewHolder
BaseViewHolder extends RecyclerView.ViewHolder BaseViewHolder扩展了RecyclerView.ViewHolder

You can create two ViewHolder class and Switch them in onCreateViewHolder . 您可以创建两个ViewHolder类并在onCreateViewHolder切换它们。 One containing your custom line, and others as your custom list items. 一个包含您的自定义行,其他包含您的自定义列表项。

   class ViewHolderLine extends RecyclerView.ViewHolder { //contains line
    }

    class ViewHolderItems extends RecyclerView.ViewHolder { //contains data
    }

  @Override
   public int getItemViewType(int position) {
        return item.get(position).getExpenseType();// Assume that this return 1 0r 2
   }

  @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup,int viewType) {

       switch (viewType) {
         case 1: return new ViewHolderLine();
         case 2:
             View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.list_layout, viewGroup, false);
            return new ItemViewHolder(itemView,viewType);
         }


  }

You can see the details description here for more info. 您可以在此处查看详细信息说明以获取更多信息。

One solution is define 2 types of RecyclerView rows (one for normal row and one for separator) Another solution is you should a Separator View in the bottom of your custom RecycleView row xml 一种解决方案是定义2种类型的RecyclerView行(一行用于普通行,一行用于分隔符)另一种解决方案是你应该在custom RecycleView row xml底部的Separator View custom RecycleView row xml

<View
    android:id="@+id/separatorView"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:visible="gone"
    android:background="@android:color/darker_gray"/>

Then in bindViewHolder of your RecyclerView.Adapter , hide the separator in normal row and visible it in separator row 然后在RecyclerView.Adapter bindViewHolder中,隐藏正常行中的分隔符并在分隔行中显示它

 @Override
    public void bindViewHolder(ViewHolder holder, int position) {
        if(position == separatorPosition){
           holder.separatorView.visible = View.VISIBLE;
        }else{
           holder.separatorView.visible = View.GONE;
        }
    }

Hope this help 希望这有帮助

if you know that you are only going to add separator in the 1st three items, then you can put a condition based on the position of the item, inside onBindViewHolder . 如果你知道你只想onBindViewHolder三个项目中添加分隔符,那么你可以在onBindViewHolder中根据项目的position设置一个条件。

ps: Please do not forget to add an else block after an if block ps:请不要忘记在if块之后添加else块

I have a recyclerview with section header and variable number of items in each section. 我有一个recyclelerview,其中包含节标题和每个部分中可变数量的项目。 The section should have a line separator across entire screen. 该部分应在整个屏幕上有一个行分隔符。 and items must have a padded line between them. 并且物品之间必须有填充线。 在此输入图像描述 So, my solution(in kotlin) was having two viewholders, one for header and one for item. 所以,我的解决方案(在kotlin中)有两个视图,一个用于标题,一个用于项目。 in getItemViewType, return the type based on the item. 在getItemViewType中,根据项返回类型。

override fun getItemViewType(position: Int): Int {
    if(dataList[position] is HeaderItem)
        return Companion.TYPE_HEADER
    return Companion.TYPE_ITEM
   }

and create the corresponding viewholder in createviewholder, bind accordingly. 并在createviewholder中创建相应的视图,相应地绑定。

Used 用过的

parent.getChildViewHolder(parent.getChildAt(i)) parent.getChildViewHolder(parent.getChildAt(i))的

to decide the padding(or color or width) for the item separator. 确定项目分隔符的填充(或颜色或宽度)。

class ItemDivider(context: Context) : RecyclerView.ItemDecoration() {
    private var mDivider: Drawable
    private val mContext = context
companion object {
    private val ATTRS = intArrayOf(android.R.attr.listDivider)
}

init {
    val styledAttributes = context.obtainStyledAttributes(ATTRS)
    mDivider = styledAttributes.getDrawable(0)
    mDivider.setTint(ContextCompat.getColor(mContext, <color>))
    styledAttributes.recycle()
}

override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State?) {
    val right = parent.width - parent.paddingRight

    for (i in 0 until parent.childCount - 1) {
        val child = parent.getChildAt(i)
        val params = child.layoutParams as RecyclerView.LayoutParams
        val left = if (parent.getChildViewHolder(child) is HeaderViewHolder) {
            0
        } else {
            child.left + params.leftMargin + mContext.resources.getDimension(<padding_in_dp>).toInt()
        }
        val top = child.bottom + params.bottomMargin
        val bottom = top + mDivider.intrinsicHeight + <divider_height>

        mDivider.setBounds(left, top, right, bottom)
        mDivider.draw(c)
    }
}
}

remember to replace the color, padding, divider height with valid values. 记得用有效值替换颜色,填充,分隔符高度。

Hope it helps! 希望能帮助到你!

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

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