Android 元件 JSON
Step1. Android Studio→先進行環境設定
詳細位置:Android Manifest
程式碼:compile 'com.android.support:recyclerview-v7:23.3.0'
Step2. Android Studio→設定主畫面的布局
詳細位置:Layout→activity_main
注意事項:要加入程式庫才有自動完成功能
android:scrollbars = 滑動拉桿
程 式 碼:
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_attractions"
android:scrollbars="vertical"
android:background="#E0E0E0"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
Step3. Android Studio→設定Item的布局 並加入預設的圖片
詳細位置:res→Layout→新增一個Layout→ item_attraction
res→drawable→新增一個圖片→sweet
注意事項:要加入第三方程式庫 程式碼才可自動完成
程式碼:
紅色地方要注意 要加入xmlns:tools="http://schemas.android.com/tools" 才能設定 tools:text="???"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="@+id/iv_image"
android:layout_width="140dp"
android:layout_height="70dp"
android:layout_marginRight="15dp"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
android:src="@drawable/sweet" />
<TextView
android:id="@+id/tv_stitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:textColor="#212121"
android:textSize="18sp"
tools:text="Title goes here"/>
<TextView
android:id="@+id/tv_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_stitle"
android:paddingLeft="16dp"
android:textColor="#727272"
android:textSize="18sp"
tools:text="Subtitle here"/>
<Button
android:id="@+id/btn_map"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_category"
android:text="map"
android:onClick="clickMap"
android:textColor="@android:color/holo_blue_light"/>
</RelativeLayout>
|
Step4. Android Studio→創造一個Model
詳細位置:新增Package→名稱=model,再新增Class→名稱=TapieiAttractions
注意事項:因為類別 Bean,是外掛自動產生的 構造非常複雜 getter非常不易
所以我們要做一個model為我們使用者服務
程式碼:
package com.example.andy.lab14_opendata.model;
import com.example.andy.lab14_opendata.beans.TaipeiAttractionBean;
import java.util.List;
public class TaipeiAttractions {
private Bean bean; //依賴的關西,這個類別擁有類別 Bean
private List<TaipeiAttractionBean.ResultBean.ResultsBean> attractions;
private List<List<String>> imageUrlsList; //陣列裡面的陣列 所以要這樣寫
//建構子
public TaipeiAttractions(Bean bean, List<List<String>> imageUrlsList) {
this.bean = bean;
this.imageUrlsList = imageUrlsList;
this.attractions = bean.getResult().getAttractions();
}
public List<Bean.ResultBean.ResultsBean> getAttractions() {
return attractions;
}
//封裝的概念 欄位是私有的,透過getter取得
public TaipeiAttractionBean getBean() {
return bean;
}
//想要從第i項景點(Attraction) 取得什麼資訊 就自己做一個getter即可
public String getSubtitle(int index){ //從第i項景點取得Stitle
return attractions.get(index).getStitle();
}
public String getCategory(int index){ //從第i項景點取得Category
return attractions.get(index).getCategory();
}
public int getCount(){ //取得Count
return bean.getResult().getCount();
}
public List<List<String>> getImageUrlsList() {
return imageUrlsList;
} }
|
Step5. Android Studio→建立一個存放資料的類別
詳細位置:新增Package→名稱=myapp,再新增Class→名稱=MyApp
注意事項:因為MyApp這個類別的生命週期比Activity長,所以存放資料比較安全
Activity繼承到Context這個類別,而Context這個抽象類別有個方法叫 getResources()
這方法可以取得資源,所以我們要將這個方法給抽離出來(以後想設計 問答用<資料在本機端>)
所已將MyApp繼承到 Application,
因為Application已實做了Context這個抽象類別 重點是繼承Application的物件 生命週期長,資料不易銷毀
所已將MyApp繼承到 Application,
因為Application已實做了Context這個抽象類別 重點是繼承Application的物件 生命週期長,資料不易銷毀
package com.example.andy.lab14_opendata.myapp;
import android.app.Application;
import android.content.Context;
import com.example.andy.lab14_opendata.model.TaipeiAttractions;
public class MyApp extends Application {
private static Context context;
//context = 就是我自己這個類別
public MyApp() {context=this;
}
//如果別的類別要使用→MyApp.getContext().getResources() 即可
public static Context getContext() {
return context;
}
____________________________________________________________________休息下
//依賴的關西:我有一個TaipeiAttractions
private static TaipeiAttractions taipeiAttractions;
//封裝的概念:欄位是私有的 做一個getter取得
public static TaipeiAttractions getTaipeiAttractions() {
return taipeiAttractions;
}
//因為目前的資料是空的,所以要做一個setter設定,當使用這個setter這方法時
//我們new TaipeiAttractions(Bean bean, List<List<String>> imageUrlsList) 會要求輸入參數
//這時候再把 call.enqueue這個執行緒 所得到的資料放入bean 和解剖完的Urls放進去即可
public static void setTaipeiAttractions(TaipeiAttractions taipeiAttractions) {
MyApp.taipeiAttractions = taipeiAttractions;
}
}
|
下一篇再跟大家介紹 如何實現再Recyclerview
沒有留言:
張貼留言