[Android]异步 layout inflation(翻译)

简介:
随笔- 137  文章- 6  评论- 145 

[Android]异步 layout inflation(翻译)


以下内容为原创,欢迎转载,转载请注明
来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5829809.html

异步 layout inflation

原文:https://medium.com/@lupajz/asynchronous-layout-inflation-7cbca2653bf#.q22vpezg4

随着最近发布的Android Support Library, revision 24,Google开发者在v4包中增加了一个用来异步inflate layouts的帮助类。

进入 AsyncLayoutInflater

你会发现AsyncLayoutInflater可以使用在当你想要去懒加载你应用中部分UI或者用户行为的响应。这个帮助类将会允许你的UI线程在执行繁重的inflate时继续保持响应。

使用AsyncLayoutInflater你需要在你应用的UI线程创建一个它的实例。

假设下面部分代码是你Activity代码中的一部分(我将在这里使用Kotlin语法):

val inflater = AsyncLayoutInflater(this)

然后现在可以通过这个实例inflate你的layout文件:

inflater.inflate(resId: Int, parent: ViewGroup) 

如你所见,inflate函数接收3个参数。第一个是你layout资源,第二个是可选的view作为预期inflated层次结构的parent,然后第三个是一个OnInflateFinishedListener,是一个回调,一旦layout被inflate完毕它就会被回调(例子中被lambda函数代替)。

比较基本的LayoutInflater的inflate函数,一般使用一个boolean作为第三个参数,它表示是否inflate层次结构应该附属岛一个root参数上面。在我们的inflate函数的异步版本中并没有这样一个参数,大多数情况下你会使用这种方式调用:

inflater.inflate(resId: Int, parent: ViewGroup) 
    { view, resid, parent -> parent.addView(view) }

使用AsyncLayoutInflater的缺点

当然它有如下一些缺点:

  • parent的 generateLayoutParams() 函数必须是线程安全的。

  • 所有正在构建的views一定不能创建任何 Handlers 或者调用 Looper.myLooper 函数。

  • 不支持设置LayoutInflater.Factory也不支持LayoutInflater.Factory2

  • 不支持包含Fragments的inflating layouts

如果我们尝试异步的方式去inflate的layout不支持这种方式,那么inflation处理将会自动回退到主线程中。

使用Kotlin Android Extensions 的 Kotlin的小例子

MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        loadFirst.setOnClickListener { 
           loadAsync(R.layout.async) { 
             second.text = "I am second TextView" 
           } 
        }
    }
}

fun MainActivity.loadAsync(@LayoutRes res: Int, 
                           action: View.() -> Unit) =
    AsyncLayoutInflater(this).inflate(res, frame) 
    { view, resid, parent ->
        with(parent) {
            addView(view)
            action(view)
        }
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.cartoon.player.MainActivity">

    <TextView
        android:id="@+id/loadFirst"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:gravity="center"
        android:layout_marginBottom="16dp"
        android:text="Load f async"/>

</LinearLayout>

async.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">

    <TextView
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="1"/>

    <TextView
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="2"/>

</LinearLayout>
本文转自天天_byconan博客园博客,原文链接:http://www.cnblogs.com/tiantianbyconan/p/5829809.html ,如需转载请自行联系原作者
相关文章
|
1月前
|
Java 数据库 Android开发
Android异步之旅:探索AsyncTask
Android异步之旅:探索AsyncTask
23 0
|
7月前
|
API Android开发
Android 自定义最大宽度,高度, 宽高比例 Layout
Android 自定义最大宽度,高度, 宽高比例 Layout
|
1月前
|
Android开发 开发者
Android异步之旅:探索IntentService
Android异步之旅:探索IntentService
20 0
|
1月前
|
消息中间件 数据库 Android开发
Android异步之旅:探索HandlerThread
Android异步之旅:探索HandlerThread
22 0
|
1月前
|
Android开发 对象存储
OSS对象储存android开发进行下载到本地文件时异步操作失效
android vivo80使用官方示例代码进行文件下载,但是使用oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>()时onSuccess和onFailure不执行
|
8月前
|
安全 Java Android开发
Android 中AsyncTask后台线程,异步任务的理解
Android 中AsyncTask后台线程,异步任务的理解
101 0
|
4月前
|
XML Java 调度
Android App网络通信中通过runOnUiThread快速操纵界面以及利用线程池Executor调度异步任务实战(附源码 简单易懂)
Android App网络通信中通过runOnUiThread快速操纵界面以及利用线程池Executor调度异步任务实战(附源码 简单易懂)
30 0
|
4月前
|
XML Java Android开发
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
60 0
|
6月前
|
开发工具 Android开发
Android Studio layout 文件没有代码提示
Android Studio layout 文件没有代码提示
166 0
|
8月前
|
Android开发
Android 使用DataBinding时 将布局页面转换为数据绑定布局(Convert to data binding layout) 不出现提示解决办法
Android 使用DataBinding时 将布局页面转换为数据绑定布局(Convert to data binding layout) 不出现提示解决办法
90 0