Last active
August 29, 2015 14:10
-
-
Save extralam/004f87fc03aa42d4b4ad to your computer and use it in GitHub Desktop.
BaseRecycleViewAdapter For RecycleView v7 ( include back onItemClickListener, headerview, footerview)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Collections; | |
/** | |
* | |
* <p/> | |
* Created by extralam@HongKong on 24/11/2014. | |
* Copyright (c) 2014年 . All rights reserved. | |
*/ | |
public abstract class BaseRecycleViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
public interface TYPE { | |
int HEADER = 0; | |
int ITEM = 1; | |
int FOOTER = 2; | |
} | |
protected Context mmContext; | |
protected ArrayList<T> mItems; | |
protected BaseRecycleViewHeader mHeaderViews = null; | |
protected BaseRecycleViewFooter mFooterViews = null; | |
private OnItemClickListener mItemClickListener; | |
public BaseRecycleViewAdapter(Context context) { | |
mmContext = context; | |
mItems = new ArrayList<T>(); | |
} | |
public void addHeaderView(BaseRecycleViewHeader view) { | |
mHeaderViews = view; | |
} | |
public void addFooterView(BaseRecycleViewFooter view) { | |
mFooterViews = view; | |
} | |
private OnItemClickListener mOnItemClickLitener; | |
public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) { | |
this.mOnItemClickLitener = mOnItemClickLitener; | |
} | |
public BaseRecycleViewHeader getHeaderViewHolder(ViewGroup viewGroup) { | |
return mHeaderViews; | |
} | |
public BaseRecycleViewFooter getFooterViewHolder(ViewGroup viewGroup) { | |
return mFooterViews; | |
} | |
public BaseRecycleViewItem getNormalItemViewHolder(ViewGroup viewGroup) { | |
return null; | |
} | |
public T getItem(int position) { | |
if (mItems.size() > position || position < 0) { | |
return mItems.get(position); | |
} else { | |
return null; | |
} | |
} | |
/** | |
* Appends the specified element to the end of the list. | |
*/ | |
public void add(T item) { | |
mItems.add(item); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Inserts the specified element at the specified position in the list. | |
*/ | |
public void add(int position, T item) { | |
mItems.add(position, item); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Appends all of the elements in the specified collection to the end of the | |
* list, in the order that they are returned by the specified collection's | |
* Iterator. | |
*/ | |
public void addAll(Collection<? extends T> items) { | |
mItems.addAll(items); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Appends all of the elements to the end of the list, in the order that | |
* they are specified. | |
*/ | |
public void addAll(T... items) { | |
Collections.addAll(mItems, items); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Inserts all of the elements in the specified collection into the list, | |
* starting at the specified position. | |
*/ | |
public void addAll(int position, Collection<? extends T> items) { | |
mItems.addAll(position, items); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Inserts all of the elements into the list, starting at the specified | |
* position. | |
*/ | |
public void addAll(int position, T... items) { | |
for (int i = position; i < (items.length + position); i++) { | |
mItems.add(i, items[i]); | |
} | |
notifyDataSetChanged(); | |
} | |
/** | |
* Removes all of the elements from the list. | |
*/ | |
public void clear() { | |
mItems.clear(); | |
mItems = new ArrayList<T>(); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Replaces the element at the specified position in this list with the | |
* specified element. | |
*/ | |
public void set(int position, T item) { | |
mItems.set(position, item); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Removes the specified element from the list | |
*/ | |
public void remove(T item) { | |
mItems.remove(item); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Removes the element at the specified position in the list | |
*/ | |
public void remove(int position) { | |
mItems.remove(position); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Removes all elements at the specified positions in the list | |
*/ | |
public void removePositions(Collection<Integer> positions) { | |
ArrayList<Integer> positionsList = new ArrayList<Integer>(positions); | |
Collections.sort(positionsList); | |
Collections.reverse(positionsList); | |
for (int position : positionsList) { | |
mItems.remove(position); | |
} | |
notifyDataSetChanged(); | |
} | |
/** | |
* Removes all of the list's elements that are also contained in the | |
* specified collection | |
*/ | |
public void removeAll(Collection<T> items) { | |
mItems.removeAll(items); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Retains only the elements in the list that are contained in the specified | |
* collection | |
*/ | |
public void retainAll(Collection<T> items) { | |
mItems.retainAll(items); | |
notifyDataSetChanged(); | |
} | |
/** | |
* Returns the position of the first occurrence of the specified element in | |
* this list, or -1 if this list does not contain the element. More | |
* formally, returns the lowest position <tt>i</tt> such that | |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, | |
* or -1 if there is no such position. | |
*/ | |
public int indexOf(T item) { | |
return mItems.indexOf(item); | |
} | |
/** | |
* Temp No use | |
* | |
* @param viewHolder | |
* @param position | |
*/ | |
private void onBindHeaderView(RecyclerView.ViewHolder viewHolder, int position) { | |
} | |
/** | |
* Temp No use | |
* | |
* @param viewHolder | |
* @param position | |
*/ | |
private void onBindFooterView(RecyclerView.ViewHolder viewHolder, int position) { | |
} | |
public void onBindNormalItemView(RecyclerView.ViewHolder viewHolder, int position) { | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { | |
final RecyclerView.ViewHolder holder; | |
if (viewType == TYPE.HEADER) { | |
holder = getHeaderViewHolder(viewGroup); | |
} else if (viewType == TYPE.FOOTER) { | |
holder = getFooterViewHolder(viewGroup); | |
} else if (viewType == TYPE.ITEM) { | |
holder = getNormalItemViewHolder(viewGroup); | |
} else { | |
holder = null; | |
} | |
return holder; | |
} | |
@Override | |
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) { | |
if (viewHolder instanceof BaseRecycleViewHeader) { | |
onBindHeaderView(viewHolder, position); | |
} else if (viewHolder instanceof BaseRecycleViewFooter) { | |
onBindFooterView(viewHolder, position); | |
} else if (viewHolder instanceof BaseRecycleViewItem) { | |
onBindNormalItemView(viewHolder, position); | |
} | |
if (mOnItemClickLitener != null) { | |
viewHolder.itemView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
mOnItemClickLitener.onItemClick(viewHolder.itemView, position); | |
} | |
}); | |
} | |
} | |
@Override | |
public int getItemViewType(int position) { | |
if (mHeaderViews != null && position == 0) { | |
return TYPE.HEADER; | |
} | |
if (mHeaderViews != null && mFooterViews != null && position == (mItems.size() + 2)) { | |
return TYPE.FOOTER; | |
} | |
if (mHeaderViews == null && mFooterViews != null && position == (mItems.size() + 1)) { | |
return TYPE.FOOTER; | |
} | |
if (mHeaderViews == null && mFooterViews == null) { | |
return TYPE.ITEM; | |
} | |
return TYPE.ITEM; | |
} | |
@Override | |
public int getItemCount() { | |
if (mItems == null) { | |
return 0; | |
} | |
int total = mItems.size(); | |
if (mHeaderViews != null) { | |
total += 1; | |
} | |
if (mFooterViews != null) { | |
total += 1; | |
} | |
return total; | |
} | |
public String getString(int res) { | |
return mmContext.getString(res); | |
} | |
public interface OnItemClickListener { | |
public void onItemClick(View view, int position); | |
} | |
public static abstract class BaseRecycleViewHeader extends BaseReycleViewHolder { | |
public BaseRecycleViewHeader(View itemView) { | |
super(itemView); | |
} | |
} | |
public static abstract class BaseRecycleViewFooter extends BaseReycleViewHolder { | |
public BaseRecycleViewFooter(View itemView) { | |
super(itemView); | |
} | |
} | |
public static abstract class BaseRecycleViewItem extends BaseReycleViewHolder { | |
public BaseRecycleViewItem(View itemView) { | |
super(itemView); | |
} | |
} | |
private static class BaseReycleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { | |
public BaseReycleViewHolder(View itemView) { | |
super(itemView); | |
itemView.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
} | |
} | |
/* | |
* Copyright (C) 2014 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
public static class DividerItemDecoration extends RecyclerView.ItemDecoration { | |
private static final int[] ATTRS = new int[]{ | |
android.R.attr.listDivider | |
}; | |
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; | |
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; | |
private Drawable mDivider; | |
private int mOrientation; | |
public DividerItemDecoration(Context context, int orientation) { | |
final TypedArray a = context.obtainStyledAttributes(ATTRS); | |
mDivider = a.getDrawable(0); | |
a.recycle(); | |
setOrientation(orientation); | |
} | |
public void setOrientation(int orientation) { | |
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { | |
throw new IllegalArgumentException("invalid orientation"); | |
} | |
mOrientation = orientation; | |
} | |
@Override | |
public void onDraw(Canvas c, RecyclerView parent) { | |
if (mOrientation == VERTICAL_LIST) { | |
drawVertical(c, parent); | |
} else { | |
drawHorizontal(c, parent); | |
} | |
} | |
public void drawVertical(Canvas c, RecyclerView parent) { | |
final int left = parent.getPaddingLeft(); | |
final int right = parent.getWidth() - parent.getPaddingRight(); | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child | |
.getLayoutParams(); | |
final int top = child.getBottom() + params.bottomMargin; | |
final int bottom = top + mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(c); | |
} | |
} | |
public void drawHorizontal(Canvas c, RecyclerView parent) { | |
final int top = parent.getPaddingTop(); | |
final int bottom = parent.getHeight() - parent.getPaddingBottom(); | |
final int childCount = parent.getChildCount(); | |
for (int i = 0; i < childCount; i++) { | |
final View child = parent.getChildAt(i); | |
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child | |
.getLayoutParams(); | |
final int left = child.getRight() + params.rightMargin; | |
final int right = left + mDivider.getIntrinsicHeight(); | |
mDivider.setBounds(left, top, right, bottom); | |
mDivider.draw(c); | |
} | |
} | |
@Override | |
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { | |
if (mOrientation == VERTICAL_LIST) { | |
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); | |
} else { | |
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment