xml中引用PullToRefreshListView
来源:互联网 发布:矩阵开关介绍 编辑:程序博客网 时间:2024/06/18 15:23
在xml中引用
<com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pull_refresh_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:fadingEdge="none" ptr:ptrDrawable="@drawable/default_ptr_rotate" android:fastScrollEnabled="false" android:footerDividersEnabled="false" ptr:ptrMode="both" android:headerDividersEnabled="false" android:smoothScrollbar="true" > </com.handmark.pulltorefresh.library.PullToRefreshListView>
代码中,MainActivity
package com.bw.yangzhengzheng20160517;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.text.format.DateUtils;import com.bw.asynctask.MyAsyncTask;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;import com.handmark.pulltorefresh.library.PullToRefreshListView;public class MainActivity extends Activity { private PullToRefreshListView pull_refresh_list; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pull_refresh_list = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); pull_refresh_list.setOnRefreshListener(new OnRefreshListener2() { @Override public void onPullDownToRefresh(PullToRefreshBase refreshView) { handler.postDelayed(new Runnable() { public void run() { MyAsyncTask task2 = new MyAsyncTask(MainActivity.this,pull_refresh_list); task2.execute("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=242&pn=20&rn=20&dtype=xml"); onLoad(); pull_refresh_list.onRefreshComplete(); } }, 2000); } @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { handler.postDelayed(new Runnable() { public void run() { MyAsyncTask task3 = new MyAsyncTask(MainActivity.this,pull_refresh_list); task3.execute("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=242&pn=30&rn=20&dtype=xml"); onLoad(); pull_refresh_list.onRefreshComplete(); } }, 2000); } }); } public void requestData() { MyAsyncTask task = new MyAsyncTask(this,pull_refresh_list); task.execute("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id=242&pn=0&rn=20&dtype=xml"); } /* * 当前时间 */ private void onLoad() { String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // Update the LastUpdatedLabel pull_refresh_list.getLoadingLayoutProxy().setLastUpdatedLabel(label); }}
MyAsyncTask中
package com.bw.asynctask;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import android.R.integer;import android.content.Context;import android.os.AsyncTask;import android.util.Xml;import com.bw.adapter.MyAdapter;import com.bw.bean.Books;import com.handmark.pulltorefresh.library.PullToRefreshListView;public class MyAsyncTask extends AsyncTask<String, integer, List<Books>> { private Context context; private PullToRefreshListView pull_refresh_list; private List<Books> list; public MyAsyncTask(Context context, PullToRefreshListView pull_refresh_list) { super(); this.context = context; this.pull_refresh_list = pull_refresh_list; } @Override protected List<Books> doInBackground(String... params) { try { //获取URL URL url = new URL(params[0]); //获取HttpURLConnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置请求超时时间 conn.setConnectTimeout(10000); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("GET"); conn.connect(); if(conn.getResponseCode() == 200){ InputStream is = conn.getInputStream(); pullParserXML(is); return list; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(List<Books> result) { super.onPostExecute(result); pull_refresh_list.setAdapter(new MyAdapter(context,list)); } private void pullParserXML(InputStream is) { try { XmlPullParser parser = Xml.newPullParser(); parser.setInput(is, "utf-8"); int event = parser.getEventType(); boolean flag = true; String tagname = ""; Books book = null; while(flag){ switch (event) { case XmlPullParser.START_DOCUMENT: list = new ArrayList<Books>(); break; case XmlPullParser.START_TAG: tagname = parser.getName(); if(tagname.equals("item")){ book = new Books(); } break; case XmlPullParser.TEXT: if(tagname.equals("title")){ book.setTitle(parser.getText()); }else if(tagname.equals("sub1")){ book.setSub1(parser.getText()); }else if(tagname.equals("img")){ book.setImg(parser.getText()); }else if(tagname.equals("reading")){ book.setReading(parser.getText()); }else if(tagname.equals("bytime")){ book.setBytime(parser.getText()); } break; case XmlPullParser.END_TAG: tagname = parser.getName(); if(tagname.equals("item")){ list.add(book); } tagname = ""; break; case XmlPullParser.END_DOCUMENT: flag = false; break; } event = parser.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
0 0
- xml中引用PullToRefreshListView
- PullToRefreshListView xml 写法
- PullToRefreshListView的xml布局
- PullToRefreshListView中如何addHeaderView
- 在html中引用XML
- Android中引用XML文件
- xml中引用自定义schema
- PullToRefreshListView
- PullToRefreshListView
- PullToRefreshListView
- PullToRefreshListView
- PullToRefreshListView
- PullToRefreshListView
- PullToRefreshListView
- PullToRefreshListView
- PullToRefreshListView
- PullToRefreshListView
- PullToRefreshListView
- 非常全面的 Android Bitmap 知识点梳理
- cPanel下网站的备份
- 线程同步-生产者消费者问题
- Shell脚本学习笔记-函数
- maven分模块,聚合 在实际项目里面的应用
- xml中引用PullToRefreshListView
- 递归和动态规划-汉诺塔II
- VMware安装MacOS虚拟机无法从光盘启动
- Tomcat部署项目(JEECG)时报错处理
- Ubuntu14.04交叉编译Linux内核
- linux基本命令(17)——whereis命令
- 输入一个集合,输出这个集合的所有子集
- 华为2016机试题目01
- 迭代器模式