android 开发技巧(2)--使用延迟加载以及避免代码重复
2016-02-20 17:10
741 查看
对于一些需要重复利用的视图以及需要延迟加载的视图,比较好的处理方法是:
使用 include 标签避免代码重复
通过 ViewStub 实现 View 的延迟加载
先上效果图
主布局xml
其中inflatedId就是新加载进来的view的id,如果需要获取这个view,就要用这个inflatedId,原来的id已经被取代了
view_stub_layout.xml
footer.xml
java代码
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,则会报错,比如
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。
使用 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。
相关文章推荐
- Android开发笔记1
- android 中打 Log 的一些技巧
- Android属性动画完全解析(上),初识属性动画的基本用法
- android下拉刷新上拉加载的XListView的完美修改版,增加仿IOS的等待圈
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- Android漫游记(4)---.so文件动态调试一例
- Android IPC机制(五)用Socket实现跨进程聊天程序
- Android 多线程处理之多线程用法大集合
- Android IPC机制(五)用Socket实现跨进程聊天程序
- Android ListView异步加载图片乱序问题,原因分析及解决方案
- Android Context完全解析,你所不知道的Context的各种细节
- Android广播机制
- Android Scroller完全解析,关于Scroller你所需知道的一切
- Android之Installation error: INSTALL_FAILED_UPDATE_INCOMPATIBLE问题解决
- android使用min3d引擎加载模型
- Android Design Support Library详解
- android logcat的妙用及注意事项
- 在Android studio中如何使用lambda?
- Android动画动画二
- Android动画属性一