Android實(shí)現(xiàn)基本功能的新聞應(yīng)用
先準(zhǔn)備好一個(gè)新聞實(shí)體類(lèi)
package com.zb.fragmentbestpractice /** * title:表示新聞的實(shí)體類(lèi) * content:表示新聞的內(nèi)容 */ class News(val title: String, val content: String) { }
新建布局文件news_content_frag.xml,作為新聞布局的內(nèi)容
<?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"> <LinearLayout android:id="@+id/contentLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:visibility="invisible"> <TextView android:id="@+id/newsTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000000" /> <TextView android:id="@+id/newsContent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="15dp" android:textSize="18sp" /> </LinearLayout> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:background="#000000" /> </RelativeLayout>
- 新聞布局主要分為兩個(gè)部分:頭部部分顯示新聞標(biāo)題,正文部分顯示內(nèi)容,中間使用一條水平方向的細(xì)線進(jìn)行隔開(kāi),除此之外還有一條豎直防線的細(xì)線,它的作用是在雙頁(yè)模式下將左側(cè)新聞列表和右側(cè)新聞的內(nèi)容進(jìn)行分隔開(kāi).
- 我們還需要將新聞內(nèi)容的布局設(shè)置成為不可見(jiàn),因?yàn)樵陔p頁(yè)模式下,如果還沒(méi)有選中新聞列表中的任何一條新聞,是不應(yīng)該顯示新聞內(nèi)容布局的.
- 接下來(lái)新建一個(gè)NewsContentFragment類(lèi)
class NewsContentFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.news_content_frag, container, false) } /** * 該方法用于將新聞的標(biāo)題和內(nèi)容顯示在我們剛剛定義的界面上, * 當(dāng)調(diào)用了refresh方法時(shí),需要將我們剛才隱藏的新聞內(nèi)容布局設(shè)置成為可見(jiàn) */ fun refresh(title: String, content: String) { //將布局設(shè)置成可見(jiàn) contentLayout.visibility = View.VISIBLE //設(shè)置新聞標(biāo)題內(nèi)容 newsTitle.text = title //設(shè)置新聞內(nèi)容 newsContent.text = content } }
- 在onCreatView()方法中加載了我們剛剛創(chuàng)建的布局
- 這樣就把新聞內(nèi)容的Fragment和布局創(chuàng)建好了,但是它們都是在雙頁(yè)模式當(dāng)中使用的,如果想在單頁(yè)模式中使用,我們還需要?jiǎng)?chuàng)建一個(gè)Activity
- 創(chuàng)建一個(gè)NewsContentActivity
<?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="match_parent" android:orientation="vertical"> <fragment android:id="@+id/newsContentFrag" android:name="com.zb.fragmentbestpractice.NewsContentFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
- 在這個(gè)地方發(fā)揮了代碼的復(fù)用性,直接在布局中引入了NewsContentFragment.這樣相當(dāng)于把news_content_frag布局的內(nèi)容自動(dòng)加了進(jìn)來(lái)
- 修改NewsContentActivity中的代碼
class NewsContentActivity : AppCompatActivity() { companion object { fun actionStart(context: Context, title: String, content: String) { val intent = Intent(context, NewsContentActivity::class.java).apply { putExtra("news title", title) putExtra("news content", content) } context.startActivity(intent) } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_news_content) val title = intent.getStringExtra("news title")//獲取傳入的新聞標(biāo)題 val content = intent.getStringExtra("news content")//獲取傳入的新聞內(nèi)容 if (title != null && content!= null) { val fragment = newsContentFrag as NewsContentFragment fragment.refresh(title, content) //刷新NewsContentFragment 界面 } } }
- onCreate()方法當(dāng)中,我們通過(guò)Intent獲取到了傳入的新聞標(biāo)題和新聞內(nèi)容,然后獲取NewsContentFragment實(shí)例,接著調(diào)用它的refres()方法,將新聞標(biāo)題和內(nèi)容傳入,就可以把這些數(shù)據(jù)顯示出來(lái)了
- 接下來(lái)創(chuàng)建一個(gè)用于顯示新聞列表的布局,新建news_title_frag.xml
<?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="match_parent" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/newsTitleRecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
該布局比較簡(jiǎn)單,里面只有一個(gè)用于顯示新聞列表的RecyclerView,既然要用到RecyclerView就要編寫(xiě)子項(xiàng)的布局,新建news_item.xml作為RecyclerView子項(xiàng)的布局
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/newsTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" android:ellipsize="end" android:textSize="18sp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="15dp" android:paddingBottom="15dp"> </TextView>
- 子項(xiàng)布局也比較簡(jiǎn)單,只有一個(gè)TextView
- 其中android:padding表示給空間周?chē)由涎a(bǔ)白,這樣不至于讓文本內(nèi)容,緊靠在邊緣上
- android:ellipsize用于設(shè)定當(dāng)文本內(nèi)容超出控件寬度時(shí)的縮略方式,這里指定成為end表示在尾部進(jìn)行省略
- 新聞列表和子布局都創(chuàng)建好了,現(xiàn)在需要一個(gè)用于展示新聞列表的地方,這里新建NewsTitleFragment作為列表的Fragment
class NewsTitleFragment : Fragment() { private var isTwoPane = false override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.news_title_frag, container, false) } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) isTwoPane = activity?.findViewById<View>(R.id.newsContent.newsContentLayout) != null } }
- 其中的onActivityCreated()方法通過(guò)在Activity中能否找到一個(gè)id為newsContentLayout的View,來(lái)判斷當(dāng)前是雙頁(yè)模式還是單頁(yè)模式
- 因此我們需要讓這個(gè)id為newsContentLayout的View旨在雙頁(yè)模式當(dāng)中才會(huì)出現(xiàn).
- 使用限定符實(shí)現(xiàn)id為newsContentLayout的View只在雙頁(yè)模式當(dāng)中才能出現(xiàn).
- 修改activity_main.xml中的代碼
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/newsTitleLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/newsTitleFrag" android:name="com.zb.fragmentbestpractice.NewsTitleFragment" android:layout_width="match_parent" android:layout_height="match_parent" </FrameLayout>
- 上述代碼表示只會(huì)在單頁(yè)模式下加載一個(gè)新聞標(biāo)題的Fragment
- 然后新建一個(gè)layout-sw600dp文件夾,在這個(gè)文件夾下面創(chuàng)建一個(gè)activity_main.xml文件,代碼如下
<?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="match_parent" android:orientation="horizontal"> <fragment android:id="@+id/newsTitleFrag" android:name="com.zb.fragmentbestpractice.NewsTitleFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/newsContentLayout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3"> <fragment android:id="@+id/newsContentFrag" android:name="com.zb.fragmentbestpractice.NewsContentFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> </LinearLayout>
- 可以看出在雙頁(yè)模式下面,同時(shí)引入了兩個(gè)fragment,并將新聞內(nèi)容的fragment放在了Fragment的布局下面,這個(gè)Fragment布局id為newsContentFrag
- 因此只要能夠找到這個(gè)布局的id就說(shuō)明是雙頁(yè)模式,反之來(lái)說(shuō)就是單頁(yè)模式.
- 接下來(lái)就是在NewsTitleFragment中通過(guò)RecyclerView將新聞列表展示出來(lái)
- 在NewsTitleFragment中新建一個(gè)內(nèi)部類(lèi)NewsAdapter,來(lái)作為RecyclerView的適配器
package com.zb.fragmentbestpractice import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.activity_news_content.* import kotlinx.android.synthetic.main.news_item.view.* import kotlinx.android.synthetic.main.news_title_frag.* /** * 用于展示新聞列表 */ class NewsTitleFragment : Fragment() { private var isTwoPane = false override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.news_title_frag, container, false) } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) isTwoPane = activity?.findViewById<View>(R.id.newsContentLayout) != null val layoutManager = LinearLayoutManager(activity) newsTitleRecyclerView.layoutManager = layoutManager val adapter = NewsAdapter(getNews()) newsTitleRecyclerView.adapter = adapter } private fun getNews(): List<News> { val newsList = ArrayList<News>() for (i in 1..50) { val news = News("This is news title $i", getRandomLengthString("This is news content $i.")) newsList.add(news) } return newsList } private fun getRandomLengthString(str: String): String { val n = (1..20).random() val builder = StringBuilder() repeat(n) { builder.append(str) } return builder.toString() } /** * 內(nèi)部類(lèi),用來(lái)作為RecyclerView的適配器 */ inner class NewsAdapter(val newsList: List<News>) : RecyclerView.Adapter<NewsAdapter.ViewHolder>() { inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val newsTitle: TextView = view.newsTitle } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.news_item, parent, false) val holder = ViewHolder(view) holder.itemView.setOnClickListener { //在newsList當(dāng)中先獲取news實(shí)例 val news = newsList[holder.adapterPosition] //根據(jù)isTwoPane判斷是單頁(yè)模式還是雙頁(yè)模式 if (isTwoPane) { //如果是雙頁(yè)模式,則刷新newsContentFragment中的內(nèi)容 val fragment = newsContentFrag as NewsContentFragment fragment.refresh(news.title, news.content) } else { //如果是單頁(yè)模式,則直接啟動(dòng)NewsContentActivity NewsContentActivity.actionStart(parent.context, news.title, news.content) } } return holder } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val news = newsList[position] holder.newsTitle.text = news.title } override fun getItemCount(): Int { return newsList.size } } }
- 最后一步收尾工作,向RecyclerView當(dāng)中添加數(shù)據(jù)
- 修改NewsTitleFragment中的代碼
到此這篇關(guān)于Android實(shí)現(xiàn)基本功能的新聞應(yīng)用的文章就介紹到這了,更多相關(guān)Android新聞應(yīng)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android實(shí)現(xiàn)雅虎新聞?wù)虞d視差動(dòng)畫(huà)效果
- Android仿網(wǎng)易新聞圖片詳情下滑隱藏效果示例代碼
- Android開(kāi)發(fā)實(shí)現(xiàn)自定義新聞加載頁(yè)面功能實(shí)例
- Android UI設(shè)計(jì)與開(kāi)發(fā)之PopupWindow仿騰訊新聞底部彈出菜單
- Android RecyclerView仿新聞?lì)^條的頻道管理功能
- Android網(wǎng)絡(luò)編程之簡(jiǎn)易新聞客戶端
- Android模擬實(shí)現(xiàn)網(wǎng)易新聞客戶端
- Android 模擬新聞APP顯示界面滑動(dòng)優(yōu)化實(shí)例代碼
相關(guān)文章
關(guān)于Android中ListView嵌套GridView的問(wèn)題
在Android開(kāi)發(fā)的過(guò)程中可能需要用到listview嵌套gridview的場(chǎng)景,但是在嵌套過(guò)程中也許會(huì)遇到問(wèn)題,我們下面一起來(lái)看看是什么問(wèn)題以及如何解決。2016-08-08Android PopupWindow使用方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android實(shí)現(xiàn)創(chuàng)意LoadingView動(dòng)畫(huà)效果
這篇文章主要介紹了Android實(shí)現(xiàn)創(chuàng)意LoadingView動(dòng)畫(huà)效果的相關(guān)資料,需要的朋友可以參考下2016-02-02Android環(huán)形進(jìn)度條(安卓默認(rèn)形式)實(shí)例代碼
這篇文章主要介紹了Android環(huán)形進(jìn)度條(安卓默認(rèn)形式)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-03-03Flutter如何輕松實(shí)現(xiàn)動(dòng)態(tài)更新ListView淺析
在Android中通常都會(huì)用到listview.那么flutter里面怎么用呢?下面這篇文章主要給大家介紹了關(guān)于Flutter如何輕松實(shí)現(xiàn)動(dòng)態(tài)更新ListView的相關(guān)資料,需要的朋友可以參考下2022-02-02Rxjava2_Flowable_Sqlite_Android數(shù)據(jù)庫(kù)訪問(wèn)實(shí)例
下面小編就為大家分享一篇Rxjava2_Flowable_Sqlite_Android數(shù)據(jù)庫(kù)訪問(wèn)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Android Studio 實(shí)現(xiàn)將support庫(kù)改成Androidx
這篇文章主要介紹了Android Studio 實(shí)現(xiàn)將support庫(kù)改成Androidx,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Flutter檢查連接網(wǎng)絡(luò)connectivity_plus實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了Flutter檢查連接網(wǎng)絡(luò)connectivity_plus實(shí)現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android基于socket實(shí)現(xiàn)的簡(jiǎn)單C/S聊天通信功能
這篇文章主要介紹了Android基于socket實(shí)現(xiàn)的簡(jiǎn)單C/S聊天通信功能,結(jié)合實(shí)例形式分析了Android使用socket實(shí)現(xiàn)客服端與服務(wù)器端數(shù)據(jù)的發(fā)送與接收處理技巧,需要的朋友可以參考下2016-10-10