Android ViewTreeObserver的经常使用技巧

原文地址: https://blog.csdn.net/geekzhe/article/details/47003811html

Aview tree observer is used to register listeners that can be notified of globalchanges in the view tree. Such global events include, but are not limited to,layout of the whole tree, beginning of the drawing pass, touch mode change....A ViewTreeObserver should never be instantiated by applications as it isprovided by the views hierarchy. Refer toView.getViewTreeObserver() for more information.android

从上面的描述中,不难看出,ViewTreeObserver是用来帮助咱们监听某些View的某些变化的。app

 

在ViewTreeObserver中,包含了如下几个接口:ide

interface ViewTreeObserver.OnGlobalFocusChangeListener布局

interface ViewTreeObserver.OnGlobalLayoutListener测试

interface ViewTreeObserver.OnPreDrawListener字体

interface ViewTreeObserver.OnScrollChangedListenerthis

interface ViewTreeObserver.OnTouchModeChangeListenerspa

本文将测试除ViewTreeObserver.OnScrollChangedListener外的四个接口.net

 

1.    建立一个AndroidProject,修改main.xml使之以下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/full_screen"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

   

    <TextView

                   android:id="@+id/tv_show"

             android:layout_width="fill_parent"

             android:layout_height="wrap_content"

             android:text=""

             android:textSize="32px"

             android:textColor="#FFFF00"

   />

   

    <EditText

    android:id="@+id/ed_enter1"

    android:layout_width="fill_parent"

             android:layout_height="wrap_content"

             android:text=""

         />

        

    <EditText

    android:id="@+id/ed_enter2"

    android:layout_width="fill_parent"

             android:layout_height="wrap_content"

             android:text=""

         />

        

         <TextView

                   android:id="@+id/tv_display"

             android:layout_width="fill_parent"

             android:layout_height="wrap_content"

             android:text=""

   />

   

         <Button

                   android:id="@+id/button"

             android:layout_width="fill_parent"

             android:layout_height="wrap_content"

             android:text="OK"

    />   

</LinearLayout>

 

注意:给layout增长一个id:full_screen

 

2.    Activity对应的Java代码以下:

publicclass ControlViewTreeObserver extends Activity

implements

OnClickListener,

ViewTreeObserver.OnTouchModeChangeListener,            // 用于监听Touch和非Touch模式的转换

ViewTreeObserver.OnGlobalLayoutListener,                       // 用于监听布局之类的变化,好比某个空间消失了

ViewTreeObserver.OnPreDrawListener,                               // 用于在屏幕上画View以前,要作什么额外的工做

ViewTreeObserver.OnGlobalFocusChangeListener          // 用于监听焦点的变化

{

         private TextView tv_show;

    private ViewTreeObserver vto;

    private View all;

   

    private EditText ed1;

    private EditText ed2;   

    private TextView tv_display;

    private Button button;

    privatebooleanbtnClicked;

   

    @Override

    publicvoid onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        tv_show = (TextView)this.findViewById(R.id.tv_show);

        all = this.findViewById(R.id.full_screen);                                  // 获得整个屏幕对象由于顶层layoutwidthheight都是fill_parent

        vto = (ViewTreeObserver)all.getViewTreeObserver();           // 经过getViewTreeObserver得到ViewTreeObserver对象

       

        tv_display = (TextView)this.findViewById(R.id.tv_display);

        ed1 = (EditText)this.findViewById(R.id.ed_enter1);

        ed2 = (EditText)this.findViewById(R.id.ed_enter2);

        button = (Button)this.findViewById(R.id.button);

        button.setOnClickListener(this);

       

        vto.addOnTouchModeChangeListener(this);                          // 增长对应的Listener

        vto.addOnGlobalFocusChangeListener(this);                        // 增长对应的Listener

        vto.addOnPreDrawListener(this);                                              // 增长对应的Listener

        vto.addOnGlobalLayoutListener(this);                                      // 增长对应的Listener

    }

 

    // onTouchModeChanged是接口ViewTreeObserver.OnTouchModeChangeListener

    // 中定义的方法。

         @Override

         publicvoid onTouchModeChanged(boolean isInTouchMode)

         {

                   if(isInTouchMode)tv_show.setText("In touch mode");

                   elsetv_show.setText("Not in touch mode");

         }

 

         // onGlobalLayout是接口ViewTreeObserver.OnGlobalLayoutListener

         // 中定义的方法。

         // Callback method to be invokedwhen the global layout state or the

         // visibility of views within the viewtree changes

         @Override

         publicvoid onGlobalLayout()

         {

                   if(btnClicked)

                   {

                            if(!ed2.isShown())

                                     ed1.setText("第二个EditText不见了");

                            else

                                     ed1.setText("第二个EditText出来了");

                   }

         }

 

         // onPreDraw是接口ViewTreeObserver.OnPreDrawListener

        // 中定义的方法。

         @Override

         publicboolean onPreDraw()

         {

                   // 在屏幕上画出ed1控件之间给它增长一个提示并改变其字体大小

                   ed1.setHint("onPreDraw方法中增长一个提示信息");

                   ed1.setTextSize((float) 20.0);

                  

                   //return false;    // Return true to proceed with the current drawing pass, or falseto cancel.

                   returntrue;        // 若是此处不返回true则整个界面不能完整显示。

         }                                            

 

         // onGlobalFocusChanged是接口ViewTreeObserver.OnGlobalFocusChangeListener

         // 中定义的方法。

         // 焦点发生变化时,会触发这个方法的执行

         @Override

         publicvoid onGlobalFocusChanged(View oldFocus, ViewnewFocus)

         {

                   if(oldFocus != null && newFocus != null)

                   {

                            tv_display.setText("Focus /nFROM:/t" + oldFocus.toString() + "/n     TO:/t" + newFocus.toString());

                   }

         }

 

         @Override

         publicvoid onClick(View v)

         {

                   // 改变ed2的可见性会触发onGlobalLayout方法的执行

                   btnClicked = true;

                   if(v.getId() == R.id.button)

                   {

                            if(ed2.isShown())

                                     ed2.setVisibility(View.INVISIBLE);

                            else

                                     ed2.setVisibility(View.VISIBLE);

                   }

         }

}

 

3.    运行结果:

能够看到第一个EditText中存在字体发生了变化的提示信息,这种效果是在onPreDraw()方法中实现的。

用鼠标点击屏幕上的第二个EditText,

有两个变化:

一个是有Not intouch mode变成了In touchmode,二是显示了焦点变化方面的信息。它们分别是onTouchModeChanged和onGlobalFocusChanged这两个方法所输出的信息。

 

若是用模拟器右边的键盘进行操做,将交掉移动到第一个EditText,则又会回到Notin touch mode的状态。

 

点击OK按钮,改变第二个EditText的可见性:

第一个EditText中的内容是在onGlobalLayout方法中设定的。

相关文章
相关标签/搜索