Android PullToRefresh使用详解(五)--下拉刷新的ScrollView

来源:互联网 发布:化工设备画图软件 编辑:程序博客网 时间:2024/05/22 02:03

转自:http://blog.csdn.net/harvic880925/article/details/24722657

前言:时隔三月,再次用到PullToRefresh,但这次与上次不同,用到的是ScrollView的下拉刷新,网上资料甚少,唯有看源码,还好难度不大,一下午时间,攻克难题,分享给大家。

 以往相关文章

与本篇相关的只有第一篇的实例加载过程,其它内容都是针对ListView的下拉刷新讲解的。

PullToRefresh使用详解(四)--利用回调函数实现到底加载

PullToRefresh使用详解(三)--实现异步加载的下拉刷新列表

PullToRefresh使用详解(二)---重写BaseAdapter实现复杂XML下拉刷新

 《PullToRefresh使用详解(一)--构建下拉刷新的listView

 

 本篇效果图:

             下拉前                                               下拉中                                       下拉后

      

 效果讲解:

1、本例仅讲解上拉加载,对于其它加载内容,参考pullToRefresh源码,在PullToRefreshListActivity.java中写的很详细;

2、上拉加载前,有一个XML布局,下拉刷新后,添加一个与原有布局完全相同的动态布局,只是TextView的内容不一样了而已。

 一、下拉前

 1、导入Library库,至于导入Library库的过程,参考PullToRefresh使用详解(一)--构建下拉刷新的listView》。

 2、XML构建:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <!-- The PullToRefreshScrollView replaces a standard PullToRefreshScrollView widget. -->  
  9.   
  10.     <com.handmark.pulltorefresh.library.PullToRefreshScrollView  
  11.         xmlns:ptr="http://schemas.android.com/apk/res-auto"  
  12.         android:id="@+id/pull_refresh_scrollview"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="fill_parent"  
  15.         ptr:ptrAnimationStyle="flip"  
  16.         ptr:ptrMode="both" >  
  17.   
  18.         <LinearLayout  
  19.             android:id="@+id/sub_root_lin"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:orientation="vertical" >  
  23.   
  24.             <!-- 动态创建布局开始 -->  
  25.             <LinearLayout  
  26.                 android:layout_width="fill_parent"  
  27.                 android:layout_height="wrap_content"  
  28.                 android:orientation="vertical" >  
  29.   
  30.                 <RelativeLayout  
  31.                     android:layout_width="fill_parent"  
  32.                     android:layout_height="wrap_content" >  
  33.   
  34.                     <ImageView  
  35.                         android:layout_width="50dip"  
  36.                         android:layout_height="50dip"  
  37.                         android:layout_alignParentRight="true"  
  38.                         android:clickable="true"  
  39.                         android:padding="5dip"  
  40.                         android:src="@drawable/list_item_detail_part_navi_edit" />  
  41.   
  42.                     <TextView  
  43.                         android:layout_width="fill_parent"  
  44.                         android:layout_height="50dip"  
  45.                         android:gravity="center_horizontal|center_vertical"  
  46.                         android:text="(二)"  
  47.                         android:textSize="20sp"  
  48.                         android:textStyle="bold" />  
  49.                 </RelativeLayout>  
  50.   
  51.                 <TextView  
  52.                     android:layout_width="fill_parent"  
  53.                     android:layout_height="match_parent"  
  54.                     android:text="@string/changHenGe"  
  55.                     android:textSize="20sp" />  
  56.             </LinearLayout>  
  57.             <!-- 动态创建布局结束 -->  
  58.               
  59.         </LinearLayout>  
  60.           
  61.     </com.handmark.pulltorefresh.library.PullToRefreshScrollView>  
  62.   
  63. </LinearLayout>  

这段代码实现的效果就如“下拉前”这张图片显示的一样。

注意两点:

1、利用<com.handmark.pulltorefresh.library.PullToRefreshScrollView取代原有的ScrollView应在的位置即可,其它想怎么布局怎么布局。
2、在<!--动态创建布局-->标注起来的部分,这段代码要格外注意,因为我们在代码中,会创建一个完全相同的动态布局,插入到它上面。

2、JAVA代码 

 完整代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.try_pulltorefresh_listview;  
  2. /** 
  3.  * @author harvic 
  4.  * @date 2014-4-29 
  5.  * @address http://blog.csdn.net/harvic880925 
  6.  */  
  7. import com.handmark.pulltorefresh.library.PullToRefreshBase;  
  8. import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;  
  9. import com.handmark.pulltorefresh.library.PullToRefreshScrollView;  
  10. import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;  
  11.   
  12. import android.os.AsyncTask;  
  13. import android.os.Bundle;  
  14. import android.app.Activity;  
  15. import android.graphics.Typeface;  
  16. import android.util.Log;  
  17. import android.view.Gravity;  
  18. import android.widget.ImageView;  
  19. import android.widget.LinearLayout;  
  20. import android.widget.RelativeLayout;  
  21. import android.widget.ScrollView;  
  22. import android.widget.TextView;  
  23.   
  24. public class MainActivity extends Activity {  
  25.     PullToRefreshScrollView mPullRefreshScrollView;  
  26.     ScrollView mScrollView;  
  27.   
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.activity_main);  
  32.           
  33.         //这几个刷新Label的设置  
  34.         mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview);  
  35.         mPullRefreshScrollView.getLoadingLayoutProxy().setLastUpdatedLabel("lastUpdateLabel");   
  36.         mPullRefreshScrollView.getLoadingLayoutProxy().setPullLabel("PULLLABLE");  
  37.         mPullRefreshScrollView.getLoadingLayoutProxy().setRefreshingLabel("refreshingLabel");  
  38.         mPullRefreshScrollView.getLoadingLayoutProxy().setReleaseLabel("releaseLabel");  
  39.           
  40.         //上拉、下拉设定  
  41.         mPullRefreshScrollView.setMode(Mode.PULL_FROM_START);  
  42.           
  43.         //上拉监听函数  
  44.         mPullRefreshScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {  
  45.               
  46.             @Override  
  47.             public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {  
  48.                 //执行刷新函数  
  49.                 new GetDataTask().execute();  
  50.             }  
  51.         });  
  52.   
  53.         //获取ScrollView布局,此文中用不到  
  54.         mScrollView = mPullRefreshScrollView.getRefreshableView();  
  55.     }  
  56.   
  57.     private class GetDataTask extends AsyncTask<Void, Void, LinearLayout> {  
  58.   
  59.         @Override  
  60.         protected LinearLayout doInBackground(Void... params) {  
  61.             // Simulates a background job.  
  62.             try {  
  63.                 Thread.sleep(4000);  
  64.                 LinearLayout lin=viewSingleItem();  
  65.                 return lin;  
  66.             } catch (InterruptedException e) {  
  67.                 Log.e("msg","GetDataTask:" + e.getMessage());  
  68.             }  
  69.             return null;  
  70.         }  
  71.   
  72.         @Override  
  73.         protected void onPostExecute(LinearLayout result) {  
  74.             // Do some stuff here  
  75.   
  76.             LinearLayout sub_root_lin=(LinearLayout) findViewById(R.id.sub_root_lin);  
  77.               
  78.             LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  79.                     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  80.             sub_root_lin.addView(result, 0, LP_FW);  
  81.               
  82.             mPullRefreshScrollView.setMode(Mode.DISABLED);  
  83.               
  84.             // Call onRefreshComplete when the list has been refreshed.  
  85.             //在更新UI后,无需其它Refresh操作,系统会自己加载新的listView  
  86.             mPullRefreshScrollView.onRefreshComplete();  
  87.               
  88.               
  89.             super.onPostExecute(result);  
  90.         }  
  91.     }  
  92.       
  93.     /** 
  94.      * 下拉刷新后,动态产生的一条布局 
  95.      * @return 
  96.      */  
  97.     private LinearLayout viewSingleItem()  
  98.     {  
  99.         LinearLayout layout_root_lin=new LinearLayout(this);  
  100.         layout_root_lin.setOrientation(LinearLayout.VERTICAL);  
  101.           
  102.         //添加第一个子布局集  
  103.         RelativeLayout layout_sub_relative=new RelativeLayout(this);  
  104.           
  105.         ImageView relative_sub_IV=new ImageView(this);  
  106.         relative_sub_IV.setPadding(5555);  
  107.         relative_sub_IV.setClickable(true);  
  108.         relative_sub_IV.setImageResource(R.drawable.list_item_detail_part_navi_edit);  
  109.         RelativeLayout.LayoutParams RL_IM = new RelativeLayout.LayoutParams(50,50);//尤其注意这个位置,用的是父容器的布局参数  
  110.         RL_IM.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);//这里要注意设置方法!!!---靠父容器右侧对齐  
  111.         layout_sub_relative.addView(relative_sub_IV, RL_IM);  
  112.           
  113.         TextView relative_sub_TV=new TextView(this);  
  114.         relative_sub_TV.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);  
  115.         relative_sub_TV.setText("(一)");  
  116.         relative_sub_TV.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));  
  117.         relative_sub_TV.setTextSize(20);  
  118.         RelativeLayout.LayoutParams RL_TV = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,50);  
  119.         layout_sub_relative.addView(relative_sub_TV, RL_TV);  
  120.           
  121.         LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  122.                 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  123.         layout_root_lin.addView(layout_sub_relative, LP_FW);  
  124.           
  125.         //添加第二个子布局  
  126.         TextView lin_sub_TV=new TextView(this);  
  127.         lin_sub_TV.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);  
  128.         lin_sub_TV.setText("四月十七,正是去年今日,别君时。忍泪佯低面,含羞半敛眉。" +  
  129.                            "不知魂已断,空有梦相随。除却天边月,没人知。");  
  130.         lin_sub_TV.setTextSize(20);  
  131.         layout_root_lin.addView(lin_sub_TV, LP_FW);  
  132.           
  133.         return layout_root_lin;  
  134.           
  135.           
  136.     }  
  137. }  

讲解:
 1、最长的一个函数:viewSingleItem()

这个函数的功能,就是动态创建一个上面我们所讲的<!-- 动态创建布局 -->之间的布局,并返回根结点,对于动态布局的创建参考《动态添加综合布局---动态添加控件及将某XML动态加入到Activity显示(续)》和《动态添加控件及将某XML动态加入到Activity显示》。

2、然后从上往下看一:刷新标签设定

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview);  
  2. mPullRefreshScrollView.getLoadingLayoutProxy().setLastUpdatedLabel("lastUpdateLabel");   
  3. mPullRefreshScrollView.getLoadingLayoutProxy().setPullLabel("PULLLABLE");  
  4. mPullRefreshScrollView.getLoadingLayoutProxy().setRefreshingLabel("refreshingLabel");  
  5. mPullRefreshScrollView.getLoadingLayoutProxy().setReleaseLabel("releaseLabel");  
这几行代码就是设置下拉刷新时那些标签所显示的内容,自定义标签内容的方法,自己试试改几个,看看效果就知道哪个函数对应哪个标签了。我这里列出了所有的标签,供大家理解清楚各自功能。

 3、设置下拉、上拉、上下拉刷新模式

这里是上拉刷新

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. mPullRefreshScrollView.setMode(Mode.PULL_FROM_START);  
4、设置上拉刷新监听函数

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. mPullRefreshScrollView.setOnRefreshListener(new OnRefreshListener<ScrollView>() {  
  2.       
  3.     @Override  
  4.     public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {  
  5.         //执行刷新函数  
  6.         new GetDataTask().execute();  
  7.     }  
  8. });  
对于真正执行的部分在GetDataTask()函数,下面是它的实现源码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private class GetDataTask extends AsyncTask<Void, Void, LinearLayout> {  
  2.     @Override  
  3.     protected LinearLayout doInBackground(Void... params) {  
  4.         // Simulates a background job.  
  5.         try {  
  6.             Thread.sleep(4000);  
  7.             LinearLayout lin=viewSingleItem();  
  8.             return lin;  
  9.         } catch (InterruptedException e) {  
  10.             Log.e("msg","GetDataTask:" + e.getMessage());  
  11.         }  
  12.         return null;  
  13.     }  
  14.   
  15.     @Override  
  16.     protected void onPostExecute(LinearLayout result) {  
  17.         // Do some stuff here  
  18.   
  19.         LinearLayout sub_root_lin=(LinearLayout) findViewById(R.id.sub_root_lin);  
  20.         LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  21.                 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  22.         sub_root_lin.addView(result, 0, LP_FW);  
  23.           
  24.         mPullRefreshScrollView.setMode(Mode.DISABLED);  
  25.           
  26.         // Call onRefreshComplete when the list has been refreshed.  
  27.         //在更新UI后,无需其它Refresh操作,系统会自己加载新的listView  
  28.         mPullRefreshScrollView.onRefreshComplete();  
  29.         super.onPostExecute(result);  
  30.     }  
  31. }  
首先为了实现异步加载,派生自AsyncTask,对于AsyncTask的具体解释,后面应该会再开篇具体讲述,这里先粗略的说下:

 1、doInBackground()中产生一条动态布局,并作为最终结果返回;

 2、onPostExecute()接收到这条动态布局结果,添加到XML中并显示,注意这里并没有更新XML view所使用的setContentView(),而是程序自动给加载了。最后利用SetMode取消下拉刷新。

 

 源码下载地址:http://download.csdn.net/detail/harvic880925/7271493,不要分,仅供分享

 

请大家尊重作者原创版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/24722657 谢谢!


4 0
原创粉丝点击