使用库文件实现ListView的下拉刷新

ListView的下拉刷新有很多种,个人觉得使用官方提供的库文件更加方便和简单。使用方法如下:

1.在android环境下导入下载的库文件。库文件的下载地址:http://download.youkuaiyun.com/download/xuyanhu_jiayou/5037440

2.新建android项目,在xml中代码:
    <RelativeLayout 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"
    tools:context=".MainActivity" >


    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/lv_pull"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.handmark.pulltorefresh.library.PullToRefreshListView>


</RelativeLayout>
3.在activity中:
package com.example.demo;


import java.util.Arrays;
import java.util.LinkedList;


import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends Activity{
private PullToRefreshListView myListView;
private LinkedList<String> mListItems;
 private String[] mStrings = {
           "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
           "Abondance"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (PullToRefreshListView)findViewById(R.id.lv_pull);
mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mListItems);


        myListView.setAdapter(adapter);
        myListView.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.
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return mStrings;
        }


        @Override
        protected void onPostExecute(String[] result) {
            mListItems.addFirst("Added after refresh...");


            // Call onRefreshComplete when the list has been refreshed.
            myListView.onRefreshComplete();


            super.onPostExecute(result);
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值