實現展示數據
public class HomeFragment extends Fragment { @BindView(R.id.rv) AutoLoadRecyclerView rv; @BindView(R.id.refresh_layout) SwipeRefreshLayout refreshLayout; ————> 插件拓展:④插件:ButterKnife(黃油刀)——> 一鍵處理 findViewById控件插件 private int currPageIndex = 0; private AppAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //1.布局xml使用code int layout = R.layout.fragment_home; //2.閱讀接口文檔 //3.可在調試模式獲取json currPageIndex = 0; HttpUtils.get(ApiUrls.HOME + "?index=" + currPageIndex, callback); //支持下拉刷洗與滾動加載 View view = inflater.inflate(layout, 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 AutoLoadRecyclerView.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); } } protected void createView(String json) { HomeWebInfo info = new Gson().fromJson(json, HomeWebInfo.class); if (currPageIndex == 0) {//首頁邏輯 if (adapter == null) { //5.高級控件的顯示 rv.setLayoutManager(new LinearLayoutManager(getContext())); //創建控件,設置適配器 adapter = new AppAdapter(info.list); rv.setAdapter(adapter); } else { adapter.getData().clear();//清空之前數據 adapter.getData().addAll(info.list);//添加新獲取的首頁數據 adapter.notifyDataSetChanged(); Toast.makeText(getContext(), "下拉刷新成功", Toast.LENGTH_SHORT).show(); } } else { //添加下一頁數據 adapter.getData().addAll(info.list); adapter.notifyDataSetChanged(); Toast.makeText(getContext(), "加載更多完成", Toast.LENGTH_SHORT).show(); //加載完成,設置loading為false可以加載下一頁 rv.setLoading(false); } } };}1.安裝插件 setting -> plugins 2.網絡下載butterknife支持包 project structure -> dependencies 3.在project的build.gradle中依賴包 dependencies { classpath ‘com.android.tools.build:gradle:2.2.2’ classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’ } 4.在app的build.gradle中首行增加 apply plugin: ‘android-apt’ dependencies { compile ‘com.jakewharton:butterknife:8.4.0’ apt ‘com.jakewharton:butterknife-compiler:8.4.0’ }
新聞熱點
疑難解答