Skip to content

Commit

Permalink
Add GridLayoutManager HeaderView support
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadCym committed Jun 24, 2016
1 parent 1832652 commit d708811
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
Binary file modified demo_res/demo.apk
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
Expand Down Expand Up @@ -134,6 +135,7 @@ public void setOnLoadMoreListener(RequestLoadMoreListener requestLoadMoreListene

/**
* Sets the duration of the animation.
*
* @param duration The length of the animation, in milliseconds.
*/
public void setDuration(int duration) {
Expand Down Expand Up @@ -181,6 +183,7 @@ public void setPageSize(int pageSize) {
public int getPageSize() {
return this.pageSize;
}

/**
* Register a callback to be invoked when an item in this AdapterView has
* been clicked.
Expand All @@ -190,6 +193,7 @@ public int getPageSize() {
public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
}

/**
* Interface definition for a callback to be invoked when an item in this
* AdapterView has been clicked.
Expand All @@ -198,12 +202,14 @@ public interface OnRecyclerViewItemClickListener {
/**
* Callback method to be invoked when an item in this AdapterView has
* been clicked.
* @param view The view within the AdapterView that was clicked (this
* will be a view provided by the adapter)
*
* @param view The view within the AdapterView that was clicked (this
* will be a view provided by the adapter)
* @param position The position of the view in the adapter.
*/
public void onItemClick(View view, int position);
}

/**
* Register a callback to be invoked when an item in this AdapterView has
* been clicked and held
Expand All @@ -222,18 +228,21 @@ public interface OnRecyclerViewItemLongClickListener {
/**
* callback method to be invoked when an item in this view has been
* click and held
* @param view The view whihin the AbsListView that was clicked
*
* @param view The view whihin the AbsListView that was clicked
* @param position The position of the view int the adapter
* @return true if the callback consumed the long click ,false otherwise
*/
public boolean onItemLongClick(View view, int position);
}

private OnRecyclerViewItemChildClickListener mChildClickListener;

/**
* Register a callback to be invoked when childView in this AdapterView has
* been clicked and held
* {@link OnRecyclerViewItemChildClickListener}
*
* @param childClickListener The callback that will run
*/
public void setOnRecyclerViewItemChildClickListener(OnRecyclerViewItemChildClickListener childClickListener) {
Expand Down Expand Up @@ -262,24 +271,25 @@ public void onClick(View v) {
* @param layoutResId The layout resource id of each item.
* @param data A new list is created out of this one to avoid mutable list
*/
public BaseQuickAdapter( int layoutResId, List<T> data) {
public BaseQuickAdapter(int layoutResId, List<T> data) {
this.mData = data == null ? new ArrayList<T>() : data;
if (layoutResId != 0) {
this.mLayoutResId = layoutResId;
}
}

public BaseQuickAdapter( List<T> data) {
public BaseQuickAdapter(List<T> data) {
this(0, data);
}

public BaseQuickAdapter(View contentView, List<T> data) {
this( 0, data);
this(0, data);
mContentView = contentView;
}

/**
* remove the item associated with the specified position of adapter
*
* @param position
*/
public void remove(int position) {
Expand All @@ -289,7 +299,8 @@ public void remove(int position) {
}

/**
* insert a item associated with the specified position of adapter
* insert a item associated with the specified position of adapter
*
* @param position
* @param item
*/
Expand Down Expand Up @@ -326,6 +337,7 @@ public void addData(List<T> data) {

/**
* set a loadingView
*
* @param loadingView
*/
public void setLoadingView(View loadingView) {
Expand All @@ -334,6 +346,7 @@ public void setLoadingView(View loadingView) {

/**
* Get the data of list
*
* @return
*/
public List getData() {
Expand All @@ -353,20 +366,25 @@ public T getItem(int position) {

/**
* if setHeadView will be return 1 if not will be return 0
*
* @return
*/
public int getHeaderViewsCount() {
return mHeaderView == null ? 0 : 1;
}

/**
* if mFooterView will be return 1 or not will be return 0
*
* @return
*/
public int getFooterViewsCount() {
return mFooterView == null ? 0 : 1;
}

/**
* if mEmptyView will be return 1 or not will be return 0
*
* @return
*/
public int getmEmptyViewCount() {
Expand Down Expand Up @@ -408,10 +426,10 @@ public int getItemCount() {
* Get the type of View that will be created by {@link #getItemView(int, ViewGroup)} for the specified item.
*
* @param position The position of the item within the adapter's data set whose view type we
* want.
* want.
* @return An integer representing the type of View. Two views should share the same type if one
* can be converted to the other in {@link #getItemView(int, ViewGroup)}. Note: Integers must be in the
* range 0 to {@link #getItemCount()} - 1.
* can be converted to the other in {@link #getItemView(int, ViewGroup)}. Note: Integers must be in the
* range 0 to {@link #getItemCount()} - 1.
*/
@Override
public int getItemViewType(int position) {
Expand Down Expand Up @@ -460,7 +478,7 @@ public int getItemViewType(int position) {
else if ((!mFootAndEmptyEnable || !mHeadAndEmptyEnable) && position == 1 && mFooterView != null) {
return FOOTER_VIEW;
}
} else if (mData.size() == 0 &&mEmptyView != null && getItemCount() == (mHeadAndEmptyEnable ? 2 : 1) && mEmptyEnable) {
} else if (mData.size() == 0 && mEmptyView != null && getItemCount() == (mHeadAndEmptyEnable ? 2 : 1) && mEmptyEnable) {
return EMPTY_VIEW;
} else if (position == mData.size() + getHeaderViewsCount()) {
if (mNextLoadEnable)
Expand Down Expand Up @@ -510,9 +528,10 @@ private BaseViewHolder getLoadingView(ViewGroup parent) {
}

/**
* Called when a view created by this adapter has been attached to a window.
* simple to solve item will layout using all
* {@link #setFullSpan(RecyclerView.ViewHolder)}
* Called when a view created by this adapter has been attached to a window.
* simple to solve item will layout using all
* {@link #setFullSpan(RecyclerView.ViewHolder)}
*
* @param holder
*/
@Override
Expand All @@ -529,6 +548,7 @@ public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
* is vertical, the view will have full width; if orientation is horizontal, the view will
* have full height.
* if the hold view use StaggeredGridLayoutManager they should using all span area
*
* @param holder True if this item should traverse all spans.
*/
protected void setFullSpan(RecyclerView.ViewHolder holder) {
Expand All @@ -538,11 +558,28 @@ protected void setFullSpan(RecyclerView.ViewHolder holder) {
}
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if (manager instanceof GridLayoutManager) {
final GridLayoutManager gridManager = ((GridLayoutManager) manager);
gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int type = getItemViewType(position);
return (type == EMPTY_VIEW || type == HEADER_VIEW || type == FOOTER_VIEW || type == LOADING_VIEW) ? gridManager.getSpanCount() : 1;
}
});
}
}

/**
* To bind different types of holder and solve different the bind events
* @see #getDefItemViewType(int)
*
* @param holder
* @param positions
* @see #getDefItemViewType(int)
*/
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int positions) {
Expand Down Expand Up @@ -571,7 +608,7 @@ public void onBindViewHolder(final RecyclerView.ViewHolder holder, int positions
if (mItemTouchHelper != null && itemDragEnabled && viewType != LOADING_VIEW && viewType != HEADER_VIEW
&& viewType != EMPTY_VIEW && viewType != FOOTER_VIEW) {
if (mToggleViewId != NO_TOGGLE_VIEW) {
View toggleView = ((BaseViewHolder)holder).getView(mToggleViewId);
View toggleView = ((BaseViewHolder) holder).getView(mToggleViewId);
if (toggleView != null) {
toggleView.setTag(holder);
if (mDragOnLongPress) {
Expand Down Expand Up @@ -600,14 +637,17 @@ protected BaseViewHolder createBaseViewHolder(ViewGroup parent, int layoutResId)

/**
* easy to show a simple headView
*
* @param header
*/
public void addHeaderView(View header) {
this.mHeaderView = header;
this.notifyDataSetChanged();
}

/**
* easy to show a simple footerView
*
* @param footer
*/
public void addFooterView(View footer) {
Expand All @@ -624,7 +664,6 @@ public void setEmptyView(View emptyView) {
}

/**
*
* @param isHeadAndEmpty false will not show headView if the data is empty true will show emptyView and headView
* @param emptyView
*/
Expand Down Expand Up @@ -672,8 +711,7 @@ public void isNextLoad(boolean isNextLoad) {

/**
* @param isNextLoad true
* if true when loading more data can show loadingView
* if true when loading more data can show loadingView
*/
public void notifyDataChangedAfterLoadMore(boolean isNextLoad) {
mNextLoadEnable = isNextLoad;
Expand All @@ -684,6 +722,7 @@ public void notifyDataChangedAfterLoadMore(boolean isNextLoad) {

/**
* add more data
*
* @param data
* @param isNextLoad
*/
Expand All @@ -703,6 +742,7 @@ private void addLoadMore(RecyclerView.ViewHolder holder) {

/**
* init the baseViewHolder to register onRecyclerViewItemClickListener and onRecyclerViewItemLongClickListener
*
* @param baseViewHolder
*/
private void initItemClickListener(final BaseViewHolder baseViewHolder) {
Expand All @@ -726,6 +766,7 @@ public boolean onLongClick(View v) {

/**
* add animation when you want to show time
*
* @param holder
*/
private void addAnimation(RecyclerView.ViewHolder holder) {
Expand All @@ -747,6 +788,7 @@ private void addAnimation(RecyclerView.ViewHolder holder) {

/**
* set anim to start when loading
*
* @param anim
* @param index
*/
Expand All @@ -757,19 +799,19 @@ protected void startAnim(Animator anim, int index) {

/**
* Determine whether it is loaded more
*
* @return
*/
private boolean isLoadMore() {
return mNextLoadEnable && pageSize != -1 && mRequestLoadMoreListener != null && mData.size() >= pageSize;
}

/**
*
* @param layoutResId ID for an XML layout resource to load
* @param parent Optional view to be the parent of the generated hierarchy or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy
* @return view will be return
* @param parent Optional view to be the parent of the generated hierarchy or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy
* @return view will be return
*/
protected View getItemView(int layoutResId, ViewGroup parent) {
return mLayoutInflater.inflate(layoutResId, parent, false);
Expand Down Expand Up @@ -839,6 +881,7 @@ public void openLoadAnimation() {

/**
* {@link #addAnimation(RecyclerView.ViewHolder)}
*
* @param firstOnly true just show anim when first loading false show anim when load the data every time
*/
public void isFirstOnly(boolean firstOnly) {
Expand Down Expand Up @@ -915,6 +958,7 @@ public boolean onTouch(View v, MotionEvent event) {
/**
* Enable drag items.
* Use itemView as the toggleView when long pressed.
*
* @param itemTouchHelper {@link ItemTouchHelper}
*/
public void enableDragItem(@NonNull ItemTouchHelper itemTouchHelper) {
Expand All @@ -923,8 +967,9 @@ public void enableDragItem(@NonNull ItemTouchHelper itemTouchHelper) {

/**
* Enable drag items. Use the specified view as toggle.
*
* @param itemTouchHelper {@link ItemTouchHelper}
* @param toggleViewId The toggle view's id.
* @param toggleViewId The toggle view's id.
* @param dragOnLongPress If true the drag event will be trigger on long press, otherwise on touch down.
*/
public void enableDragItem(@NonNull ItemTouchHelper itemTouchHelper, int toggleViewId, boolean dragOnLongPress) {
Expand Down

0 comments on commit d708811

Please sign in to comment.