首先是RecyclerView的ViewHolder(這裏面的方法能夠根據本身須要進行添加)java
import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.v7.widget.RecyclerView.ViewHolder; import android.text.Spanned; import android.util.SparseArray; import android.view.View; import android.view.animation.AlphaAnimation; import android.widget.Checkable; import android.widget.CheckedTextView; import android.widget.ImageView; import android.widget.TextView; public class BaseHolder extends ViewHolder { private SparseArray<View> mViews; private View mConvertView; public BaseHolder(View mConvertView) { super(mConvertView); this.mConvertView = mConvertView; this.mViews = new SparseArray<>(); } public <T extends View> T getView(int viewId) { View view = mViews.get(viewId); if (null == view) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } /** * 設置TextView的內 * * @param viewId 控件id * @param text 文本內容 * @return ViewHolder */ public BaseHolder setText(int viewId, String text) { TextView view = getView(viewId); view.setText(text); return this; } /** * 設置TextView的內 * * @param viewId 控件id * @param spanned 文本內容 * @return BaseHolder */ public BaseHolder setText(int viewId, Spanned spanned) { TextView view = getView(viewId); view.setText(spanned); return this; } /** * 設置TextView的內 * * @param viewId 控件id * @param resId 資源文件中的id * @return BaseHolder */ public BaseHolder setText(int viewId, int resId) { TextView view = getView(viewId); view.setText(resId); return this; } /** * @param viewId 控件id * @param resId 資源文件中的id * @return BaseHolder */ public BaseHolder setImageResource(int viewId, int resId) { ImageView view = getView(viewId); view.setImageResource(resId); return this; } /** * @param viewId 控件id * @param bitmap 圖片的的Bitmap * @return BaseHolder */ public BaseHolder setImageBitmap(int viewId, Bitmap bitmap) { ImageView view = getView(viewId); view.setImageBitmap(bitmap); return this; } /** * @param viewId 控件id * @param drawable 圖片的的Drawable * @return BaseHolder */ public BaseHolder setImageDrawable(int viewId, Drawable drawable) { ImageView view = getView(viewId); view.setImageDrawable(drawable); return this; } /** * 給控件設置tag * * @param viewId 控件id * @param obj tag * @return ViewHolder */ public BaseHolder setTag(int viewId, Object obj) { View view = getView(viewId); view.setTag(obj); return this; } /** * 給控件設置OnClickListener * * @param viewId 控件id * @param listener 點擊事件 * @return ViewHolder */ public BaseHolder setClickListener(int viewId, View.OnClickListener listener, Object tag) { View view = getView(viewId); view.setOnClickListener(listener); view.setTag(tag); return this; } /** * 給控件設置OnClickListener * * @param viewIds 控件ids * @param listener 點擊事件 * @return ViewHolder */ public BaseHolder setClickListener(View.OnClickListener listener, Object tag, int... viewIds) { for (int viewId : viewIds) { View view = getView(viewId); view.setTag(tag); view.setOnClickListener(listener); } return this; } /** * 設置選中 * * @param checked 是否選擇 * @param viewIds view的Id * @return BaseHolder */ public BaseHolder setChecked(boolean checked, int... viewIds) { for (int viewId : viewIds) { Checkable view = getView(viewId); view.setChecked(checked); } return this; } /** * 設置view可見 * * @param visible 是否看見 * @param viewIds view的Id * @return BaseHolder */ public BaseHolder setVisibility(boolean visible, int... viewIds) { for (int viewId : viewIds) { setVisible(viewId, visible); } return this; } public BaseHolder setVisible(int viewId, boolean visible) { View view = getView(viewId); view.setVisibility(visible ? View.VISIBLE : View.GONE); return this; } public BaseHolder setAlpha(float value, int... viewIds) { for (int viewId : viewIds) { setAlpha(viewId, value); } return this; } public BaseHolder setAlpha(int viewId, float value) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { getView(viewId).setAlpha(value); } else { AlphaAnimation alpha = new AlphaAnimation(value, value); alpha.setDuration(0); alpha.setFillAfter(true); getView(viewId).startAnimation(alpha); } return this; } public BaseHolder setImageLevel(int viewId, int level) { ImageView view = getView(viewId); view.setImageLevel(level); return this; } public BaseHolder setSelected(int viewId, boolean selected) { CheckedTextView view = getView(viewId); view.setSelected(selected); return this; } }
而後就是關鍵的Adapter了android
import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; public abstract class LBaseAdapter<T> extends RecyclerView.Adapter<BaseHolder> { public static final int ITEM_TYPE_HEADER = 0; public static final int ITEM_TYPE_CONTENT = 1; public static final int ITEM_TYPE_BOTTOM = 2; protected int layoutId; protected List<T> mDatas; protected View mHeaderView, emptyView, networkView, mBottomView; protected int mHeaderCount = 0;//頭部View個數 protected int mBottomCount = 0;//底部View個數 public LBaseAdapter(int layoutId) { this.mDatas = new ArrayList<>(); this.layoutId = layoutId; } public void setHeaderView(View headerView) { mHeaderCount=1; mHeaderView = headerView; notifyItemInserted(0); } public void addItems(List<T> items) { if (ArrayUtil.isNotEmpty(items)) { int oldCont = getItemCount(); mDatas.addAll(items); if (oldCont == 0) { notifyDataSetChanged(); } else { notifyItemRangeChanged(oldCont, mDatas.size()); } } } public void addItem(T item) { mDatas.add(item); notifyDataSetChanged(); } public void addItems(List<T> items, boolean isRefresh) { if (isRefresh) { mDatas.clear(); notifyDataSetChanged(); } addItems(items); } public void showEmptyView() { if (null != networkView) { boolean networkAvailable = CommonUtils.isNetworkAvailable(networkView.getContext()); networkView.setVisibility(networkAvailable || !isEmpty() ? View.GONE : View.VISIBLE); } if (null != emptyView) { if (null != networkView && networkView.getVisibility() == View.VISIBLE) { emptyView.setVisibility(View.GONE); return; } emptyView.setVisibility(isEmpty() ? View.VISIBLE : View.GONE); } if (null != mBottomView) mBottomView.setVisibility(isEmpty() ? View.GONE : View.VISIBLE); } @Override public int getItemViewType(int position) { int dataItemCount = mDatas.size(); if (mHeaderCount != 0 && position < mHeaderCount) { return ITEM_TYPE_HEADER; } else if (mBottomCount != 0 && position >= (mHeaderCount + dataItemCount)) { return ITEM_TYPE_BOTTOM; } else { return ITEM_TYPE_CONTENT; } } @Override public BaseHolder onCreateViewHolder(ViewGroup parent, final int viewType) { if (viewType == ITEM_TYPE_HEADER) { return new BaseHolder(mHeaderView); } else if (viewType == ITEM_TYPE_CONTENT) { View mView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false); BaseHolder baseHolder = new BaseHolder(mView); baseHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Object vTag = view.getTag(); if (vTag != null && vTag instanceof Integer) onItemClick(mDatas.get(Integer.valueOf(vTag.toString()))); } }); return baseHolder; } else if (viewType == ITEM_TYPE_BOTTOM) { return new BaseHolder(mBottomView); } return null; } protected void onItemClick(T t) { } @Override public void onBindViewHolder(BaseHolder baseHolder, int position) { int itemViewType = getItemViewType(position); if ((itemViewType == ITEM_TYPE_HEADER) || (itemViewType == ITEM_TYPE_BOTTOM)) return; int pos = getRealPosition(baseHolder); baseHolder.itemView.setTag(pos); onBind(baseHolder, pos, mDatas.get(pos)); } @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) { return (isHeaderView(position) || isBottomView(position)) ? gridManager.getSpanCount() : 1; } }); } } //判斷當前item是不是HeadView public boolean isHeaderView(int position) { return mHeaderCount != 0 && position < mHeaderCount; } //判斷當前item是不是FooterView public boolean isBottomView(int position) { return mBottomCount != 0 && position >= (mHeaderCount + mDatas.size()); } @Override public void onViewAttachedToWindow(BaseHolder baseHolder) { super.onViewAttachedToWindow(baseHolder); ViewGroup.LayoutParams lp = baseHolder.itemView.getLayoutParams(); if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams && baseHolder.getLayoutPosition() == 0) { StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; p.setFullSpan(true); } } public int getRealPosition(RecyclerView.ViewHolder holder) { int position = holder.getLayoutPosition(); return position - mHeaderCount; } @Override public int getItemCount() { return mDatas.size() + mHeaderCount + mBottomCount; } public abstract void onBind(BaseHolder baseHolder, int realPosition, T t); public boolean isEmpty() { return ArrayUtil.isEmpty(mDatas); } public void setEmptyView(View emptyView) { this.emptyView = emptyView; } public T getItem(int position) { return mDatas.get(position); } public void setNetworkView(View networkView) { this.networkView = networkView; } public void setBottomView(View bottomView) { mBottomCount=1; this.mBottomView = bottomView; notifyItemInserted(getItemCount()); } }
其中有幾個工具類就沒上傳了,無非就是判斷集合是否是爲空或者是否是有網絡的狀況。其實還有不少須要補充的。網絡