天天看點

Android:UI控件scrollView、TabHost

UI控件scrollView:

容器内最外層隻能有一個布局。

UI控件TabHost:

public class MainActivity extends TabActivity//繼承TabActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                      
        TabHost tabHost = getTabHost();
                      
        /**
         * 自定義建立TabHost方法
         */
        //TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        //tabHost.setup();
                      
        tabHost.addTab(tabHost.newTabSpec("").setIndicator("title1",
                getResources().getDrawable(R.drawable.ic_launcher))//設定tab背景圖
                .setContent(new Intent(this, Second.class)));
                              
        tabHost.addTab(tabHost.newTabSpec("").setIndicator("title1")
                .setContent(new Intent(this, Second.class)));
                      
        tabHost.addTab(tabHost.newTabSpec("").setIndicator("title1")
                .setContent(new Intent(this, Second.class)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));//Activity的啟動模式
    }
                  
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}      

scrollView相關:

  1. 監聽scrollView滾動到底部:

代碼如下:

    /**
     * ******************增加滾動監聽接口*****************************
     */
    private OnScrollListener onScrollListener;
    
    /**
     * 設定滾動接口
     * 
     * @param onScrollListener
     */
    public void setOnScrollListener(OnScrollListener onScrollListener)
    {
        this.onScrollListener = onScrollListener;
    }
    
    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy)
    {
        super.onScrollChanged(x, y, oldx, oldy);
        if (onScrollListener != null)
        {
            onScrollListener.onScroll(x, y, oldx, oldy);
            
            //是否滾動到底部
            if(getScrollY() + getHeight() >= computeVerticalScrollRange() )
            {
                onScrollListener.onScrollBottom(x, y, oldx, oldy);
            }
        }
        
    }
    
    /**
     * 滾動的回調接口
     */
    public interface OnScrollListener
    {
        /**
         * 回調方法, 傳回ScrollView滑動的各方向距離
         */
        public void onScroll(int x, int y, int oldx, int oldy);
        public void onScrollBottom(int x, int y, int oldx, int oldy);
    }      

2.使scrollview内的子控件充滿整個螢幕,需要設定android:fillViewport="true"屬性。

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fillViewport="true" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="fill_parent"
                        android:orientation="vertical" >

                        <!-- 卷軸欄内布局 -->

                        <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:orientation="vertical" >      

3.scrollTo() 是直接指定滾動條的位置, 但是由于這個動作不是單純關于 ScrollView 而已, 還要根據 ScrollView 裡面包含的View 的實際資訊. 是以這動作必須在頁面加載完成以後才能執行. 

4.網上說的方法亂七八糟,能用的就是自己算高度,其實sdk-9中,ScrollView已經加入了一個方法,能監聽到是否已經不能滾動,稍加處理,就可以監聽是否滑到底部了。

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
public class BottomScrollView extends ScrollView {
	private OnScrollToBottomListener onScrollToBottom;
	
	public BottomScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	public BottomScrollView(Context context) {
		super(context);
	}
	@Override
	protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,
			boolean clampedY) {
		super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
		if(scrollY != 0 && null != onScrollToBottom){
			onScrollToBottom.onScrollBottomListener(clampedY);
		}
	}
	
	public void setOnScrollToBottomLintener(OnScrollToBottomListener listener){
		onScrollToBottom = listener;
	}
	public interface OnScrollToBottomListener{
		public void onScrollBottomListener(boolean isBottom);
	}
}      

繼續閱讀