ViewGroup调用scrollTo()

PS:

该篇博客已经deprecated,不再维护,详情请参见 

站在源码的肩膀上全解Scroller工作机制

 http://blog.csdn.NET/lfdfhl/article/details/53143114

 

MainActivity如下:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cc.ac;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. import android.widget.LinearLayout;  
  8. import android.app.Activity;  
  9. /** 
  10.  * Demo描述: 
  11.  * 对ViewGroup调用scrollTo()和scrollBy()方法. 
  12.  *  
  13.  * 验证理论: 
  14.  * 假如一个ViewGroup(比如此处的XXXLayout)调用了scrollTo(By)() 
  15.  * 它的Content(即它所有的子View)都会移动. 
  16.  *  
  17.  *    
  18.  * 备注说明: 
  19.  * 使用scrollTo(By)()方法移动过程较快而且比较生硬. 
  20.  * 为了优化scrollTo(By)()的滑动过程可采用Scroller类. 
  21.  * 该类源码第一句This class encapsulates scrolling. 
  22.  * 就指明了该类的目的:封装了滑动过程. 
  23.  * 在后面的示例中,将学习到Scroller的使用. 
  24.  */  
  25. public class MainActivity extends Activity {  
  26.    private LinearLayout mLinearLayout;  
  27.    private Button mButton;  
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.         init();  
  33.     }  
  34.       
  35.     private void init(){  
  36.         mLinearLayout=(LinearLayout) findViewById(R.id.linearLayout);  
  37.         mButton=(Button) findViewById(R.id.button);  
  38.         mButton.setOnClickListener(new OnClickListener() {  
  39.             @Override  
  40.             public void onClick(View view) {  
  41.                 mLinearLayout.scrollBy(-50, 0);  
  42.             }  
  43.         });  
  44.     }  
  45.   
  46.       
  47. }  

 

 

 

 

 

main.xml如下:

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:tools="http://schemas.android.com/tools"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent" >  
    5.   
    6.     <LinearLayout  
    7.         android:id="@+id/linearLayout"  
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="300dip"  
    10.         android:background="@android:color/darker_gray" >  
    11.   
    12.         <TextView  
    13.             android:layout_width="150dip"  
    14.             android:layout_height="50dip"  
    15.             android:background="@android:color/black"  
    16.             android:text="@string/hello_world"  
    17.             android:textColor="@android:color/white" />  
    18.     </LinearLayout>  
    19.   
    20.     <Button  
    21.         android:id="@+id/button"  
    22.         android:layout_width="wrap_content"  
    23.         android:layout_height="wrap_content"  
    24.         android:layout_alignParentBottom="true"  
    25.         android:layout_centerHorizontal="true"  
    26.         android:text="BUTTON" />  
    27.   
    28. </RelativeLayout>  
posted @ 2016-11-26 15:18  天涯海角路  阅读(115)  评论(0)    收藏  举报