界面截圖:java
界面很是的簡單。。。android
數據來源Openweathermap和和風天氣。json
主要界面使用RecyclerView網絡
在新版的RecyclerView裏面,裏面的item佈局android:layout_height不能夠爲match_parent,應爲這樣的話,每個item的佈局的高度就是RecyclerView的高度。ide
如下是裏面的item的佈局:佈局
這個item佈局主要用來顯示當前的天氣情況。ui
這個佈局主要用來顯示天氣預報的內容。url
這個佈局主要用來顯示當前天氣的其餘數據。.net
adapter裏面主要代碼插件
public class WeatherAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private LayoutInflater mInflater; private static final int HEAD_ITEMTYPE = 100; private static final int BODY_ITEMTYPE = 101; private static final int FOOT_ITEMTYPE = 102; private static final int ADS_ITEMTYPE = 103; //admob view // private AdView gadView; //數據當前天氣的數據 private ItemCurrWeatherData currWeatherData = null; //天氣預報的數據 private List<ItemWeatherForecast> forecastsList; public WeatherAdapter(Context c) { context = c; mInflater = LayoutInflater.from(c); forecastsList = new ArrayList<ItemWeatherForecast>(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { RecyclerView.ViewHolder viewHolder = null; if (viewType == HEAD_ITEMTYPE) { View v = mInflater.inflate(R.layout.item_main, parent, false); viewHolder = new ViewHolder1(v); } else if (viewType == BODY_ITEMTYPE) { View v = mInflater.inflate(R.layout.item_body, parent, false); viewHolder = new ViewHolder2(v); } else if (viewType == FOOT_ITEMTYPE) { View v = mInflater.inflate(R.layout.item_detail, parent, false); viewHolder = new ViewHolder3(v); } else if (viewType == ADS_ITEMTYPE) { View v = mInflater.inflate(R.layout.item_ad, parent, false); viewHolder = new ViewHolderAd(v); } return viewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof ViewHolder1) { //頭部 if (currWeatherData != null) { ... } } else if (holder instanceof ViewHolder2) { //天氣預報信息 ((ViewHolder2) holder).setStyle(position); if (forecastsList.size() != 0) { ... } } else if (holder instanceof ViewHolder3) { //天氣詳細信息 if (currWeatherData != null) { ... } } } @Override public int getItemCount() { return 7; } @Override public int getItemViewType(int position) { if (position == 0) { return HEAD_ITEMTYPE; } else if (position == 5) { return FOOT_ITEMTYPE; } else if (position == 6) { return ADS_ITEMTYPE; } else { return BODY_ITEMTYPE; } } /** * 列表中的頭部 */ public static class ViewHolder1 extends RecyclerView.ViewHolder { private ImageView weatherImg; private TextView temp, description, loc, date; public ViewHolder1(View itemView) { super(itemView); weatherImg = (ImageView) itemView.findViewById(R.id.weatherImg); temp = (TextView) itemView.findViewById(R.id.temp); description = (TextView) itemView.findViewById(R.id.description); loc = (TextView) itemView.findViewById(R.id.loc); date = (TextView) itemView.findViewById(R.id.date); } ... } /** * 列表中天氣預報的item */ public static class ViewHolder2 extends RecyclerView.ViewHolder { private View root; private TextView data, weather, min_temp, max_temp; private ImageView weatherImg; public ViewHolder2(View itemView) { super(itemView); root = itemView.findViewById(R.id.root); data = (TextView) itemView.findViewById(R.id.item_date); weather = (TextView) itemView.findViewById(R.id.item_weather); weatherImg = (ImageView) itemView.findViewById(R.id.img); min_temp = (TextView) itemView.findViewById(R.id.item_mintemp); max_temp = (TextView) itemView.findViewById(R.id.item_maxtemp); } ... } /** * 列表的尾部 */ public static class ViewHolder3 extends RecyclerView.ViewHolder { private TextView pressure, humidity, sunrise, sunset, windspeed, winddeg; public ViewHolder3(View itemView) { super(itemView); pressure = (TextView) itemView.findViewById(R.id.pressure); humidity = (TextView) itemView.findViewById(R.id.humidity); sunrise = (TextView) itemView.findViewById(R.id.sunrise); sunset = (TextView) itemView.findViewById(R.id.sunset); windspeed = (TextView) itemView.findViewById(R.id.windspeed); winddeg = (TextView) itemView.findViewById(R.id.winddeg); } ... } /** * 最後的說明 */ public static class ViewHolderAd extends RecyclerView.ViewHolder { private AdView adView; public ViewHolderAd(View itemView) { super(itemView); // adView = (AdView) itemView.findViewById(R.id.adView); // AdRequest adRequest = new AdRequest.Builder().build(); // adView.loadAd(adRequest); } } ..... }
主要是根據getItemViewType來決定顯示不一樣的item佈局。
天氣數據的處理
推薦使用一個android studio的插件,gsonformat,能夠根據json直接生成java對象。
當前的天氣信息來自Openweathermap
{ "coord": { "lon": 119.31, "lat": 26.06 }, "weather": [ { "id": 803, "main": "Clouds", "description": "broken clouds", "icon": "04n" } ], "base": "stations", "main": { "temp": 299.15, "pressure": 1010, ..氣壓 "humidity": 88, ..溼度 "temp_min": 299.15, ..溫度 "temp_max": 299.15, ..溫度 "sea_level":1019.43, "grnd_level":1001.39 }, "visibility": 10000, ..可見度 "wind": { "speed": 1 ..風速 }, "clouds": { "all": 75 ..雲朵 }, "dt": 1466942400, "sys": { "type": 1, "id": 7441, "message": 0.0098, "country": "CN", "sunrise": 1466889171, "sunset": 1466938716 }, "id": 1810821, "name": "Fuzhou", "cod": 200 }
大體是這樣,直接能夠用gsonformat插件自動生成類。
天氣預報裏面的數據來自和風天氣,數據由來自國內和國外,如下是國外的數據
{ "HeWeather data service 3.0": [ { "basic": { "city": "New York City", "cnty": "United States", "id": "US5128581", "lat": "40.714272", "lon": "-74.005966", "update": { "loc": "2015-07-14 23:44", "utc": "2015-07-15 03:46" } }, "status": "ok", "now": { "cond": { "code": "500", "txt": "Mist" }, "fl": "26", "hum": "89", "pcpn": "0.1", "pres": "1003", "tmp": "24", "vis": "10", "wind": { "deg": "120", "dir": "ESE", "sc": "1-2", "spd": "7" } }, "daily_forecast": [ { "date": "2015-07-14", "astro": { "sr": "05:37", "ss": "20:26" }, "cond": { "code_d": "103", "code_n": "305", "txt_d": "Partly Cloudy", "txt_n": "Light Rain" }, "hum": "59", "pcpn": "0.1", "pop": "43", "pres": "1002", "tmp": { "max": "31", "min": "22" }, "vis": "10", "wind": { "deg": "166", "dir": "SSE", "sc": "3-4", "spd": "7" } } ... ], "hourly_forecast": [ { "date": "2015-07-15 01:00", "hum": "83", "pop": "14", "pres": "1002", "tmp": "24", "wind": { "deg": "229", "dir": "SW", "sc": "3-4", "spd": "9" } } ... ] } ] }
裏面的數聽說明能夠查看官網的說明。
每次網絡請求的時候,將獲取到的數據直接使用gson轉化成對應的類。
其餘內容若是須要,之後再補充。
項目下載地址
http://www.oschina.net/code/snippet_2000932_58476
項目比較簡單,有疑問歡迎