深入理解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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章