package com.z.fragment; import android.os.Bundle; import android.os.Environment; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.google.gson.Gson; import com. .MyApp; import com.z. .R; import com.z..adapter.AppNewAdapter; import com.z..bean.HomeNewData; import com.z..utils.ApiUrls; import com.z..utils.DefaultCallBack; import com.z..utils.HttpUtils; import com.z..view.AutoLoadRecylerView; import butterknife.BindView; import butterknife.ButterKnife; public class HomeFragment extends Fragment { @BindView(R.id.rv) AutoLoadRecylerView rv; @BindView(R.id.refresh_layout) SwipeRefreshLayout refreshLayout; private AppNewAdapter newAdapter; private int currPageIndex = 0; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) { //1.布局xml|使用Code int layoutId = R.layout.fragment_home_new; //2.阅读接口文档 //3.可在调试模式获取json currPageIndex = 0; HttpUtils.get(ApiUrls.HOME + "?index=" + currPageIndex, callback); //6.支持下拉刷新与滚动加载 View view = inflater.inflate(layoutId, container, false); ButterKnife.bind(this, view); //编写下拉刷新事件的处理 refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // 1.清空数据 // 2.添加新获取的首页数据 // 3.列表刷新 Toast.makeText(getContext(), "下拉刷新中", Toast.LENGTH_SHORT).show(); currPageIndex = 0; HttpUtils.get(ApiUrls.HOME + "?index=" + currPageIndex, callback); } }); rv.setLoadMoreListener(new AutoLoadRecylerView.loadMoreListener() { //滚动到底部可以加载下一页数据 @Override public void onLoadMore() { if (currPageIndex == 2) { Toast.makeText(getContext(), "没有更多记录", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getContext(), "加载更多中..", Toast.LENGTH_SHORT).show(); currPageIndex += 1; HttpUtils.get(ApiUrls.HOME + "?index=" + currPageIndex, callback); } } }); return view; } DefaultCallBack callback = new DefaultCallBack() { @Override public void onStart(int what) { super.onStart(what); if (currPageIndex == 0) { refreshLayout.setRefreshing(true); } } @Override public void onFinish(int what) { super.onFinish(what); if (currPageIndex == 0) { refreshLayout.setRefreshing(false); } } @Override protected void createView(String json) { //4.使用Gsonformat HomeNewData data = new Gson().fromJson(json, HomeNewData.class); System.out.println(data); if (currPageIndex == 0) //首页逻辑 { if (newAdapter == null) {//1. //5.高级控件的显示 rv.setLayoutManager(new LinearLayoutManager(getContext())); //设置适配器 newAdapter = new AppNewAdapter(data.list); rv.setAdapter(newAdapter); } else { newAdapter.getData().clear(); //添加新获取的首页数据 newAdapter.getData().addAll(data.list); newAdapter.notifyDataSetChanged(); Toast.makeText(getContext(), "下拉刷新成功", Toast.LENGTH_SHORT).show(); } } else { //添加 下一页数据 newAdapter.getData().addAll(data.list); newAdapter.notifyDataSetChanged(); Toast.makeText(getContext(), "加载更多完成", Toast.LENGTH_SHORT).show(); //加载完成,设置loading 为false可以加载下一页 rv.setLoading(false); } } }; }