标签:加载 设置 适配器 text mat blog cal extend nload
1 package com.z.fragment; 2 3 import android.os.Bundle; 4 import android.os.Environment; 5 import android.support.annotation.Nullable; 6 import android.support.v4.app.Fragment; 7 import android.support.v4.widget.SwipeRefreshLayout; 8 import android.support.v7.widget.LinearLayoutManager; 9 import android.view.LayoutInflater; 10 import android.view.View; 11 import android.view.ViewGroup; 12 import android.widget.Toast; 13 14 import com.google.gson.Gson; 15 import com. .MyApp; 16 import com.z. .R; 17 import com.z..adapter.AppNewAdapter; 18 import com.z..bean.HomeNewData; 19 import com.z..utils.ApiUrls; 20 import com.z..utils.DefaultCallBack; 21 import com.z..utils.HttpUtils; 22 import com.z..view.AutoLoadRecylerView; 23 24 import butterknife.BindView; 25 import butterknife.ButterKnife; 26 27 public class HomeFragment extends Fragment { 28 @BindView(R.id.rv) 29 AutoLoadRecylerView rv; 30 @BindView(R.id.refresh_layout) 31 SwipeRefreshLayout refreshLayout; 32 private AppNewAdapter newAdapter; 33 private int currPageIndex = 0; 34 @Nullable 35 @Override 36 public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) { 37 //1.布局xml|使用Code 38 int layoutId = R.layout.fragment_home_new; 39 //2.阅读接口文档 40 //3.可在调试模式获取json 41 currPageIndex = 0; 42 HttpUtils.get(ApiUrls.HOME + "?index=" + currPageIndex, callback); 43 //6.支持下拉刷新与滚动加载 44 View view = inflater.inflate(layoutId, container, false); 45 ButterKnife.bind(this, view); 46 //编写下拉刷新事件的处理 47 refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 48 @Override 49 public void onRefresh() { 50 // 1.清空数据 51 // 2.添加新获取的首页数据 52 // 3.列表刷新 53 Toast.makeText(getContext(), "下拉刷新中", Toast.LENGTH_SHORT).show(); 54 currPageIndex = 0; 55 HttpUtils.get(ApiUrls.HOME + "?index=" + currPageIndex, callback); 56 57 } 58 }); 59 rv.setLoadMoreListener(new AutoLoadRecylerView.loadMoreListener() { 60 //滚动到底部可以加载下一页数据 61 @Override 62 public void onLoadMore() { 63 if (currPageIndex == 2) { 64 Toast.makeText(getContext(), "没有更多记录", Toast.LENGTH_SHORT).show(); 65 } else { 66 Toast.makeText(getContext(), "加载更多中..", Toast.LENGTH_SHORT).show(); 67 currPageIndex += 1; 68 HttpUtils.get(ApiUrls.HOME + "?index=" + currPageIndex, callback); 69 } 70 } 71 }); 72 return view; 73 } 74 DefaultCallBack callback = new DefaultCallBack() { 75 @Override 76 public void onStart(int what) { 77 super.onStart(what); 78 if (currPageIndex == 0) { 79 refreshLayout.setRefreshing(true); 80 } 81 } 82 @Override 83 public void onFinish(int what) { 84 super.onFinish(what); 85 if (currPageIndex == 0) { 86 refreshLayout.setRefreshing(false); 87 } 88 } 89 90 @Override 91 protected void createView(String json) { 92 //4.使用Gsonformat 93 HomeNewData data = new Gson().fromJson(json, HomeNewData.class); 94 System.out.println(data); 95 if (currPageIndex == 0) //首页逻辑 96 { 97 if (newAdapter == null) {//1. 98 //5.高级控件的显示 99 rv.setLayoutManager(new LinearLayoutManager(getContext())); 100 //设置适配器 101 newAdapter = new AppNewAdapter(data.list); 102 rv.setAdapter(newAdapter); 103 } else { 104 newAdapter.getData().clear(); 105 //添加新获取的首页数据 106 newAdapter.getData().addAll(data.list); 107 newAdapter.notifyDataSetChanged(); 108 Toast.makeText(getContext(), "下拉刷新成功", Toast.LENGTH_SHORT).show(); 109 } 110 } else { 111 //添加 下一页数据 112 newAdapter.getData().addAll(data.list); 113 newAdapter.notifyDataSetChanged(); 114 Toast.makeText(getContext(), "加载更多完成", Toast.LENGTH_SHORT).show(); 115 //加载完成,设置loading 为false可以加载下一页 116 rv.setLoading(false); 117 } 118 } 119 }; 120 }
标签:加载 设置 适配器 text mat blog cal extend nload
原文地址:http://www.cnblogs.com/Oldz/p/6275741.html