package lmm.bw.com.mythird;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
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 android.widget.Toast;
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 FragmentA extends Fragment {
String urlstring = "http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?page=1&count=10";
int page;
private PullToRefreshListView pull;
public List<PerBean.ResultBean> list = new ArrayList<>();
private MyAdapter myAdapter;
private Dao dao;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(),R.layout.fragment11,null);
pull = view.findViewById(R.id.plistview);
inData(page);
myAdapter = new MyAdapter(getActivity(), list);
pull.setAdapter(myAdapter);
pull.setMode(PullToRefreshBase.Mode.BOTH);
dao = new Dao(getActivity());
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
list.clear();
page = 0;
inData(page);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
page++;
inData(page);
}
});
return view;
}
private void inData(int page) {
Boolean iswork = NetUtils.iswork(getActivity());
if (iswork){
String myurl = urlstring + page;
new MyAsycnTask().execute(myurl);
}else {
Toast.makeText(getActivity(),"没网",Toast.LENGTH_SHORT).show();
list = dao.select();
myAdapter = new MyAdapter(getActivity(),list);
pull.setAdapter(myAdapter);
}
}
private class MyAsycnTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
String gson = NetUtils.getGson(strings[0]);
return gson;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
PerBean perBean = gson.fromJson(s, PerBean.class);
List<PerBean.ResultBean> result = perBean.getResult();
list.addAll(result);
List<PerBean.ResultBean> select = dao.select();
if (select.size() <=100){
for (int i = 0; i<list.size();i++){
String tee = list.get(i).getName();
String tes = list.get(i).getSummary();
String ivs = list.get(i).getImageUrl();
dao.add(ivs,tes,tee);
}
}
myAdapter.notifyDataSetChanged();
pull.onRefreshComplete();
}
}
}