1.效果
2.使用下拉刷新和上拉加载更多: https://github.com/android-cjj/Android-MaterialRefreshLayout
3.因为国内镜像仓库没有,所以不能使用镜像
allprojects {
repositories {
jcenter()
// maven{ url 'http://maven.oschina.net/content/groups/public/'}
}
}
4.布局fragment_hot.xml
5.item布局
*图片是可缓存的
6.HotWaresAdapter.java
package cniao5.com.cniao5shop.adapter;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
import cniao5.com.cniao5shop.R;
import cniao5.com.cniao5shop.bean.Wares;
/**
* Created by 菜鸟窝
* 一个专业的Android开发在线教育平台
*/
public class HotWaresAdapter extends RecyclerView.Adapter {
private List mDatas;
private LayoutInflater mInflater;
public HotWaresAdapter(List wares){
mDatas = wares;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//创建一个ViewHolder,把View传入
mInflater = LayoutInflater.from(parent.getContext());
View view = mInflater.inflate(R.layout.template_hot_wares,null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//绑定数据
Wares wares = getData(position);
holder.draweeView.setImageURI(Uri.parse(wares.getImgUrl()));
holder.textTitle.setText(wares.getName());
holder.textPrice.setText("¥"+wares.getPrice());
}
public Wares getData(int position){
return mDatas.get(position);
}
public List getDatas(){
return mDatas;
}
public void clearData(){
mDatas.clear();
notifyItemRangeRemoved(0,mDatas.size());
}
//下拉刷新更新数据
public void addData(List datas){
addData(0,datas);
}
//把数据更新,加载更多
public void addData(int position,List datas){
if(datas !=null && datas.size()>0) {
mDatas.addAll(datas);
notifyItemRangeChanged(position, mDatas.size());
}
}
@Override
public int getItemCount() {
if(mDatas!=null && mDatas.size()>0)
return mDatas.size();
return 0;
}
//获取控件
class ViewHolder extends RecyclerView.ViewHolder{
SimpleDraweeView draweeView;
TextView textTitle;
TextView textPrice;
public ViewHolder(View itemView) {
super(itemView);
draweeView = (SimpleDraweeView) itemView.findViewById(R.id.drawee_view);
textTitle= (TextView) itemView.findViewById(R.id.text_title);
textPrice= (TextView) itemView.findViewById(R.id.text_price);
}
}
}
7.HotFragment
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.HotWaresAdapter;
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 datas;
private HotWaresAdapter 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>(getContext()) {
@Override
public void onSuccess(Response response, Page 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 HotWaresAdapter(datas);
mRecyclerView.setAdapter(mAdatper);
//设置RecyclerView相关属性
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator()); //动画
mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL_LIST));
break;
case STATE_REFREH:
mAdatper.clearData();
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;
}
}
}