package com.example.mode_lun;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.adpter.MyAdapter;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class Fragment1 extends Fragment {
private PullToRefreshListView lv;
private String urlPath = "http://www.vipandroid.cn/week/bw_list.txt";
private List<Bean.ItemList> list = new ArrayList<>();
private MyAdapter adapter;
private Boolean isPull;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(),R.layout.fragment1,null);
lv =view.findViewById(R.id.list_view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new MyAdapter(getActivity(),list);
lv.setAdapter(adapter);
lv.setMode(PullToRefreshBase.Mode. BOTH );
lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
isPull = true;
doHttp();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
doHttp();
}
});
}
private void doHttp() {
new Helper().get(urlPath).Result(new Helper.HttpListener() {
@Override
public void success(String data) {
Gson gson = new Gson();
if (isPull) {
isPull = !isPull;
list.clear();
}
Bean bean = gson.fromJson(data,Bean.class);
List<Bean.ItemList> items = bean.getItems();
list.addAll(items);
adapter.notifyDataSetChanged();
}
});
}
}