ListView下拉刷新框架使用

在github上下载类库带入项目就可以使用,这里就不详细说明下载步骤。现在直接看看ListView的下拉刷新的使用示例。
布局文件

<?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
        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" />
</LinearLayout>

MainActivity代码

package com.example.listview;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.State;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.extras.SoundPullEventListener;

import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/*
 * ListView下拉刷新可以设置声音、模式、是否允许滚动;
 * 更新数据需要使用一个异步类来加载数据并调用executed()方法执行异步类;
 */
public class MainActivity extends ListActivity {
    /*
     * 菜单项
     */
    static final int MENU_MANUAL_REFRESH = 0;
    static final int MENU_DISABLE_SCROLL = 1;
    static final int MENU_SET_MODE = 2;
    static final int MENU_DEMO = 3;
    private PullToRefreshListView mPullToRefreshListView;
    private LinkedList<String> mData;
    //ListView显示数据
    private String[] mString = new String[] { "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday", "Sunday", "China", "Chinese",
            "Made in China" };
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 取得下拉刷新列表
        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
        mData = new LinkedList<String>();
        for (int i = 0; i < mString.length; i++) {
            mData.add(mString[i]);
        }
        adapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_1, mData);
        // setListAdapter(adapter);
        // ListView listView= mPullToRefreshListView.getRefreshableView();
        mPullToRefreshListView.setAdapter(adapter);
        /**
         *  设置下拉刷新监听事件
         */
        mPullToRefreshListView
                .setOnRefreshListener(new OnRefreshListener<ListView>() {

                    @Override
                    public void onRefresh(
                            PullToRefreshBase<ListView> refreshView) {

                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                                "yyyy-MM-dd HH-mm");
                        String time = simpleDateFormat.format(new Date());
                        String label = time;
                        // 设置上次刷新时间
                        refreshView.getLoadingLayoutProxy()
                                .setLastUpdatedLabel("上次刷新时间:" + label);
                        Toast.makeText(getApplicationContext(), "刷新……", 0)
                                .show();
                        new NewDatas().execute();// 获取新的数据
                    }
                });
        /**
         * 下拉到底部触发的事件
         */
        mPullToRefreshListView
                .setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

                    @Override
                    public void onLastItemVisible() {

                        Toast.makeText(getApplicationContext(), "已经到了底部", 0)
                                .show();
                    }
                });
        /**
         * 添加声音
         */
        SoundPullEventListener<ListView> soundPullEventListener=new SoundPullEventListener<ListView>(MainActivity.this); 
        soundPullEventListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
        soundPullEventListener.addSoundEvent(State.RESET, R.raw.reset_sound);
        soundPullEventListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
        mPullToRefreshListView.setOnPullEventListener(soundPullEventListener);
    }

    /**
     * 加载数据
     */
    private class NewDatas extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return mString;
        }

        @Override
        protected void onPostExecute(String[] result) {
            mData.addFirst("added newly");
            adapter.notifyDataSetChanged();
            mPullToRefreshListView.onRefreshComplete();// 刷新完成
            super.onPostExecute(result);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_MANUAL_REFRESH, 0, "人工刷新");
        menu.add(0, MENU_DISABLE_SCROLL, 1, mPullToRefreshListView
                .isScrollingWhileRefreshingEnabled() ? "允许滚动" : "禁止滚动");
        menu.add(0, MENU_SET_MODE, 0,
                mPullToRefreshListView.getMode() == Mode.BOTH ? "下拉模式" : "两端模式");
        menu.add(0, MENU_DEMO, 0, "示例");
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_MANUAL_REFRESH:
            new NewDatas().execute();
            mPullToRefreshListView.setRefreshing(false);
            break;

        case MENU_DISABLE_SCROLL:
            mPullToRefreshListView
                    .setScrollingWhileRefreshingEnabled(!mPullToRefreshListView
                            .isScrollingWhileRefreshingEnabled());
            break;
        case MENU_SET_MODE:
            mPullToRefreshListView
                    .setMode(mPullToRefreshListView.getMode() == Mode.BOTH ? Mode.PULL_FROM_START
                            : Mode.BOTH);
            break;
        case MENU_DEMO:
            mPullToRefreshListView.demo();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 改变菜单项内容
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        MenuItem setMOde = menu.findItem(MENU_SET_MODE);
        setMOde.setTitle(mPullToRefreshListView.getMode() == Mode.BOTH ? "两端模式"
                : "下拉模式");
        MenuItem srcollMode = menu.findItem(MENU_DISABLE_SCROLL);
        srcollMode.setTitle(mPullToRefreshListView
                .isScrollingWhileRefreshingEnabled() ? "允许滚动" : "禁止滚动");
        return super.onPrepareOptionsMenu(menu);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值