深入理解ScrollView



        在网上看到一些网页在实现分页加载的时候,能够自动判断是否到达底部,然后自动加载下一页的内容,开始用的是ListView实现的,但是这个效果并不是很好,因为图片比较多出现了有些卡卡的现象,用了另外一种方式去实现了主要是为了对比一下速度的问题,找了很多最后发现可以使用ScrollView,查了很多ScrollView的文档但是没有多少能够使用到的的东西,可能也是水平有限吧,没有仔细的深入看源码,在捕获Y值得时候出现了问题。

对于判断是否停止和是否到达底部的问题,网上的都是使用每个几十个毫秒去判断是否停止(网上可以搜到),通过两次捕获然后对比是否到达了底部。再接着去加载下一页的内容,这种方式会出现的问题就是用户的体验不太好,只有滑动了两次之后才能够加载下一页的内容。后来找到了一种方法可以一次判断是否到达底部。下面我把两种实现方式全部都列出来,主要的一个关键点就是写了一个类继承了scrollView,在这个类中有一个监听scrollView是否变化的方法,在这个监听方法中调用了computeVerticalScrollRange方法。

main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout   
         xmlns:android="http://schemas.android.com/apk/res/android"   
         android:layout_height="wrap_content"  
         android:orientation="vertical" 
         android:paddingBottom="4dip" android:layout_width="fill_parent">  
         <ImageView   
               android:layout_height="wrap_content"   
               android:id="@+id/ItemImage"   
               android:layout_width="wrap_content"   
               android:layout_centerHorizontal="true">   
         </ImageView>  
         <TextView   
               android:layout_width="wrap_content"   
               android:layout_below="@+id/ItemImage"   
               android:layout_height="wrap_content"   
               android:text="TextView01"   
               android:layout_centerHorizontal="true"   
               android:id="@+id/ItemText">  
         </TextView>  
</LinearLayout> 

item.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout   
         xmlns:android="http://schemas.android.com/apk/res/android"   
         android:layout_height="wrap_content"  
         android:orientation="vertical" 
         android:paddingBottom="4dip" android:layout_width="fill_parent">  
         <ImageView   
               android:layout_height="wrap_content"   
               android:id="@+id/ItemImage"   
               android:layout_width="wrap_content"   
               android:layout_centerHorizontal="true">   
         </ImageView>  
         <TextView   
               android:layout_width="wrap_content"   
               android:layout_below="@+id/ItemImage"   
               android:layout_height="wrap_content"   
               android:text="TextView01"   
               android:layout_centerHorizontal="true"   
               android:id="@+id/ItemText">  
         </TextView>  
</LinearLayout> 

java代码

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }

	private void init() {
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
		for (int i = 0; i < 10; i++) {
			LinearLayout linearLayoutItem =  (LinearLayout) getLayoutInflater().inflate(R.layout.item, null);

			TextView textView = (TextView) linearLayoutItem.findViewById(R.id.ItemText);
			textView.setText("垂直滚动");
	
			ImageView imageView = (ImageView) linearLayoutItem.findViewById(R.id.ItemImage);
			imageView.setImageResource(R.drawable.item1);
			
			linearLayout.addView(linearLayoutItem);
		}

		
	}
}

上边的是一个有关scrollView的小例子


下边一个才是关键的哦

java代码

public class MySeccondListViewActivity extends Activity {
	LinearLayout linearLayout;
	TestScrollView scrollView;
	Button button;
	LinearLayout linearLayoutShowMore; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.category_project_list);
		linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
		scrollView = (com.damai.mobile.TestScrollView) findViewById(R.id.scrollView);
		System.out.println("first scrollView= " + scrollView.getScrollY());
		init();
		scrollView.onS(new TestOnScrollChanged(){
			@Override
			public void down() {
				super.down();
						linearLayoutShowMore.setVisibility(View.INVISIBLE);
						init(); 
				}
			@Override
			public void up() {
				super.up();
				Toast.makeText(MySeccondListViewActivity.this, "到达顶部了",
						Toast.LENGTH_SHORT).show();
			}
		});
	
		scrollView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
		        if (event.getAction() == MotionEvent.ACTION_UP) {  
		            /** 滑动到顶部和底部做处理 **/  
		        	scrollView.istouch=true;
		        }  
		        return false;  
		    }  
		});
    }

	private void init() {
		for (int i = 0; i < 5; i++) {
			final int postion = i ;
			LinearLayout linearLayout2 = (LinearLayout) getLayoutInflater().inflate(R.layout.item, null);
			
			LinearLayout linearLayoutRight = (LinearLayout) linearLayout2.findViewById(R.id.linearlayoutRight);
			LinearLayout linearLayoutLeft = (LinearLayout) linearLayout2.findViewById(R.id.linearlayoutLeft);
			
			TextView textView1 = (TextView)linearLayout2.findViewById(R.id.ItemTextLeft);
			ImageView imageView1 = (ImageView)linearLayout2.findViewById(R.id.ItemImageLeft);
			
			imageView1.setImageResource(R.drawable.item1);
			textView1.setText("第二列");
			
			TextView textView = (TextView) linearLayout2.findViewById(R.id.ItemText);
			ImageView imageView = (ImageView)linearLayout2.findViewById(R.id.ItemImage);
			
			textView.setText("布局方式");
			imageView.setImageResource(R.drawable.item2);
			
			linearLayout.addView(linearLayout2);
			linearLayoutRight.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					setTitle("您选择了Left第" + postion + "个");
				}
			});
			
			linearLayoutLeft.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					setTitle("您选择了Right第" + postion + "个");
				}
			});
		}
		linearLayoutShowMore = (LinearLayout) getLayoutInflater().inflate(R.layout.show_more_info, null);
		linearLayout.addView(linearLayoutShowMore);
	}
}
继承自scrollView的类
package com.damai.mobile;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.ScrollView;

public class TestScrollView extends ScrollView {

	public TestScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public TestScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TestScrollView(Context context) {
		super(context);
	}

	TestOnScrollChanged _t = null;

	public void onS(TestOnScrollChanged t) {
		_t = t;
	}

	// 是否正在移动
	boolean istouch = false;

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub
		super.onScrollChanged(l, t, oldl, oldt);

		if (istouch) {
			if (this.getScrollY() + this.getHeight() >= computeVerticalScrollRange()) {
				istouch = false;
				_t.down();
			}
			if (t == 0) {
				istouch = false;
				_t.up();
			}
		}

	}

}



Demo下载地址http://download.csdn.net/detail/walker02/4122519




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值