1.導依賴html
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3' compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.3'//沒有使用特殊Header,能夠不加這行
2.XML佈局文件android
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/srl" tools:context="com.example.yuekao003.CFragment"> <ListView android:id="@+id/lv" android:overScrollMode="never" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </com.scwang.smartrefresh.layout.SmartRefreshLayout>
3.代碼實現json
1)api
RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout); refreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { refreshlayout.finishRefresh(2000); } }); refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() { @Override public void onLoadmore(RefreshLayout refreshlayout) { refreshlayout.finishLoadmore(2000); } });
2)ide
//設置 Header 爲 Material風格 refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true)); //設置 Footer 爲 球脈衝 refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
全局代碼:佈局
public class CFragment extends Fragment { private ListView lv; private RefreshLayout srl; private CAdapter cAdapter; private List<CBean.DataBean.ForecastBean> list; private List<CBean.DataBean.ForecastBean> list2; private String URL = "http://www.sojson.com/open/api/weather/json.shtml?city=北京"; Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case 1: String s = (String) msg.obj; Gson gson = new Gson(); CBean bean = gson.fromJson(s, CBean.class); list = bean.getData().getForecast(); list2 = new ArrayList<>(); for (int i = 0; i < 3; i++) { list2.add(list.get(i)); } cAdapter = new CAdapter(getActivity(),list2); lv.setAdapter(cAdapter); } } }; public CFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.fragment_c, container, false); initView(inflate); initData(); initShuaxin(); return inflate; } private void initShuaxin() { srl.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { refreshlayout.finishRefresh(2000); initData(); } }); srl.setOnLoadmoreListener(new OnLoadmoreListener() { @Override public void onLoadmore(RefreshLayout refreshlayout) { refreshlayout.finishLoadmore(2000); initTianJia(); } }); //設置 Header 爲 Material風格 srl.setRefreshHeader(new MaterialHeader(getActivity()).setShowBezierWave(true)); //設置 Footer 爲 球脈衝 srl.setRefreshFooter(new BallPulseFooter(getActivity()).setSpinnerStyle(SpinnerStyle.Scale)); } private void initTianJia() { int itemCount = cAdapter.getCount(); if (itemCount+3<list.size()){ for (int i = itemCount; i < itemCount+3; i++) { list2.add(list.get(i)); } }else { for (int i = itemCount; i < list.size(); i++) { list2.add(list.get(i)); } } cAdapter.notifyDataSetChanged(); } private void initData() { OkHttpClient okHttpClient = OkHttpUtil.getInstance(); Request request = new Request.Builder().url(URL).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String s = response.body().string(); handler.obtainMessage(1,s).sendToTarget(); } }); } private void initView(View inflate) { lv = (ListView) inflate.findViewById(R.id.lv); srl = (RefreshLayout) inflate.findViewById(R.id.srl); } }