您的位置:首页 > 移动开发 > Android开发

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

2016-02-20 17:10 741 查看
对于一些需要重复利用的视图以及需要延迟加载的视图,比较好的处理方法是:

使用 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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: