Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 下拉刷新上拉加載效果功能,使用開源項目android-pulltorefresh實現

Android 下拉刷新上拉加載效果功能,使用開源項目android-pulltorefresh實現

編輯:關於Android編程

應用場景:

在App開發中,對於信息的獲取與演示,不可能全部將其獲取與演示,為了在用戶使用中,給予用戶以友好、方便的用戶體驗,以滑動、下拉的效果動態加載數據的要求就會出現。為此,該效果功能就需要應用到所需要的展示頁面中。

知識點介紹:

本文主要根據開源項目android-pulltorefresh展開介紹。
android-pulltorefresh
【一個強大的拉動刷新開源項目,支持各種控件下拉刷新 ListView、ViewPager、WevView、ExpandableListView、GridView、(Horizontal )ScrollView、Fragment上下左右拉動刷新,比下面johannilsson那個只支持ListView的強大的多。並且他實現的下拉刷新ListView在item不足一屏情況下也不會顯示刷新提示,體驗更好。】

第一步:新建Android工程SampleDemo
第二步:在res/values下新建attrs.xml
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 "PullToRefresh"> "pullDownFromTop"value="0x1"> "pullUpFromBottom"value="0x2"> "both"value="0x3"> srings.xml "app_name">SampleDemo "action_settings">Settings "pull_to_refresh_pull_down_label">滑動刷新 "pull_to_refresh_release_label">釋放刷新 "pull_to_refresh_refreshing_label">加載中 "pull_to_refresh_tap_label">點擊刷新 第三步:將所需要的圖片文件放入相應的文件夾下面,所用的圖片文件有: \
第四步:
1、導入或將開源項目android-pulltorefresh中需要的類文件(.java),加入到自己的項目中的指定包內。
該演示用例涉及的類文件為:
【library src com.handmark.pulltorefresh.library】
PullToRefreshAdapterViewBase.java
PullToRefreshBase.java
PullToRefreshListView.java
【library src com.handmark.pull.torefresh.library.internal】
EmptyViewMethodAccessor.java
LoadingLayout.java
2、構建自己所需要的類文件(.java)。
【PullTask.java】
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 importjava.util.LinkedList; importcom.example.sampledemo.view.PullToRefreshListView; importandroid.os.AsyncTask; importandroid.widget.BaseAdapter; publicclassPullTask extendsAsyncTask<void, string="">{ privatePullToRefreshListView pullToRefreshListView; //實現下拉刷新與上拉加載的ListView privateintpullState; //記錄判斷,上拉與下拉動作 privateBaseAdapter baseAdapter; //ListView適配器,用於提醒ListView數據已經更新 privateLinkedList linkedList; publicPullTask(PullToRefreshListView pullToRefreshListView, intpullState, BaseAdapter baseAdapter, LinkedList linkedList) { this.pullToRefreshListView = pullToRefreshListView; this.pullState = pullState; this.baseAdapter = baseAdapter; this.linkedList = linkedList; } @Override protectedString doInBackground(Void... params) { try{ Thread.sleep(1000); }catch(InterruptedException e) { } returnStringTest; } @Override protectedvoidonPostExecute(String result) { if(pullState == 1) {//name=pullDownFromTop value=0x1 下拉 linkedList.addFirst(頂部數據); } if(pullState == 2) {//name=pullUpFromBottom value=0x2 上拉 linkedList.addLast(底部數據); } baseAdapter.notifyDataSetChanged(); pullToRefreshListView.onRefreshComplete(); super.onPostExecute(result); } }void,>
【PullAdapter.java】
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 importjava.util.LinkedList; importcom.example.sampledemo.R; importandroid.content.Context; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.BaseAdapter; importandroid.widget.TextView; publicclassPullAdapter extendsBaseAdapter { privateLinkedList linkedList; privateLayoutInflater mInflater; publicPullAdapter(LinkedList linkedList, Context context) { mInflater = LayoutInflater.from(context); this.linkedList = linkedList; } @Override publicintgetCount() { returnlinkedList.size(); } @Override publicObject getItem(intposition) { returnlinkedList.get(position); } @Override publiclonggetItemId(intposition) { returnposition; } @Override publicView getView(intposition, View convertView, ViewGroup parent) { ViewHolder holder=null; if(convertView == null) { holder = newViewHolder(); convertView = mInflater.inflate(R.layout.layout_main_listitem, null); holder.textView = (TextView) convertView.findViewById(R.id.textView); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } if(linkedList.size()>0){ finalString dataStr = linkedList.get(position); holder.textView.setText(dataStr); } returnconvertView; } privatestaticclass ViewHolder { TextView textView; //數據顯示區域 } }
第四步:為PullAdapter.java 設計布局文件layout_main_listitem.xml
? 1 2 3 4 "#FFFFFF"android:layout_height="match_parent"android:layout_width="match_parent"android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android"> "left"android:id="@+id/textView"android:layout_height="wrap_content"android:layout_margintop="4dp"android:layout_width="match_parent"android:textcolor="#99CC66"android:textsize="18dp">
滑動時出現提醒布局文件pull_to_refresh_header.xml
? 1 2 3 4 5 6 "fill_parent"android:layout_width="fill_parent"android:paddingbottom="10dip"android:paddingtop="10dp"xmlns:android="http://schemas.android.com/apk/res/android"> "@+id/pull_to_refresh_text"android:layout_centerinparent="true"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="@string/pull_to_refresh_pull_down_label"android:textappearance="?android:attr/textAppearanceMedium"android:textstyle="bold"> "@+id/pull_to_refresh_progress"android:indeterminate="true"android:layout_centervertical="true"android:layout_height="wrap_content"android:layout_marginleft="30dip"android:layout_marginright="20dip"android:layout_width="wrap_content"android:visibility="gone"style="?android:attr/progressBarStyleSmall"> "@+id/pull_to_refresh_image"android:layout_centervertical="true"android:layout_height="wrap_content"android:layout_marginleft="30dip"android:layout_marginright="20dip"android:layout_width="wrap_content">
MainActivity.java 主布局文件activity_main.xml
? 1 2 3 4 "#FFFFFF"android:layout_height="match_parent"android:layout_width="match_parent"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:cp="http://schemas.android.com/apk/res/com.example.sampledemo"xmlns:tools="http://schemas.android.com/tools"> "#FFFFFF"android:cachecolorhint="#00000000"android:divider="@android:color/black"android:dividerheight="0.1dip"android:id="@+id/pullrefresh"android:layout_height="fill_parent"android:layout_width="fill_parent"cp:mode="both">
第五步:編寫MainActivity.java ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 importjava.util.Arrays; importjava.util.LinkedList; importcom.example.sampledemo.view.PullToRefreshBase.OnRefreshListener; importcom.example.sampledemo.view.PullToRefreshListView; importcom.example.sampledemo.view.adapter.PullAdapter; importcom.example.sampledemo.view.task.PullTask; importandroid.os.Bundle; importandroid.widget.ArrayAdapter; importandroid.widget.ListView; importandroid.app.Activity; /** * @ClassName MainActivity.java * @Author MaHaochen * @Date 2014-4-30 15:56:47 */ publicclassMainActivity extendsActivity { privateLinkedList mListItems; privatePullToRefreshListView mPullRefreshListView; privateArrayAdapter mAdapter; privateListView mListView; privatePullAdapter pullAdapter; privateString[] mStrings = { 初始數據01,初始數據02,初始數據03,初始數據04,初始數據05 }; @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } privatevoidinitViews() { mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pullrefresh); mPullRefreshListView.setOnRefreshListener(mOnrefreshListener); mListView = mPullRefreshListView.getRefreshableView(); mListItems = newLinkedList(); mListItems.addAll(Arrays.asList(mStrings)); pullAdapter = newPullAdapter(mListItems, MainActivity.this); mListView.setAdapter(pullAdapter); } OnRefreshListener mOnrefreshListener = newOnRefreshListener() { publicvoidonRefresh() { PullTask pullTask = newPullTask(mPullRefreshListView, mPullRefreshListView.getRefreshType(), pullAdapter, mListItems); pullTask.execute(); } }; }

下載地址:http://download.csdn.net/detail/fngy123/7611567

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