1.BaseAdapter
package cniao5.com.cniao5shop.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by <a href="http://www.cniao5.com">菜鸟窝</a>
* 一个专业的Android开发在线教育平台
*/
public abstract class BaseAdapter<T,H extends BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder>{
protected static final String TAG = BaseAdapter.class.getSimpleName();
protected final Context context;
protected final int layoutResId;
protected List<T> datas;
private OnItemClickListener mOnItemClickListener = null;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
public BaseAdapter(Context context, int layoutResId) {
this(context, layoutResId, null);
}
public BaseAdapter(Context context, int layoutResId, List<T> datas) {
this.datas = datas == null ? new ArrayList<T>() : datas;
this.context = context;
this.layoutResId = layoutResId;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutResId, viewGroup, false);
BaseViewHolder vh = new BaseViewHolder(view,mOnItemClickListener);
return vh;
}
@Override
public void onBindViewHolder(BaseViewHolder viewHoder, int position) {
T item = getItem(position);
convert((H)viewHoder, item);
}
@Override
public int getItemCount() {
if(datas==null || datas.size()<=0)
return 0;
return datas.size();
}
public T getItem(int position) {
if (position >= datas.size()) return null;
return datas.get(position);
}
public void clear(){
int itemCount = datas.size();
datas.clear();
this.notifyItemRangeRemoved(0,itemCount);
}
public List<T> getDatas(){
return datas;
}
public void addData(List<T> datas){
addData(0,datas);
}
public void addData(int position,List<T> datas){
if(datas !=null && datas.size()>0) {
this.datas.addAll(datas);
this.notifyItemRangeChanged(position, datas.size());
}
}
/**
* Implement this method and use the helper to adapt the view to the given item.
* @param viewHoder A fully initialized helper.
* @param item The item that needs to be displayed.
*/
protected abstract void convert(H viewHoder, T item); //绑定数据
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
}
2.BaseViewHolder
package cniao5.com.cniao5shop.adapter;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by <a href="http://www.cniao5.com">菜鸟窝</a>
* 一个专业的Android开发在线教育平台
*/
public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private SparseArray<View> views;
private BaseAdapter.OnItemClickListener mOnItemClickListener ;
public BaseViewHolder(View itemView,BaseAdapter.OnItemClickListener onItemClickListener){
super(itemView);
itemView.setOnClickListener(this);
this.mOnItemClickListener =onItemClickListener;
this.views = new SparseArray<View>();
}
public TextView getTextView(int viewId) {
return retrieveView(viewId);
}
public Button getButton(int viewId) {
return retrieveView(viewId);
}
public ImageView getImageView(int viewId) {
return retrieveView(viewId);
}
public View getView(int viewId) {
return retrieveView(viewId);
}
protected <T extends View> T retrieveView(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = itemView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v,getLayoutPosition());
}
}
}
3.SimpleAdapter
package cniao5.com.cniao5shop.adapter;
import android.content.Context;
import java.util.List;
/**
* Created by <a href="http://www.cniao5.com">菜鸟窝</a>
* 一个专业的Android开发在线教育平台
*/
public abstract class SimpleAdapter<T> extends BaseAdapter<T,BaseViewHolder> {
public SimpleAdapter(Context context, int layoutResId) {
super(context, layoutResId);
}
public SimpleAdapter(Context context, int layoutResId, List<T> datas) {
super(context, layoutResId, datas);
}
}
4.HWAdatper
package cniao5.com.cniao5shop.adapter;
import android.content.Context;
import android.net.Uri;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
import cniao5.com.cniao5shop.R;
import cniao5.com.cniao5shop.bean.Wares;
/**
* Created by <a href="http://www.cniao5.com">菜鸟窝</a>
* 一个专业的Android开发在线教育平台
*/
public class HWAdatper extends SimpleAdapter<Wares> {
public HWAdatper(Context context, List<Wares> datas) {
super(context, R.layout.template_hot_wares, datas);
}
@Override
protected void convert(BaseViewHolder viewHolder, Wares wares) {
SimpleDraweeView draweeView = (SimpleDraweeView) viewHolder.getView(R.id.drawee_view);
draweeView.setImageURI(Uri.parse(wares.getImgUrl()));
viewHolder.getTextView(R.id.text_title).setText(wares.getName());
// TextView textView = (TextView) viewHolder.getView(R.id.text_title);
}
}
5.使用 HotFragment.java
package cniao5.com.cniao5shop.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.okhttp.Response;
import java.util.List;
import cniao5.com.cniao5shop.Contants;
import cniao5.com.cniao5shop.R;
import cniao5.com.cniao5shop.adapter.DividerItemDecoration;
import cniao5.com.cniao5shop.adapter.HWAdatper;
import cniao5.com.cniao5shop.bean.Page;
import cniao5.com.cniao5shop.bean.Wares;
import cniao5.com.cniao5shop.http.OkHttpHelper;
import cniao5.com.cniao5shop.http.SpotsCallBack;
public class HotFragment extends Fragment {
private OkHttpHelper httpHelper = OkHttpHelper.getInstance();
private int currPage=1;
private int totalPage=1;
private int pageSize=10;
private List<Wares> datas;
private HWAdatper mAdatper;
@ViewInject(R.id.recyclerview)
private RecyclerView mRecyclerView;
@ViewInject(R.id.refresh_view)
private MaterialRefreshLayout mRefreshLaout;
private static final int STATE_NORMAL=0;
private static final int STATE_REFREH=1;
private static final int STATE_MORE=2;
private int state=STATE_NORMAL;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_hot,container,false);
ViewUtils.inject(this,view);
initRefreshLayout();
getData();
return view ;
}
private void initRefreshLayout(){
mRefreshLaout.setLoadMore(true);
mRefreshLaout.setMaterialRefreshListener(new MaterialRefreshListener() {
@Override
public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
refreshData();
}
@Override
public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
if(currPage <=totalPage)
loadMoreData();
else{
// Toast.makeText()
mRefreshLaout.finishRefreshLoadMore();
}
}
});
}
private void refreshData(){
currPage =1;
state=STATE_REFREH;
getData();
}
private void loadMoreData(){
currPage = ++currPage;
state = STATE_MORE;
getData();
}
private void getData(){
String url = Contants.API.WARES_HOT+"?curPage="+currPage+"&pageSize="+pageSize;
httpHelper.get(url, new SpotsCallBack<Page<Wares>>(getContext()) {
@Override
public void onSuccess(Response response, Page<Wares> waresPage) {
datas = waresPage.getList();
currPage = waresPage.getCurrentPage();
totalPage =waresPage.getTotalPage();
showData();
}
@Override
public void onError(Response response, int code, Exception e) {
}
});
}
private void showData(){
switch (state){
case STATE_NORMAL:
mAdatper = new HWAdatper(getContext(),datas);
mRecyclerView.setAdapter(mAdatper);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL_LIST));
break;
case STATE_REFREH:
mAdatper.clear();
mAdatper.addData(datas);
mRecyclerView.scrollToPosition(0);
mRefreshLaout.finishRefresh();
break;
case STATE_MORE:
mAdatper.addData(mAdatper.getDatas().size(),datas);
mRecyclerView.scrollToPosition(mAdatper.getDatas().size());
mRefreshLaout.finishRefreshLoadMore();
break;
}
}
}