Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android下拉刷新,上拉獲取更多

android下拉刷新,上拉獲取更多

編輯:關於Android編程

很經典的功能,可以嘗試去寫寫,項目趕的很,就借用了別人的。github上搜索的,選擇了 chrisbanes/Android-PullToRefresh 效果實現了,在此記錄下。 1、配置環境,這個比較簡單,下載後作為一個library直接導入; 2、實現一個下拉刷新和上拉獲取更多,(借鑒自帶的一個listview): MainActivity.java  
package com.ttdevs.pullrefresh;  
  
import java.util.Arrays;  
import java.util.LinkedList;  
  
import android.app.ListActivity;  
import android.os.AsyncTask;  
import android.os.Bundle;  
import android.widget.ArrayAdapter;  
import android.widget.ListView;  
import android.widget.Toast;  
  
import com.handmark.pulltorefresh.library.PullToRefreshBase;  
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;  
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;  
import com.handmark.pulltorefresh.library.PullToRefreshListView;  
  
public class MainActivity extends ListActivity {  
  
    private PullToRefreshListView mPullRefreshListView;  
    private LinkedList<String> mListItems;  
    private ArrayAdapter<String> mAdapter;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);  
        mPullRefreshListView.setMode(Mode.BOTH);  
        mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {  
  
            @Override  
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {  
                Toast.makeText(getApplicationContext(), "下拉刷新", Toast.LENGTH_LONG).show();  
                new GetDataTask().execute();  
            }  
  
            @Override  
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {  
                Toast.makeText(getApplicationContext(), "上拉獲取更多", Toast.LENGTH_LONG).show();  
                new GetDataTask().execute();  
            }  
          
        });  
  
        ListView actualListView = mPullRefreshListView.getRefreshableView();  
  
        // Need to use the Actual ListView when registering for Context Menu  
        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 class GetDataTask extends AsyncTask<Void, Void, String[]> {  
  
        @Override  
        protected String[] doInBackground(Void... params) {  
            try {  
                Thread.sleep(1000);  
            } catch (InterruptedException e) {  
            }  
            return mStrings;  
        }  
  
        @Override  
        protected void onPostExecute(String[] result) {  
            mListItems.addFirst("Added after refresh...");  
            mAdapter.notifyDataSetChanged();  
  
            mPullRefreshListView.onRefreshComplete();  
  
            super.onPostExecute(result);  
        }  
    }  
  
    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" };  
}  

activity_main.xml

 

 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_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="match_parent"  
        android:layout_height="match_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>  

 


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