Android应用开发之性能优化1:ViewStub

     开发我们的安卓应用时,根据产品需求,可能需要在运行时根据一定的条件来动态显某View或Layout。我们最常用的方法可能就是:把所有可能用到的View都写在布局文件中,先把不需要的View.setVisibility()参数设为View.GONE(不可见),然后在代码中根据显示的需要动态setVisibility(View.VISIBLE)。这样做好处在于:逻辑简单,控制灵活。缺点也很明显:耗费资源。

      更好的方法就是:使用ViewStub,它是一个轻量级的View:看不见的,不占布局位置,占用资源非常小的控件。在我们的布局文件中为ViewStub指定一个布局,在Inflate布局的时候,只有ViewStub会被初始化,然后当ViewStub被设置为可见的时候,或是调用了ViewStub.inflate()的时候,ViewStub所向的布局就会被Inflate和实例化,然后ViewStub的布局属性都会传给它所指向的布局。就可以使用ViewStub来方便的在运行时,是否要显示出某个布局。

      显然ViewStub使用方便而且从性能上做了优化,不过它不可能是万能的,说说ViewStub的一些特点:

         1. ViewStub只能Inflate一次,之后ViewStub对象会被置为空。意思是:某个被ViewStub指定的布局被Inflate后,就不会够再通过ViewStub来控制它了,相当于我们在特定的情况下让ViewStub中的布局可见了,就不能再让其不可见。

         2. ViewStub只能Inflate一个布局文件,而不是某个具体的View(如TextView),当然可以把View写在某个布局文件中。

     所以我们通常在以下情况下才考虑使用ViewStub:

         1. 在程序的运行期间,某个布局在Inflate后,就不会有变化,除非重新启动。

         2. 想要控制显示与隐藏的是一个布局文件,而非某个View。

  下面是一个很简单的例子:

首先是含有ViewStub的而布局文件layout.activity_result

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
           <RelativeLayout <!--初始化进入页面就要显示的布局-->
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF" >

            <mageView
                android:id="@+id/personal_page_iv"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="15dp"
                android:src="@drawable/clear" />
            <TextView
                android:id="@+id/personal_page_name_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_toRightOf="@id/personal_page_iv"
                android:textColor="#333333"
                android:textSize="14sp" 
               />
            <TextView
                android:id="@+id/personal_page_id_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/personal_page_name_tv"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/personal_page_iv"
                android:text="ID:"
                android:textColor="#969696"
                android:textSize="12sp" />
            <TextView
                android:id="@+id/personal_page_id_number_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/personal_page_name_tv"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/personal_page_id_tv"
                android:text="8888888"
                android:textColor="#969696"
                android:textSize="12sp" />
            <TextView
                android:id="@+id/personal_page_total_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/personal_page_id_tv"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/personal_page_iv"
                android:textColor="#969696"
                android:textSize="12sp" />
            <TextView
                android:id="@+id/personal_page_total_number_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/personal_page_id_number_tv"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/personal_page_total_tv"
                android:text="245"
                android:textColor="#969696"
                android:textSize="12sp" />
        </RelativeLayout>
    <ViewStub<!--进入页面只有数据加载成功才要显示的布局-->
                android:id="@+id/stub_pay_result_success"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inflatedId="@+id/subTree"
                android:layout="@layout/mystub_pay_result_fail" />

</LinearLayout>

然后我们在所在Activity的代码中来控制ViewStub显示:

Class MyActivity extends Activity{

ViewStub viewStub;

@Override
protected void onCreate(Bundle savedInstanceState) {


setContentView(R.layout.activity_result);
      initViews();//初始化控件,省略

               getServerData();//获取服务器服务对应数据

private void getServerData(){

   //如果获取成功,比如成功的标志是boolean  flag=true;

if(flag){

viewStub = (ViewStub) findViewById(R.id.stub_pay_result_success);

        View view = viewStub.inflate();
//然后再用findViewById()找到view中的控件进行设置数据

}

}

这样就可以在获取成功的时候把数据显示出来,如果获取失败则不会显示数据,是不很简单呢?

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。