主方法中的代码
private String name; private String news_url; private List<news> newslist; private PullToRefreshListView lv; private Myadap myadap; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //接收传过来的URL Bundle bundle=getArguments(); name= (String) bundle.get("name"); news_url= (String) bundle.get("url"); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.pager_item,null); lv= (PullToRefreshListView) view.findViewById(R.id.lv); //设置上拉加载和下拉刷新 lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { getJson(); lv.onRefreshComplete(); } }); lv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() { @Override public void onLastItemVisible() { Load(); } }); //调用解析数据的方法 getJson(); return view; } private void getJson() { new AsyncTask<String,Integer,String>(){ @Override protected String doInBackground(String... params) { String url=params[0]; String json=new NetHowoder().getjsonBy(url); return json; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Log.d("TAG",s+"-----------------"); Gson gson=new Gson(); UserBean userBean = gson.fromJson(s, UserBean.class); newslist = userBean.getNewslist(); //设置适配器 myadap= new Myadap(getActivity(), newslist); lv.setAdapter(myadap); myadap.notifyDataSetChanged(); } }.execute(news_url); } private void Load(){ new AsyncTask<String,Integer,String>(){ @Override protected String doInBackground(String... params) { String url=params[0]; String json=new NetHowoder().getjsonBy(url); return json; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Log.d("TAG",s+"-----------------"); Gson gson=new Gson(); UserBean userBean = gson.fromJson(s, UserBean.class); List<news> newslist3 = userBean.getNewslist(); newslist.addAll(newslist3); myadap.notifyDataSetChanged(); } }.execute(news_url); }///XML布局中写上 PullToRefreshListView
<com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="fill_parent" />