首先导入类库,添加到所需的项目中
这是布局文件
<com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" ptr:ptrMode="both" > </com.handmark.pulltorefresh.library.PullToRefreshListView>接下来写下拉刷新,上拉加载的逻辑判断
private ArrayList<Detail> list=new ArrayList<Detail>(); private PullToRefreshListView lv; private MyListAdapter adapter; private int p=10; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { if (msg.what == 0) { String data = (String) msg.obj; // 解析Json数据 Gson gson = new Gson(); AllData ad = gson.fromJson(data, AllData.class); list=ad.getData(); adapter = new MyListAdapter(MainActivity.this, list); lv.setAdapter((ListAdapter) adapter); adapter.notifyDataSetChanged(); lv.onRefreshComplete(); }if(msg.what==1){ String data = (String) msg.obj; // 解析Json数据 Gson gson = new Gson(); AllData ad = gson.fromJson(data, AllData.class); list.addAll(ad.getData()); adapter.notifyDataSetChanged(); lv.onRefreshComplete(); } }; };
// 找到需要显示新闻的listview lv = (PullToRefreshListView) findViewById(R.id.lv); new Thread() { public void run() { readData(); } }.start();
//给listview设置下拉刷新和上拉加载的刷新监听事件
lv.setOnRefreshListener(new OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh( PullToRefreshBase<ListView> refreshView) { // 刷新时间 String label = DateUtils.formatDateTime( getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // Update the LastUpdatedLabel refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); new Thread() { public void run() { try { Thread.sleep(2000); z=z-1; } catch (InterruptedException e) { e.printStackTrace(); } readData();//下拉和初始化数据可以使用同一个方法 } }.start(); } @Override public void onPullUpToRefresh( PullToRefreshBase<ListView> refreshView) { new Thread() { public void run() { try { Thread.sleep(2000); p=p+1; } catch (InterruptedException e) { e.printStackTrace(); } readData1(); } }.start(); } });
//以下是下拉刷新和初始化数据的方法
protected void readData() { try { URL url = new URL( "http://api.expoon.com/AppNews/getNewsList/type/1/p/"+z); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setReadTimeout(8000); conn.setConnectTimeout(5000); InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuilder str = new StringBuilder(); String line; while ((line = br.readLine()) != null) { str.append(line); } Message msg = new Message(); msg.what = 0; msg.obj = str.toString(); handler.sendMessage(msg); Log.i("line", str.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
//这是上拉加载的方法
private void readData1() { try { URL url = new URL( "http://api.expoon.com/AppNews/getNewsList/type/1/p/"+p); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setReadTimeout(8000); conn.setConnectTimeout(5000); InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuilder str = new StringBuilder(); String line; while ((line = br.readLine()) != null) { str.append(line); } Message msg = new Message(); msg.what = 1; msg.obj = str.toString(); handler.sendMessage(msg); Log.i("line", str.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
转载于:https://www.cnblogs.com/daidai123/p/5549998.html
相关资源:数据结构—成绩单生成器