PullToRefresh使用详解(一)

另外extras文件夹还有两个工程:PullToRefreshListFragment和PullToRefreshViewPager,由于我们的这个用不到他们的库文件,所以不必导入了;

二、实战1、新建工程,添加Libray库到工程中

新建工程(try_PullToRefresh)后,右键-》Properties-》Android-》Add 选择上面的Library,然后就是这个样子的

2、重写activity_main.xml

XML内容为:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns: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.PullToRefreshListViewandroid: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" /></LinearLayout>

其中中间那一大段<com.handmark.pull………………/>就是相当于ListView控件,用这段来代替原是ListView控件的代码

3、JAVA代码讲解

全部代码:

package com.example.try_pulltorefresh;import java.util.Arrays;import java.util.LinkedList;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import android.os.AsyncTask;import android.os.Bundle;import android.app.Activity;import android.text.format.DateUtils;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity {private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega’l Pitu", "Airag", "Airedale", "Aisy Cendre","Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega’l Pitu", "Airag", "Airedale", "Aisy Cendre","Allgauer Emmentaler" };private LinkedList<String> mListItems;private PullToRefreshListView mPullRefreshListView;private ArrayAdapter<String> mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);// Set a listener to be invoked when the list should be refreshed.mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {@Overridepublic void onRefresh(PullToRefreshBase<ListView> refreshView) {String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);// Update the LastUpdatedLabelrefreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);// Do work to refresh the list here.new GetDataTask().execute();}});mListItems = new LinkedList<String>();mListItems.addAll(Arrays.asList(mStrings));mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);//这两个绑定方法用其一// 方法一// mPullRefreshListView.setAdapter(mAdapter);//方法二ListView actualListView = mPullRefreshListView.getRefreshableView();actualListView.setAdapter(mAdapter);}private class GetDataTask extends AsyncTask<Void, Void, String> {//后台处理部分@Overrideprotected String doInBackground(Void… params) {// Simulates a background job.try {Thread.sleep(1000);} catch (InterruptedException e) {}String str="Added after refresh…I add";return str;}//这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中//根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值@Overrideprotected void onPostExecute(String result) {//在头部增加新添内容mListItems.addFirst(result);//通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合mAdapter.notifyDataSetChanged();// Call onRefreshComplete when the list has been refreshed.mPullRefreshListView.onRefreshComplete();super.onPostExecute(result);}}}如果我们想要更多的玫瑰花,就必须种植更多的玫瑰树。

PullToRefresh使用详解(一)

相关文章:

你感兴趣的文章:

标签云: