Android中GridView滚动到底部加载数据终极版

来源:互联网 发布:手机网络制式 编辑:程序博客网 时间:2024/05/18 18:20

  之前在项目中有一个需求是需要GridView控件,滚动到底部自动加载。但是呢GridView控件并不提供诸如ListView监听滚动到底部的onScrollListener方法,为了实现这样一个效果,用ListView实现了,但是控件的Item子项书写过于复杂,前前后后的出了好多次的Bug,而且还需要对数据进行特殊处理,用ListView实现了诸如GridView的效果并有滚动到底部加载更多的数据,但是过于复杂的结构及数据有点得不偿失。

 

      博客地址:http://blog.csdn.net/ALoveBtoC

      

        今天就关于用GridView控件来实现滚动到底部加载更多数据一个效果实现的技术讲解:

        1、ScrollView中嵌套GridView布局

        2、ScrollView滚动到底部监听

        3、数据加载,刷新适配器

 

布局样式:

[java] view plaincopy
  1. <RelativeLayout 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.     tools:context=".MainActivity"  
  6.     android:background="@android:color/white" >  
  7.   
  8.     <ScrollView  
  9.         android:id="@+id/MainGridViewScroll"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:fadingEdge="none"  
  13.         android:fillViewport="true"  
  14.         android:scrollbars="none" >  
  15.   
  16.         <LinearLayout  
  17.             android:id="@+id/MainGridViewScrollLinear"  
  18.             android:layout_width="fill_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:gravity="left|top"  
  21.             android:orientation="vertical" >  
  22.   
  23.             <com.sixsix.swordsman.grid.MyGridView  
  24.                 android:id="@+id/MainGridViewGrid"  
  25.                 android:layout_width="fill_parent"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:fadingEdge="none"  
  28.                 android:horizontalSpacing="5dp"  
  29.                 android:listSelector="#00000000"  
  30.                 android:numColumns="2"  
  31.                 android:padding="5dp"  
  32.                 android:scrollbars="none"  
  33.                 android:verticalSpacing="5dp" />  
  34.   
  35.             <RelativeLayout  
  36.                 android:layout_width="fill_parent"  
  37.                 android:layout_height="wrap_content" >  
  38.   
  39.                 <TextView  
  40.                     android:layout_width="fill_parent"  
  41.                     android:layout_height="50dp"  
  42.                     android:background="#00000000" />  
  43.   
  44.                 <LinearLayout  
  45.                     android:id="@+id/MainGridViewFooterLinear"  
  46.                     android:layout_width="fill_parent"  
  47.                     android:layout_height="50dp"  
  48.                     android:gravity="center"  
  49.                     android:orientation="horizontal"  
  50.                     android:padding="10dp" >  
  51.   
  52.                     <com.ant.liao.GifView  
  53.                         android:id="@+id/MainGridViewFooterGif"  
  54.                         android:layout_width="wrap_content"  
  55.                         android:layout_height="wrap_content"  
  56.                         android:enabled="false" />  
  57.   
  58.                     <TextView  
  59.                         android:layout_width="wrap_content"  
  60.                         android:layout_height="fill_parent"  
  61.                         android:layout_marginLeft="5dp"  
  62.                         android:gravity="center_vertical"  
  63.                         android:text="正在加载..."  
  64.                         android:textColor="@android:color/black"  
  65.                         android:textSize="18sp" />  
  66.                 </LinearLayout>  
  67.             </RelativeLayout>  
  68.         </LinearLayout>  
  69.     </ScrollView>  
  70.   
  71. </RelativeLayout>  


注:MyGridView是一个兼容ScrollView的GridView,百度

 

ScrollView监听滚动到底部:

[java] view plaincopy
  1. sv.setOnTouchListener(new OnTouchListener() {  
  2.               
  3.             private int lastY = 0;  
  4.           
  5.             @Override  
  6.             public boolean onTouch(View v, MotionEvent event) {  
  7.                   
  8.                 if(event.getAction() == MotionEvent.ACTION_UP){  
  9.                     lastY = sv.getScrollY();  
  10.                     if(lastY == (ll.getHeight() - sv.getHeight())){  
  11.                         LLF.setVisibility(View.VISIBLE);  
  12.                         addMoreData();  
  13.                     }  
  14.                 }  
  15.                 return false;  
  16.             }  
  17.         });  


注:LLF是加载更多效果体验,它的显隐藏来模拟加载数据过程。

 

加载更多数据:

[java] view plaincopy
  1. private void addMoreData(){  
  2.         new Thread(new Runnable(){  
  3.   
  4.             @Override  
  5.             public void run() {  
  6.                 Bundle b = new Bundle();  
  7.                 try{  
  8.                     Thread.sleep(2000);  
  9.                     b.putBoolean("addMoreData"true);  
  10.                 }catch(Exception e){}finally{  
  11.                     Message msg = handler.obtainMessage();  
  12.                     msg.setData(b);  
  13.                     handler.sendMessage(msg);  
  14.                 }  
  15.             }}).start();  
  16.     }  

 

中间只是贴出来了关键的代码,其实从整个来看,并没有什么难的技术点,就是一个完美的转换来做的。下面贴出效果图:

    

0 0