android 开发技巧(2)--使用延迟加载以及避免代码重复

对于一些需要重复利用的视图以及需要延迟加载的视图,比较好的处理方法是:

使用 include 标签避免代码重复
通过 ViewStub 实现 View 的延迟加载

先上效果图
这里写图片描述
这里写图片描述

主布局xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff">
    <TextView
        android:id="@+id/id_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:textColor="#000000"
        android:text="测试"/>

    <Button
        android:layout_below="@id/id_text"
        android:id="@+id/id_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="显示ViewStub控件"
        />
    <ViewStub
        android:id="@+id/stub"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout="@layout/view_stub_layout"
        android:inflatedId="@+id/view_stub"/>
    <!--<include layout="@layout/footer_with_layout_properties"/>-->
    <include
        layout="@layout/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp"/>
</RelativeLayout>

其中inflatedId就是新加载进来的view的id,如果需要获取这个view,就要用这个inflatedId,原来的id已经被取代了

view_stub_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/id_viewStubButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是ViewStub演示控件"
        android:textColor="#000000"/>
</LinearLayout>

footer.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:gravity="center"
    android:text="底部控件测试二"
    android:textColor="#000000">

</TextView>

java代码

//使用延迟加载以及避免代码重复,使用 <include /> 标签避免代码重复,通过 ViewStub 实现 View 的延迟加载
public class Hack2Activity extends Activity {
    private ViewStub mViewStub;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hack2);
        mViewStub = (ViewStub) findViewById(R.id.stub);
        findViewById(R.id.id_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = mViewStub.inflate();
                mViewStub.setVisibility(View.VISIBLE);
               Button button = (Button)view. findViewById(R.id.id_viewStubButton);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Hack2Activity.this, "点击", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
    }

}

mViewStub.setVisibility(View.VISIBLE);可以不需要,如果需要,则必须写在View view = mViewStub.inflate();后边,否则mViewStub.inflate()报错:java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent,因为viewstub不能反复inflate,只能inflate一次,setVisibility会间接调用inflate
如果已经调用了inflate()方法,则将用新的ID替换掉原来的,如果再调用原来的ID,则会报错,比如

 findViewById(R.id.id_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = mViewStub.inflate();
                mViewStub.setVisibility(View.VISIBLE);
               Button button = (Button)view. findViewById(R.id.id_viewStubButton);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Hack2Activity.this, "点击", Toast.LENGTH_LONG).show();
                    }
                });

                View view2 = findViewById(R.id.stub);
                view2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                View view3 = findViewById(R.id.view_stub);
                view3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
        });

view2.setOnClickListener这句代码会报错,因为ID 已经被替换掉
说明:

1.使用 include 标签,直接在include 标签里使用 android:layout_* 属性,如果想在include 标签中覆盖被包含布局所指定的任何 android:layout_*属性,必须在 include 标签中同时指定 android:layout_width 和android:layout_height 这两个属性,而 footer.xml 文 件 中 的 layout_width 和 layout_height 属性都指定为 0dp。

2.“ViewStub 是一种不可视并且大小为 0 的视图,可以延迟到
运行时填充(inflate)布局资源。当 ViewStub 设置为可视或者
inflate() 方法被调用后,就会填充布局资源,然后 ViewStub 便会
被填充的视图替代。”

3.ViewStub 的inflatedId 是 调 用ViewStub 的 inflate() 方 法 或 者 setVisibility() 方 法 时 返 回 的 ID,这个 ID 便是被填充的 View 的 ID。

4.inflate()被调用时, 被加载的视图替代viewstub并且返回自己的视图对象。具体是通过viewStub.infalte()或viewStub.setVisibility(View.VISIBLE)来完成

5.对ViewStub的inflate操作只能进行一次,因为inflate的时候是将其指向的布局文件解析inflate并替换掉当前ViewStub本身(由此体现出了ViewStub“占位符”性质),一旦替换后,此时原来的布局文件中就没有ViewStub控件了,因此,如果多次对ViewStub进行infalte,会出现错误信息:ViewStub must have a non-null ViewGroup viewParent。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值