本次使用的第三方下拉刷新控件是:Android-Pull-Refresh,下载地址:https://github.com/chrisbanes/Android-PullToRefresh
该控件适用于:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- The PullToRefreshListView replaces a standard ListView widget. -->
<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:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true"
ptr:ptrDrawable="@drawable/ic_launcher"
ptr:ptrMode="both" />
</LinearLayout>import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
/**
* MainActivity---利用第三方下拉刷新控件实现
* @author seabear
*
*/
public class MainActivity extends Activity {
private PullToRefreshListView mPullToRefreshListView;
private List<String> mArrayList = new ArrayList<String>();
private ArrayAdapter<String> mArrayAdapter;
private int mNums = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullToRefreshListView = (PullToRefreshListView)this.findViewById(R.id.pull_refresh_list);
mArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mArrayList);
mPullToRefreshListView.setAdapter(mArrayAdapter);
mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
new GetDataTask().execute();
}
});
}
private class GetDataTask extends AsyncTask<Void, Void, String>
{
@Override
protected String doInBackground(Void... params) {
// Simulates a background job.
mNums++;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return "第" + String.valueOf(mNums) + "行";
}
@Override
protected void onPostExecute(String result) {
mArrayList.add(result);
mArrayAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
mPullToRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/l243225530/article/details/46986097