Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android-PullToRefresh下拉刷新庫基本用法

Android-PullToRefresh下拉刷新庫基本用法

編輯:關於Android編程

PullToRefresh是一套實現非常好的下拉刷新庫,它支持:
ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager
等多種常用的需要刷新的View類型,而且使用起來也十分方便。
 

PullToRefresh基本用法:
1、在布局文件中添加PullToRefresh控件,比如PullToRefreshListView;
2、在Activity中,設置監聽器OnRefreshListener以響應用戶下拉操作;
3、在監聽器的onRefresh()方法中執行數據刷新操作,可以通過AsyncTask來實現;
4、在AsyncTask中獲取到數據後,記得調用onRefreshComplete()方法通知PullToRefresh控件數據已獲取完畢,可以結束刷新操作。

實例:PullToRefreshDemo
運行效果:

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316103467.png

視圖結構

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316103450.jpg


代碼清單:
布局文件:activity_main.xml

 

Java源代碼文件:MainActivity.java

 

package com.rainsong.pulltorefreshdemo;

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

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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

public class MainActivity extends Activity {
    private PullToRefreshListView mPullToRefreshListView;
    private LinkedList mListItems;
    private ArrayAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Set a listener to be invoked when the list should be refreshed.
        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_listview);
        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshBase refreshView) {
                String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                
                // Update the LastUpdatedLabel
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                
                // Do work to refresh the list here.
                new GetDataTask().execute();
            }
        });
        
        ListView actualListView = mPullToRefreshListView.getRefreshableView();
        
        mListItems = new LinkedList();
        mListItems.addAll(Arrays.asList(mStrings));
        
        mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mListItems);
        actualListView.setAdapter(mAdapter);
    }
    
    private class GetDataTask extends AsyncTask {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
            }
            return mStrings;
        }
        @Override
        protected void onPostExecute(String[] result) {
            mListItems.addFirst(Added after refresh...);
            mAdapter.notifyDataSetChanged();
            
            // Call onRefreshComplete when the list has been refreshed.
            mPullToRefreshListView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private String[] mStrings = { John, Michelle, Amy, Kim, Mary,
            David, Sunny, James, Maria, Michael, Sarah, Robert,
            Lily, William, Jessica, Paul, Crystal, Peter,
            Jennifer, George, Rachel, Thomas, Lisa, Daniel, Elizabeth,
            Kevin };
}

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved