前段时间,在使用ListView的过程中突然发现PullToRefresh的使用方法,用于上拉下拉刷新使用,觉得该方法很好使用,特此分享,供大家参考。
由于PullToRefresh方法是开源的第三方插件使用,所以使用步骤如下:
1、在GitHub上面下载源码以及需使用的包,地址为https://github.com/chrisbanes/Android-PullToRefresh,将library文件夹引用至自己的工作项目。(再此不做详细的导入说明)
2、导入完成之后,就可以开始编写自己的xml文件,具体代码如下。
注意:为了使用PullToRefresh一些属性,需要引命名空间,因此
xmlns:ptr = "http://schemas.android.com/apk/res-auto" ptr:ptrMode="both"
该两行代码不能省略。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/bg" tools:context=".MainActivity"> <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:dividerHeight="1dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" android:focusableInTouchMode="true" android:focusable="true" ptr:ptrMode="both"/> </LinearLayout>
3、编写java文件,具体代码如下。
package pulltorefresh.pulltorefresh; import android.app.Activity; import android.content.pm.ActivityInfo; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.Arrays; import java.util.LinkedList; /** * PullToRefresh * write by jimmy.li * 2016/4/9 22:58 */ public class MainActivity extends Activity { private LinkedList<String> mListItems; private PullToRefreshListView mPullRefreshListView; private ArrayAdapter<String> mAdapter; private int mDowmAddNum = 1; private int mUpAddNum = 1; //每次刷新增加5个listItem private int mAddFalg = 5; //数据源 private String[] mStrings = {"Jimmy1", "Jimmy2", "Jimmy3", "Jimmy4", "Jimmy5", "Jimmy6", "Jimmy7", "Jimmy8", "Jimmy9", "Jimmy10", "Jimmy11", "Jimmy12", "Jimmy13", "Jimmy14", "Jimmy15", "Jimmy16", "Jimmy17", "Jimmy18", "Jimmy19", "Jimmy20", "Jimmy21", "Jimmy22", "Jimmy23", "Jimmy24", "Jimmy25", "Jimmy26"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); initUI(); initData(); } private void initData() { /** * 实现 接口 OnRefreshListener2<ListView> 以便与监听 滚动条到顶部和到底部 */ mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() { //onPullDownToRefresh @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { Toast.makeText(MainActivity.this, "onPullDownToRefresh", Toast.LENGTH_SHORT).show(); for (int i = 1; i <= mAddFalg; i++) { mListItems.addFirst("Added Later refresh..." + mDowmAddNum); mDowmAddNum++; } new GetDataTask().execute(); } //onPullUpToRefresh @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { Toast.makeText(MainActivity.this, "onPullUpToRefresh", Toast.LENGTH_SHORT).show(); for (int i = 1; i <= mAddFalg; i++) { mListItems.addLast("Added after refresh..." + mUpAddNum); mUpAddNum++; } new GetDataTask().execute(); } }); //获取控件并注册 ListView actualListView = mPullRefreshListView.getRefreshableView(); registerForContextMenu(actualListView); mListItems = new LinkedList<String>(); mListItems.addAll(Arrays.asList(mStrings)); //设置适配器 mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); actualListView.setAdapter(mAdapter); } private void initUI() { mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); } //模拟网络加载数据的 异步请求类 // private class GetDataTask extends AsyncTask<Void, Void, String[]> { //子线程请求数据 @Override protected String[] doInBackground(Void... params) { // Simulates a background job. try { Thread.sleep(1000); } catch (InterruptedException e) { } return mStrings; } //主线程更新UI @Override protected void onPostExecute(String[] result) { mAdapter.notifyDataSetChanged(); // Call onRefreshComplete when the list has been refreshed. mPullRefreshListView.onRefreshComplete(); super.onPostExecute(result); } } }
4、主要代码讲解。
private void initData() { /** * 实现 接口 OnRefreshListener2<ListView> 以便与监听 滚动条到顶部和到底部 */ mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() { //onPullDownToRefresh @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { Toast.makeText(MainActivity.this, "onPullDownToRefresh", Toast.LENGTH_SHORT).show(); for (int i = 1; i <= mAddFalg; i++) { mListItems.addFirst("Added Later refresh..." + mDowmAddNum); mDowmAddNum++; } new GetDataTask().execute(); } //onPullUpToRefresh @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { Toast.makeText(MainActivity.this, "onPullUpToRefresh", Toast.LENGTH_SHORT).show(); for (int i = 1; i <= mAddFalg; i++) { mListItems.addLast("Added after refresh..." + mUpAddNum); mUpAddNum++; } new GetDataTask().execute(); } });
上面主要是利用initData()函数将PullToRefresh上拉下拉刷新的主要代码封装起来,分别对应其下拉和上拉事件监听器。
//获取控件并注册 ListView actualListView = mPullRefreshListView.getRefreshableView(); registerForContextMenu(actualListView); mListItems = new LinkedList<String>(); mListItems.addAll(Arrays.asList(mStrings)); //设置适配器 mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); actualListView.setAdapter(mAdapter);
Good luck!
Write by Jimmy.li