异步加载任务机制是为了处理多个对象同时对主线程操作而出现的,作用和handler是相似的,在后台进行操作之后,可以更改UI。但他的实现方式比handler容易许多。
AsycTask中通过四个方法实现功能:onPreExecute();在执行具体操作之前执行,可以进行一些操作的初始化;onPostExecute();更新操作完成之后执行;doInBackground;具体的逻辑操作,不会修改UI,将更改操作传送给onProgressUpdate;onProgressUpdate();负责执行更新UI的操作;除了doInbackground是子线程,其他的都是主线程。
本例涉及知识点:I/O流,异步处理任务,动态加载控件;所用文件为数据截取软件截取出来的相关的图片url和名字文件。
一,使用表格布局进行展示,将图片和文字进行封装到一个布局中,在代码中动态加载;
二,使用scrollView包裹表格布局,实现内容的滑动;
三,使用asynctask实现图片的批量加载到布局中;
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">//使scrollview里面的元素填满布局,使用fill-parent没用
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridlayout"
android:columnCount="3">
</Gri