/** * remove all header view from mHeaderLayout and set null to mHeaderLayout */ public void removeAllHeaderView() { if (getHeaderLayoutCount() == 0) return; mHeaderLayout.removeAllViews(); int position = getHeaderViewPosition(); if (position != -1) { notifyItemRemoved(position); } }
/** * @param header * @param index * @param orientation */ public void addHeaderView(View header, int index, int orientation) { if (mHeaderLayout == null) { mHeaderLayout = new LinearLayout(header.getContext()); if (orientation == LinearLayout.VERTICAL) { mHeaderLayout.setOrientation(LinearLayout.VERTICAL); mHeaderLayout.setLayoutParams(new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); } else { mHeaderLayout.setOrientation(LinearLayout.HORIZONTAL); mHeaderLayout.setLayoutParams(new LayoutParams(WRAP_CONTENT, MATCH_PARENT)); } } index = index >= mHeaderLayout.getChildCount() ? -1 : index; mHeaderLayout.addView(header, index); if (mHeaderLayout.getChildCount() == 1) { int position = getHeaderViewPosition(); if (position != -1) { notifyItemInserted(position); } } }
/** * remove header view from mHeaderLayout. * When the child count of mHeaderLayout is 0, mHeaderLayout will be set to null. * * @param header */ public void removeHeaderView(View header) { if (getHeaderLayoutCount() == 0) return; mHeaderLayout.removeView(header); if (mHeaderLayout.getChildCount() == 0) { int position = getHeaderViewPosition(); if (position != -1) { notifyItemRemoved(position); } } }