Android開發--非同步載入

來源:互聯網
上載者:User

標籤:

因為移動端軟體開發思維模式或者說是開發的架構其實是不分平台和程式設計語言的,就拿安卓和IOS來說,他們都是移動前端app開發展示資料和使用者互動資料的資料終端,移動架構的幾個大模組:UI介面展示、本機資料可持續化儲存、網路資料請求、效能最佳化等等,安卓和IOS開發都要考慮這些架構的模組。所以,熟悉IOS的開發的人,再去學習一下安卓的開發以及安卓的開發模式,你會發現很多技術和思想安卓和IOS是一樣的,只是可能說法不一樣,由於程式設計語言比如OC和Java略微的差異性,編碼習慣和細節不一樣之外,其他都是一樣的。

 

本人開始對安卓略有興趣,開始對安卓粗淺的學習,一方面也會拿IOS和安卓進行對比闡述,如果你會IOS,再學習安卓的,閱讀本人的部落格也許會對你有很大的協助。

(但是對於安卓開發的語言基礎Java、以及android studio的使用,本人不會詳細闡述,作為有情懷有獨立能力的程式員,這些基礎應該不會難道你們的吧,更何況本人對android studio的使用一直通過google和百度來學習相關的setting和快速鍵)

 

在Android中,非同步載入最常用的兩種方式:

  1、多線程\線程池

  2、AsyncTask

當然,AsyncTask底層是基於線程池實現的。所以以上兩種方法是異曲同工。

 

一、首先,按照在IOS中用UITableView載入資料的思路一樣,我們先要建立出自訂的Cell以及Cell的布局:

建立item_layout.xml檔案:

源碼:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="wrap_content" 5     android:padding="4dp" 6     android:orientation="horizontal" 7     > 8     <ImageView 9         android:id="@+id/iv_icon"10         android:layout_width="64dp"11         android:layout_height="64dp"12         android:src="@mipmap/ic_launcher"/>13 14 15     <LinearLayout16         android:layout_width="match_parent"17         android:layout_height="match_parent"18         android:padding="4dp"19         android:gravity="center"20         android:orientation="vertical">21 22         <TextView23             android:id="@+id/tv_title"24             android:layout_width="match_parent"25             android:layout_height="wrap_content"26             android:textSize="15sp"27             android:maxLines="1"28             android:text="標題標題標題"/>29 30 31         <TextView32             android:id="@+id/tv_content"33             android:layout_width="match_parent"34             android:layout_height="wrap_content"35             android:textSize="10sp"36             android:maxLines="3"37             android:text="內容內容內容"/>38 39     </LinearLayout>40 41 42 </LinearLayout>

就這樣,一個自訂的item就建立好了,就好比我們IOS中xib或者storyboard上的UITableViewCell建立好了。

然後接著在activity_main.xml中建立一個ListView,這個ListView就好比我們IOS的UITableView。

源碼:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:tools="http://schemas.android.com/tools" 4     android:id="@+id/activity_main" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     android:paddingBottom="@dimen/activity_vertical_margin" 8     android:paddingLeft="@dimen/activity_horizontal_margin" 9     android:paddingRight="@dimen/activity_horizontal_margin"10     android:paddingTop="@dimen/activity_vertical_margin"11     tools:context="com.example.heyang.myapplication.MainActivity">12 13     <ListView14         android:id="@+id/lv_main"15         android:layout_width="match_parent"16         android:layout_height="match_parent"17         />18 19 20 </RelativeLayout>

二、因為每一個item或者類比IOS的每一個Cell都需要一個圖片地址、標題Title、內容Content三個資料,所以我們就需要一個模型對象來一一映射對應到item,在安卓或者Java中的說法叫建立一個Bean對象,其實可以類比理解為IOS的model模型對象。

源碼:

 1 package com.example.heyang.myapplication; 2  3 /** 4  * Created by HeYang on 16/10/5. 5  */ 6  7 public class NewsBean { 8     // 包含三個屬性:1、標題2、內容3、圖片的網址 9     public String newsIconURL;10     public String newsTitle;11     public String newsContent;12 }

 

Android開發--非同步載入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.