Android:UI控件scrollView、TabHost

UI控件scrollView:

容器内最外层只能有一个布局。


UI控件TabHost:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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滚动到底部:

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
     /**
      * ******************增加滚动监听接口*****************************
      */
     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"属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
                 <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已经加入了一个方法,能监听到是否已经不能滚动,稍加处理,就可以监听是否滑到底部了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
     }
}





本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1209844,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值