之前在项目中有一个需求是需要GridView控件,滚动到底部自动加载。但是呢GridView控件并不提供诸如ListView监听滚动到底部的onScrollListener方法,为了实现这样一个效果,用ListView实现了,但是控件的Item子项书写过于复杂,前前后后的出了好多次的Bug,而且还需要对数据进行特殊处理,用ListView实现了诸如GridView的效果并有滚动到底部加载更多的数据,但是过于复杂的结构及数据有点得不偿失。
博客地址:http://blog.csdn.net/ALoveBtoC
今天就关于用GridView控件来实现滚动到底部加载更多数据一个效果实现的技术讲解:
1、ScrollView中嵌套GridView布局
2、ScrollView滚动到底部监听
3、数据加载,刷新适配器
布局样式:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:background="@android:color/white" >
- <ScrollView
- android:id="@+id/MainGridViewScroll"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:fadingEdge="none"
- android:fillViewport="true"
- android:scrollbars="none" >
- <LinearLayout
- android:id="@+id/MainGridViewScrollLinear"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left|top"
- android:orientation="vertical" >
- <com.sixsix.swordsman.grid.MyGridView
- android:id="@+id/MainGridViewGrid"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:fadingEdge="none"
- android:horizontalSpacing="5dp"
- android:listSelector="#00000000"
- android:numColumns="2"
- android:padding="5dp"
- android:scrollbars="none"
- android:verticalSpacing="5dp" />
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="50dp"
- android:background="#00000000" />
- <LinearLayout
- android:id="@+id/MainGridViewFooterLinear"
- android:layout_width="fill_parent"
- android:layout_height="50dp"
- android:gravity="center"
- android:orientation="horizontal"
- android:padding="10dp" >
- <com.ant.liao.GifView
- android:id="@+id/MainGridViewFooterGif"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:enabled="false" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="5dp"
- android:gravity="center_vertical"
- android:text="正在加载..."
- android:textColor="@android:color/black"
- android:textSize="18sp" />
- </LinearLayout>
- </RelativeLayout>
- </LinearLayout>
- </ScrollView>
- </RelativeLayout>
注:MyGridView是一个兼容ScrollView的GridView,百度
ScrollView监听滚动到底部:
- sv.setOnTouchListener(new OnTouchListener() {
- private int lastY = 0;
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if(event.getAction() == MotionEvent.ACTION_UP){
- lastY = sv.getScrollY();
- if(lastY == (ll.getHeight() - sv.getHeight())){
- LLF.setVisibility(View.VISIBLE);
- addMoreData();
- }
- }
- return false;
- }
- });
注:LLF是加载更多效果体验,它的显隐藏来模拟加载数据过程。
加载更多数据:
- private void addMoreData(){
- new Thread(new Runnable(){
- @Override
- public void run() {
- Bundle b = new Bundle();
- try{
- Thread.sleep(2000);
- b.putBoolean("addMoreData", true);
- }catch(Exception e){}finally{
- Message msg = handler.obtainMessage();
- msg.setData(b);
- handler.sendMessage(msg);
- }
- }}).start();
- }
中间只是贴出来了关键的代码,其实从整个来看,并没有什么难的技术点,就是一个完美的转换来做的。下面贴出效果图: