Hello,這裡是振華OPPO!
一個愛運動、愛音樂、
愛敲代碼的研究生博主
1:項目概述本項目名稱叫做TodayHistory,
顧名思義,就是「歷史上的今天」。
點擊就能查看在這悠久的歷史長河裡面,
今天這個日期都發生了哪些大事件。
、
這個應用有著三點優勢:
一來可以培養人文情懷,
二來豐富自己的知識文化儲備,
三來以史為鑑知曉大義。
主要功能是怎麼實現的呢?
下面就一起來看下。
2:開發環境3:主要實現1、介紹下項目的總體結構
首先base包裡面放的是底層需要調用的類BaseActivity和UniteApp,裡面都是繼承父類和接口後重寫了方法。ContentURL就是我們調用的接口類,裡面是獲取URL的方法。
bean包都是實體集,HIstoryBean是【歷史上的今天】實體集,HIstoryDescBean是【詳細信息】實體集,LaoHuangliBean是【老黃曆】實體集,你可以把bean理解為資料庫裡面一張表的設計,就是一個類它所有的私有屬性以及get/set方法。
下面的HistoryActivity就是二級頁面的活動文件,HistoryAdapter是適配器文件,用來把從網絡上獲取的數據顯示到列表中,有列表那肯定有adapter,可以把它理解為容器,ListView裝上adapter就可以顯示了。
HistoryDescActivity是三級頁面的活動文件,它不是列表顯示,所以不需要adapter。最後就是MainActivity了,作為所有Android項目都有的活動文件,這裡它起到的還是列表的作用,從上到下展示內容,我們下面會講到。
2、獲取網絡數據的URL
這裡的key值是我自己申請的,如果你運行項目出現沒有數據的情況,應該是使用次數到達每日上限,方便起見,可以自己去【聚合數據】官網申請這兩個key,一個是【老黃曆】,還有一個是【歷史上的今天】,然後替換掉下面三個方法中對應的key值,第一個和第三個需要替換【歷史上的今天】的key值,第二個需要替換【老黃曆】的key值。
運行環境要保證電腦是聯網的,不然key值有用,也不會顯示數據。
3、一級界面的繪製
首先講activity_main,這個布局文件是一個RelativeLayout,它定義了一個ListView(列表)和一個ImageButton(圖片按鈕),ListView不是為了顯示一個個條目,而是將我們要顯示的界面分為頭部和尾部,依次放置在這個ListView中,這個圖標按鈕是用來點擊更改日曆的,之所以能顯示在這個列表右下方,是因為 android:layout_alignParentRight="true"和android:layout_alignParentBottom="true"這兩句設置的與父布局的位置關係,代碼如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">
<ListView android:id="@+id/main_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null"/> <ImageButton android:id="@+id/main_imgbtn" android:layout_width="70dp" android:layout_height="70dp" android:src="@mipmap/icon_calendar" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:background="@drawable/calendarbg" android:layout_margin="20dp"/></RelativeLayout>整體顯示效果如下:
圖片按鈕,我在drawable文件夾下定義了一個calendarbg.xml文件,更改了它的顯示效果。首先形狀設置成圓形(oval),顏色設置成淡紅色,然後添加上下左右的內邊距,對它的長度和高度也設置成了30dp,代碼如下:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:useLevel="false"> <solid android:color="#88B22222"/> <padding android:left="2dp" android:right="2dp" android:top="1dp" android:bottom="1dp"/> <size android:width="30dp" android:height="30dp"/></shape>接下來是main_headerview,就是主界面的頭部,這裡展示的就是【老黃曆】顯示的信息,設置的都是TextView,垂直居中分布,比較簡單,代碼如下:
<?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="match_parent" android:background="@color/mainBg"> <TextView android:id="@+id/main_header_tv_yangli" android:layout_width="match_parent" android:layout_height="wrap_content" android:textStyle="bold" android:textSize="18sp" android:gravity="center" android:padding="3dp" android:text=""/> <TextView android:id="@+id/main_header_tv_day" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textStyle="bold" android:textSize="80sp" android:text="" android:textColor="@color/fireRed"/> <TextView android:id="@+id/main_header_tv_week" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/fireRed" android:textStyle="bold" android:textSize="26sp" android:text=""/> <TextView android:id="@+id/main_header_tv_nongli" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="3dp" android:textStyle="bold" android:textSize="18sp" android:layout_marginTop="10dp" android:text=""/> <LinearLayout android:orientation="vertical" android:layout_margin="20dp" android:padding="10dp" android:background="@drawable/headerbg" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/main_header_tv_baiji" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textSize="14sp" android:text=""/> <TextView android:id="@+id/main_header_tv_wuxing" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:text=""/> <TextView android:id="@+id/main_header_tv_chongsha" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:layout_marginBottom="10dp" android:text=""/>
<TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/fireRed"/>
<TextView android:id="@+id/main_header_tv_jishen" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textSize="14sp" android:text=""/> <TextView android:id="@+id/main_header_tv_xiongshen" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:textSize="14sp" android:text=""/>
<TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/fireRed"/>
<TextView android:id="@+id/main_header_tv_yi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textSize="14sp" android:text=""/> <TextView android:id="@+id/main_header_tv_ji" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:textSize="14sp" android:text=""/> </LinearLayout>
<TextView android:id="@+id/main_header_tv_history" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="歷史上的這一天" android:layout_marginLeft="20dp" android:textSize="18sp" android:textStyle="bold"/></LinearLayout>具體效果:
最後就是main_footer這個布局文件,就是主界面的尾部,這個就是顯示一個TextView,用來點擊使用,然後加載更多信息的,最簡單的一個layout文件了,代碼如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" android:background="@color/mainBg"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="點擊加載更多" android:padding="15dp"/></LinearLayout>具體效果:
這樣,在MainActivity裡面就將頭部和尾部放到ListView中,共同構成了主界面
private void addHeaderAndFooterView() { View headerView = LayoutInflater.from(this).inflate(R.layout.main_headerview,null); initHeaderView(headerView); mainLv.addHeaderView(headerView); View footerView = LayoutInflater.from(this).inflate(R.layout.main_footer,null); footerView.setTag("footer"); footerView.setOnClickListener(this); mainLv.addFooterView(footerView); }4、二級界面的繪製
二級界面就是【歷史上的今天】,包含所有事件的一個界面。首先最上面在一個子RelativeLayout中放置了TextView,文本內容 android:text=「歷史上的這一天」,android:layout_centerInParent="true"居中顯示在父布局中。然後左側放置了一個ImageView,這是返回按鈕的圖片,用的是垂直居中,代碼如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="45dp" android:id="@+id/history_title" android:background="@color/mainBg"> <TextView android:id="@+id/history_title_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="歷史上的這一天" android:textSize="18sp" android:textStyle="bold" android:layout_centerInParent="true"/> <ImageView android:id="@+id/history_iv_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:src="@mipmap/icon_back"/> </RelativeLayout>
<TextView android:id="@+id/history_line" android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@id/history_title" android:background="@color/fireRed"/> <ListView android:id="@+id/history_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/history_line" android:divider="@null"></ListView> <TextView android:id="@+id/history_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textStyle="bold" android:textSize="30sp" android:text="暫無數據" android:visibility="gone"/></RelativeLayout>顯示效果:
有ListView肯定少不了item,而item_main_timeline就是這樣一個條目文件,它顯示了時間線圖片和文本,代碼如下:
<?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="match_parent" android:background="@color/mainBg"> <LinearLayout android:id="@+id/item_main_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginLeft="1dp" android:src="@mipmap/timeline_group_divider" /> <View android:layout_width="1dp" android:layout_height="30dp" android:layout_gravity="center_horizontal" android:layout_marginTop="2dp" android:background="#996600"/> </LinearLayout> <TextView android:id="@+id/item_main_time" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:layout_weight="15" android:text="2019-11-14" android:textColor="#996600"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="70dp" android:orientation="horizontal"> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="2dp" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/template_time_spot"/> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#996600" android:layout_marginTop="2dp"/> </LinearLayout> </RelativeLayout> <android.support.v7.widget.CardView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="15" android:background="#FFF" android:layout_marginRight="15dp" android:layout_marginBottom="10dp" android:elevation="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/item_main_pic" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:scaleType="centerCrop" android:src="@mipmap/icon"/> <TextView android:id="@+id/item_main_title" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:text="聯想集團公司在京成立" android:padding="5dp" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout></LinearLayout>顯示效果,可以看到時間是一個小圓,事件是一個小點,左邊圖片,右邊文字:
4、三級界面的繪製
最後就是activity_history_desc這個布局文件了,它和二級界面在頂部幾乎是一樣的,只是多了一個分享按鈕,主要是下面使用了ScrollView(垂直滾動條),用來放大量數據,防止數據顯示不完,可以上下滑動,就和我們瀏覽網頁一樣,這個滾動條中,我們放了一個LinearLayout子布局,在其中放了TextView控制項,顯示當天日期,下面就是ImageView控制項,顯示歷史上這件事的圖片,有可能有多張,所以可以滾動,最下面就是這個事件的詳細闡述,也是用一個TextView就搞定了,代碼如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/mainBg" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="45dp"> <TextView android:id="@+id/history_tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="歷史上的這一天" android:layout_centerInParent="true" android:textSize="18sp" android:textStyle="bold"/> <ImageView android:id="@+id/desc_back_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:src="@mipmap/icon_back"/>
<ImageView android:id="@+id/desc_share_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:src="@mipmap/icon_share" android:layout_alignParentRight="true" android:layout_marginRight="10dp"/> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/fireRed"/> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/desc_tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="18sp" android:textStyle="bold" android:layout_margin="10dp"/>
<ImageView android:layout_width="match_parent" android:layout_height="280dp" android:id="@+id/desc_iv_pic" android:scaleType="fitXY" android:layout_margin="10dp"/>
<TextView android:id="@+id/desc_tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" android:layout_margin="10dp"/> </LinearLayout> </ScrollView></LinearLayout>4:項目演示1、進入應用,主頁從上至下,依次顯示的是陽曆、陰曆、歷史上的這一天
2、我們往下拖,可以看到底下顯示了5條內容,點擊加載更多內容,就會進入到二級頁面,顯示更多內容
3、進入二級界面後,會顯示所有的事件,這些事件有的有圖片,有的沒有圖片,而且有的還有兩三張圖片,這主要根據網絡數據提供,最上面有【返回】按鈕,可以返回上一級頁面,標題是【歷史上的這一天】
4、我們點擊其中一個事件,跳轉到三級界面,可以看到它的詳細內容desc,標題+大圖+文字描述,非常好的用戶體驗
5、在事件的詳情頁面中,即上圖中右上角有個分享按鈕,點擊可以分享給好友
6、右下角有一個日曆,點擊它彈出日曆的對話框,可以選擇日期
7、我們選擇2021.06.01,然後發現日曆和歷史的今天都更新成了六月一號
5:項目總結以上就是該項目的主要內容介紹,考察的還是基礎控制項(ImageButton、ListView、TextView、ImageView)和布局(RelativeLayout、LinearLayout)的使用;還有每個項目必不可少的類:適配器Adapter、實體集Bean等,綜合運用,融會貫通,邊寫邊思考,才能加深記憶,寫起來行雲流水。
6:項目源碼後臺回覆:歷史上的今天
如果對你有幫助的話,點個「贊+在看」
歡迎大家轉發分享給身邊的同學~
這有你錯過的往期精彩~
Android Studio實現一個星座配對APP
Android Stduio實現一個天氣預報APP
Android Studio實現一個健康飲食搭配APP
珍藏四年的學習資料,開學季免費分享!
那我們下期再見嘍~