码迷,mamicode.com
首页 > 其他好文 > 详细

学习使用ViewStub

时间:2015-10-25 22:58:11      阅读:575      评论:0      收藏:0      [点我收藏+]

标签:

这是官方介绍:

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. Therefore, the ViewStub exists in the view hierarchy until setVisibility(int) or inflate() is invoked. The inflated View is added to the ViewStub‘s parent with the ViewStub‘s layout parameters. Similarly, you can define/override the inflate View‘s id by using the ViewStub‘s inflatedId property. For instance:

     <ViewStub android:id="@+id/stub"
               
android:inflatedId="@+id/subTree"
               
android:layout="@layout/mySubTree"
               
android:layout_width="120dip"
               
android:layout_height="40dip" />
 

The ViewStub thus defined can be found using the id "stub." After inflation of the layout resource "mySubTree," the ViewStub is removed from its parent. The View created by inflating the layout resource "mySubTree" can be found using the id "subTree," specified by the inflatedId property. The inflated View is finally assigned a width of 120dip and a height of 40dip. The preferred way to perform the inflation of the layout resource is the following:

     ViewStub stub = (ViewStub) findViewById(R.id.stub);
     
View inflated = stub.inflate();
 

When inflate() is invoked, the ViewStub is replaced by the inflated View and the inflated View is returned. This lets applications get a reference to the inflated View without executing an extra findViewById().

下面是我从别人那里学来的:

在实际开发中,我们经常会在布局中准备多个View,默认希望都是不显示,在程序运行时会根据条件来控制某个View显示,
    这样最简单的做法就是使用View.GONE的属性隐藏。但是这样带来了问题,有些View可能一直都没使用,但却又占用了内存,因为即使设置为View.GONE的View仍然会
    被实例化。解决这个问题,用ViewStub就行了。ViewStub是一个不可见,占用资源非常小的View,能够用于在程序运行时延迟加载布局。当inflate包含ViewStub的布局时,
    只实例化了这个ViewStub,其所指向的布局并没有被实例化。当这个ViewStub被设置visible,或者调用inflate方法时才实例化它指向的布局的View和显示

package com.viewstub.carlos.viewstubtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button mShowButton;
    private Button mHideButton;
    private ViewStub mViewStub;
    private TextView mTextView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewStub = (ViewStub)findViewById(R.id.stub);
        //实例化布局添加到ViewStub显示
        mViewStub.inflate();
        mShowButton = (Button)findViewById(R.id.show_viewstub);
        mHideButton = (Button)findViewById(R.id.hide_viewstub);
        mShowButton.setOnClickListener(new showViewStubButtonListener());
        mHideButton.setOnClickListener(new hideViewStubButtonListener());
        mTextView1 = (TextView)findViewById(R.id.textView);
        mTextView1.setText(R.string.text1);
    }
    class showViewStubButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            mTextView1.setText(R.string.subtree1);
            //设置可见也能实例化布局
            mViewStub.setVisibility(View.VISIBLE);

        }
    }
    class hideViewStubButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            mViewStub.setVisibility(View.GONE);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

技术分享

整个工程文件:

http://pan.baidu.com/s/1i3CnWfr

学习使用ViewStub

标签:

原文地址:http://my.oschina.net/kingfrog/blog/521872

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!