您的位置:首页 > 编程语言

Hack2 使用延迟加载以及避免代码重复

2016-03-07 22:54 393 查看
1、使用<include />标签避免代码重复

  比如在我们的工程中,要经常用到一个TestView,那么可以把TextView的布局代码单独写入一个文件中,然后在其他布局中引用它就可以了。

<!-- footer_with_layout_properties.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:text="@string/footer_text" />


<!-- activity_main.xml -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:text="@string/hello_world"/>
<include layout="@layout/footer_with_layout_properties"/>
</RelativeLayout>


  但是这种方法有一个问题:如果要引用TextView的布局是LinearLayout怎么办?有些属性是不能用的,这样会不会发生错误呢?这个时候就需要在<include />标签里使用android:layout_* 属性。下面分别修改两个布局文件,如下:

<!-- footer_with_layout_properties.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="@string/footer_text" />


<!-- activity_main.xml-修改的部分 -->
<include layout="@layout/footer_with_layout_properties"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"/>


  和之前有什么不一样呢?在TextView一开始的定义中,设置它的宽度和高度都为0,然后在include的时候才指定。这样做就可以根据要调用的布局的实际情况设置宽度和高度。如果不设置,这个页脚就显示不出来。

2、通过ViewStub实现View的延迟加载

  有的时候,一些布局可能不是每个用户都需要看到。比如地图(MapView),那么可以把MapView放入ViewStub标签中,实现地图的延迟加载。先看看ViewStub的介绍吧:

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

  接下来看看怎么实现它:

<!-- TestViewStub - activity_main.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testviewstub.MainActivity" >

<Button android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="显示地图"
android:onClick="onShowMap"/>
<ViewStub android:id="@+id/map_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout="@layout/map"
android:inflatedId="@+id/map_view"/>

</RelativeLayout>


<!-- map.xml -->
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:apiKey="my_api_key"/>


  逻辑是很简单的:ViewStub引用map.xml,之后ViewStub加载的就是这个布局。首先说明一下inflateId属性的意思:它就是调用ViewStub的inflate()方法或者是setVisibility()方法返回的ID。在本例中,我们直接调用setVisibility(View.VISIBLE)就可以了。如果想获取被填充的视图的引用,inflate() 方法会直接返回该引用,这样避免了再次调用findViewById() 方法。

private View mViewStub;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewStub = findViewById(R.id.map_stub);
}

public void onShowMap(View v) {
Log.d("sysu", "进入点击事件");
mViewStub.setVisibility(View.VISIBLE);
}


  问题:ViewStub和一般的View设置Visibility有什么不同呢?

(但是不知道为什么出现了错误,把谷歌的MapView换成一般的布局,比如TextView就正常了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: