以下亲测可行,希望能帮到你
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="pulltiscroll.mo.com.myapplication.Main2Activity">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/ptlv"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
package pulltiscroll.mo.com.myapplication;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
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;
public class Main2Activity extends AppCompatActivity {
private PullToRefreshListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
lv = (PullToRefreshListView) findViewById(R.id.ptlv);
lv.setMode(PullToRefreshBase.Mode.BOTH);
List<String> ls = new ArrayList<>();
for (int i = 0; i <= 20; i++){
ls.add("");
}
lv.setAdapter(new MyAdapter( ls, Main2Activity.this));
lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
new Thread(){
@Override
public void run() {
super.run();
SystemClock.sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Main2Activity.this, "刷新完成", Toast.LENGTH_SHORT).show();
lv.onRefreshComplete();
}
});
}
}.start();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
new Thread(){
@Override
public void run() {
super.run();
SystemClock.sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Main2Activity.this, "加载完成", Toast.LENGTH_SHORT).show();
lv.onRefreshComplete();
}
});
}
}.start();
}
});
}
}
关于刷新加载无法停止的问题
可以看出在完成监听回调当中开了一个线程,无法停止是因为请求数据时间过短,我们让他睡眠一秒以上就可以实现停止动画