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

        之前在项目中有一个需求是需要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();
	}

 

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

    


 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 24
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值