这里我是封装了一个上下拉的library,单纯的看的话不太懂,可以直接下载了自己去了解一下
http://download.csdn.net/detail/androidhaikuan/9691593
然后就是recyclerview的点击和长按监听,我是在适配器中做了个接口,然后通过接口回调实现的:
http://blog.csdn.net/huamnge/article/details/53691346
首先要导入pulltorefresh的library
然后是main.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
//调用pulltorefreshi中的listview
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
在mainactivity中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview= (PullToRefreshListView) findViewById(R.id.listview);
//
设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */
listview.setMode(PullToRefreshBase.Mode.BOTH);
listview.setMode(PullToRefreshBase.Mode.BOTH);
//
上下拉的监听
listview.setOnRefreshListener(this);
getXml();
}
listview.setOnRefreshListener(this);
getXml();
}
//这是网络请求
public void getXml() {
new Thread(){
@Override
public void run() {
super.run();
try {
URL url=new URL("http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=\"+ye+\"&pageSize=20");
HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
int code=urlConnection.getResponseCode();
if (code==200) {
InputStream inputStream=urlConnection.getInputStream();
jieXml(inputStream);
new Thread(){
@Override
public void run() {
super.run();
try {
URL url=new URL("http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=\"+ye+\"&pageSize=20");
HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
int code=urlConnection.getResponseCode();
if (code==200) {
InputStream inputStream=urlConnection.getInputStream();
jieXml(inputStream);
//发送到handler进行解析(我这里就不写了)
handler.sendEmptyMessage(0);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
这个是下拉刷新
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ye=1;
list.clear();
getXml();
}
},1000);
myadapter.notifyDataSetChanged();
new FinishRefreshed().execute();
}
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ye=1;
list.clear();
getXml();
}
},1000);
myadapter.notifyDataSetChanged();
new FinishRefreshed().execute();
}
这个是上拉加载
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ye++;
getXml();
}
},1000);
new FinishRefreshed().execute();
}
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ye++;
getXml();
}
},1000);
new FinishRefreshed().execute();
}
内部类,写了上拉和下拉的时间
private class FinishRefreshed extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... params) {
try {
@Override
protected Void doInBackground(Void... params) {
try {
//两秒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
protected void onPostExecute(Void aVoid) {
//刷新listview
listview.onRefreshComplete();
}
}
}
listview.onRefreshComplete();
}
}
}