下拉的效果图
activity_PullToRefresh.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ptrlv_refresh"></com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
PullToRefreshActivity.java
package com.example.app13;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2017/6/9.
*/
public class PullToRefreshActivity extends AppCompatActivity{
private PullToRefreshListView ptrlv_refresh;
private List<String> list=new ArrayList<>();
private ArrayAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pulltorefresh);
ptrlv_refresh= (PullToRefreshListView) findViewById(R.id.ptrlv_refresh);
for (int i = 0; i < 10; i++) {
list.add("萌萌"+i+"次");
}
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
ptrlv_refresh.setAdapter(adapter);
//给ListView设置下拉刷新的监听
ptrlv_refresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
Toast.makeText(PullToRefreshActivity.this,"下拉",Toast.LENGTH_SHORT).show();
new MyTask().execute();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
}
});
}
@RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
class MyTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
for (int i = 0; i < 3; i++) {
//下拉后会添加妮妮3次
list.add(0,"妞妞"+i+"次");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
ptrlv_refresh.onRefreshComplete();
//通知适配器数据发生改变
adapter.notifyDataSetChanged();
}
}
如果你没有PullToRefresh下拉刷新的控件你可以到第三方去下载:http://github.com