Android開發(1) | Fragment 的應用——新聞應用

文章目錄

  • Item:標題子項
    • 布局文件
    • Java代碼
  • 標題碎片
    • 布局文件
    • Java代碼
  • 新聞內容碎片
    • 布局文件
    • Java代碼
  • 新聞內容活動
    • 布局文件
    • Java代碼
  • 首界面
    • 布局文件
    • Java代碼


Item:標題子項

布局文件

在這里插入圖片描述

news_item.xml

<TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/news_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:lines="1"android:ellipsize="end"android:textSize="18sp"android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="15dp"android:paddingBottom="15dp"></TextView>
  • padding:給控件周圍加上補白,避免文本內容緊靠邊緣
  • lines:規定文本內容只能單行顯示
  • ellipsize:設定當文本內容超出控件寬度時,文本的略縮方式

Java代碼

public class News {private String title; // 標題private String content; // 內容public void setTitle(String title) {this.title = title;}public String getTitle() {return title;}public void setContent(String content) {this.content = content;}public String getContent() {return content;}
}

標題碎片

布局文件

在這里插入圖片描述

news_title_frag.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/news_title_recycler_view"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>

Java代碼

public class NewsTitleFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.news_title_frag, container, false);// 滾動控件實例RecyclerView newsTitleRecyclerView = view.findViewById(R.id.news_title_recycler_view);// 布局方式LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());// 設置RecyclerView布局方式為線性布局newsTitleRecyclerView.setLayoutManager(layoutManager);// 為布局添加適配器newsTitleRecyclerView.setAdapter(new NewsAdapter(getNews()));return view;}// 創建一個List<News>并返回private List<News> getNews(){List<News> newsList = new ArrayList<>();for(int i=1; i<=50; i++){News news = new News();news.setTitle("News Title" + i);news.setContent(getRandomLengthContent("News Content"+i+"."));newsList.add(news);}return newsList;}// 隨機生成新聞內容private String getRandomLengthContent(String content){Random random = new Random();int length = random.nextInt(20)+1;StringBuilder builder = new StringBuilder();for(int i=0; i<length; i++){builder.append(content);}return  builder.toString();}// 以內部類形式實現自定義適配器class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{private List<News> NewsList;class ViewHolder extends RecyclerView.ViewHolder {TextView newsTitleText;public ViewHolder(View itemView) {super(itemView);newsTitleText = itemView.findViewById(R.id.news_title);}}public NewsAdapter(List<News> newsList){NewsList = newsList;}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);final ViewHolder holder = new ViewHolder(view);// 點擊RecyclerView中的新聞標題時,啟動NewsContentActiviryview.setOnClickListener((View v)->{// 獲取點擊項的News實例News news = NewsList.get(holder.getAdapterPosition());/* 單頁模式 */// 啟動新的活動顯示新聞內容NewsContentActiviry.actionStart(getActivity(), news.getTitle(), news.getContent());/* 雙頁模式 *//*NewsContentFragment newsContentFragment = (NewsContentFragment) getParentFragmentManager().findFragmentById(R.id.news_content_fragment);newsContentFragment.refresh(news.getTitle(), news.getContent());*/});return holder;}@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {News news = NewsList.get(position);holder.newsTitleText.setText(news.getTitle());}@Overridepublic int getItemCount() {return NewsList.size();}}
}

新聞內容碎片

布局文件

news_content_frag.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/visible_layout"android:orientation="vertical"android:visibility="invisible"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/news_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="20sp"android:padding="10dp"/><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#000"/><TextViewandroid:id="@+id/news_content"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:textSize="18sp"android:padding="15dp"/></LinearLayout><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:background="@color/black" /></RelativeLayout>
  • 內嵌的 LinearLayout 布局用于管理新聞內容碎片的布局。
  • 兩個 TextView 分別表示新聞標題新聞內容
  • 兩個 View 是兩條黑色分割線,第一個用于在新聞內容碎片中分割新聞標題新聞內容;第二個用于在雙頁模式下分割新聞內容碎片標題碎片
  • 外層使用 RelativeLayout 布局是為了便于布置第二個 View。(android:layout_alignParentLeft 屬性)

單頁模式:
在這里插入圖片描述

雙頁模式:

PS:豎直黑色分割線左側屬于標題碎片,跟新聞內容碎片無關。
在這里插入圖片描述


Java代碼

public class NewsContentFragment extends Fragment {private View view;// 加載布局@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.news_content_frag, container, false);return view;}// 將新聞標題和內容顯示到界面上public void refresh(String newTitle, String newContent){View view1 = view.findViewById(R.id.visible_layout);view1.setVisibility(View.VISIBLE);TextView title = view.findViewById(R.id.news_title);TextView content = view.findViewById(R.id.news_content);title.setText(newTitle); // 刷新新聞標題content.setText(newContent); // 刷新新聞內容}
}

新聞內容活動

布局文件

在單頁模式下,點擊標題碎片中的標題子項時,會根據點擊的新聞標題跳轉到具體的新聞內容活動

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/news_content_fragment"android:layout_width="match_parent"android:layout_height="match_parent"android:name="com.example.activitytest.Fragment.NewsContentFragment"/></LinearLayout>

Java代碼

public class NewsContentActiviry extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView((R.layout.news_content));String newsTitle = getIntent().getStringExtra("news_title");String newsContent = getIntent().getStringExtra("news_content");NewsContentFragment newsContentFragment = (NewsContentFragment) getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);newsContentFragment.refresh(newsTitle, newsContent); // 刷新NewsContentFragment界面}public static void actionStart(Context context, String newsTitle, String newsContent){Intent intent = new Intent(context, NewsContentActiviry.class);intent.putExtra("news_title", newsTitle);intent.putExtra("news_content", newsContent);context.startActivity(intent);}
}

有關 actionStart 詳見本文


首界面

布局文件

news_layout.xml :注釋部分解除之后即從單頁布局變為雙頁布局

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/news_title_fragment"android:name="com.example.activitytest.Fragment.NewsTitleFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><!--<FrameLayoutandroid:id="@+id/news_content_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="3"><fragmentandroid:id="@+id/news_content_fragment"android:name="com.example.activitytest.Fragment.NewsContentFragment"android:layout_width="match_parent"android:layout_height="match_parent"/></FrameLayout>-->
</LinearLayout>

單頁布局:
在這里插入圖片描述
雙頁布局:
在這里插入圖片描述


Java代碼

public class NewsActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.news_layout);}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/443704.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/443704.shtml
英文地址,請注明出處:http://en.pswp.cn/news/443704.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java Web整體異常處理

在實際的J2EE項目中&#xff0c;系統內部難免會出現一些異常&#xff0c;就如StrutsSpringHibernate項目&#xff1a;通常一個頁面請求到達后臺以后&#xff0c;首先是到action&#xff08;就是MVC中的controller&#xff09;&#xff0c;在action層會調用業務邏輯層service&am…

Android入門(11)| 全局廣播與本地廣播

文章目錄廣播概念接收廣播動態注冊實例靜態注冊實例發送廣播發送標準廣播廣播的跨進程特性發送有序廣播本地廣播廣播概念 Android 中的每個應用程序都可以對自己感興趣的廣播進行注冊&#xff0c;這樣該程序就只會接收到自己所關心的廣播內容&#xff0c;這些廣播可能是來自系…

Android開發(2) | 廣播 Broadcast 的應用——強制下線功能

文章目錄功能簡介關閉所有活動登陸界面發送強制下線的廣播廣播接收器AndroidManifest.xml運行結果功能簡介 強制下線功能只需要彈出一個對話框&#xff0c;讓用戶只能點擊確定按鈕&#xff0c;回到登錄界面。 如果在每一個活動中添加一個對話框的話太過繁瑣&#xff0c;用廣播…

Android入門(12)| 數據持久化

文章目錄數據持久化文件存儲將數據存儲進文件實例從文件中讀取數據實例SharedPreferences存儲將數據存儲進文件實例從文件中讀取數據實例實現記住密碼的功能SQLite數據庫存儲創建自己的幫助類調用自己的幫助類補全 onUpgrade() 方法增刪查改增&#xff1a;SQLiteDatabase.inser…

Android入門(13)| Android權限 與 內容提供器

文章目錄普通權限與危險權限運行時申請權限內容提供器運用安卓封裝好的內容提供器自實現的內容提供器概念實現普通權限與危險權限 主要用于不同應用程序之間在保證被訪數據的安全性的基礎上&#xff0c;實現數據共享的功能。 在 Android 6.0 開始引入了運行時權限的功能&…

Java實現身份證號碼的驗證,JAVA后臺驗證身份證號碼

代碼如下&#xff1a; package cn.gov.csrc.util;/*** 18 位身份證驗證器* * author admin* */ public class IDCard {final int[] wi { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };final int[] vi { 1, 0, X, 9, 8, 7, 6, 5, 4, 3, 2 };private int[] ai n…

Android入門(14)| 通知

文章目錄創建通知點擊效果其它小功能實例創建通知 創建通知的步驟&#xff1a; 管理通知的 NotificationManager&#xff0c;通常通過當前 Context 的 getSystemService() 獲取實例。它接受一個字符串參數用于確定獲取系統的什么服務。Android 8.0(O) 版本后需要通知通道&…

Android開發(3) | 權限和內容提供器的應用——調用相機和相冊

文章目錄拍照并保存到 ImageView 控件布局文件 notice_layout.xml按鈕 button_takePhoto 的點擊操作隱式 Intent 啟動后的回調AndroidManifest.xml從相冊選取照片并在 ImageView 控件中顯示布局文件 notice_layout.xml按鈕 button_takePhoto 的點擊操作自定義打開相冊的方法 op…

Android開發(4) | 系統權限、MediaPlayer類 和 VideoView類 的應用——播放多媒體文件

文章目錄MediaPlayer類播放音頻的實例VideoView類播放視頻的實例MediaPlayer類 對多種格式的音頻文件提供了全面的控制方法&#xff1a; 如何獲得MediaPlayer實例&#xff1f; 通過構造函數&#xff1a; MediaPlayer mp new MediaPlayer();調用 MediaPlayer.create() 方法&…

Android入門(15)| 網絡

文章目錄WebViewHTTP使用HttpURLConnection使用OkHttp封裝網絡操作封裝HttpURLConnection封裝OkHttpWebView WebView 可以在 應用程序中&#xff08;而不是瀏覽器&#xff09; 展示一些網頁。 布局文件 web_layout.xml&#xff1a; <LinearLayoutxmlns:android"http…

Java-單例模式

單例模式相信大家都不陌生&#xff0c;在JAVAEE應用中&#xff0c;單例模式是一種應用非常廣泛的設計模式&#xff0c;應用中許多組件都只需要單個實例&#xff0c;下面介紹單例模式。 使用單例模式的優點&#xff1a; 1.減少創建JAVA實例所帶來的系統開銷。 2.便于系統跟蹤單…

Android入門(16)| 服務

文章目錄概念Android 多線程繼承 Thread繼承 Runable 接口匿名類異步消息處理AsyncTask使用服務框架啟動/停止服務綁定/解綁服務服務的生命周期前臺服務IntentService完整版下載示例下載過程的回調接口&#xff1a;DownloadListener繼承 AsyncTask 實現下載功能&#xff1a;Dow…

2020德勤面試開始了嗎_2020國考面試開始,近期面試公告匯總,附結構化小組面試流程...

2020年國家公務員考試面試環節逐步恢復考試&#xff0c;各個招錄部門已經發布面試考察公告&#xff0c;對于進入面試環節的國考考生來說&#xff0c;有必要了解近期國考面試的招錄動態&#xff0c;提前做好面試準備。2020國考國家統計局機關面試面試確認&#xff1a;請進入面試…

項目積壓需求項目計劃_需求變更頻繁,項目經理如何做好需求管理?

項目實施過程中&#xff0c;項目經理常常面臨一個重大挑戰——需求變更。需求變更無處不在&#xff0c;市場條件變化、新業務出現、戰略目標調整、客戶需求修改、資源限制等&#xff0c;都會造成需求變更。需求變更會影響項目的時間、成本和質量&#xff0c;對整個項目和團隊成…

Android | Sensor.TYPE_ORIENTATION被廢棄后的解決辦法

文章目錄概述getOrientation 方法根據 旋轉矩陣R 獲取 設備旋轉弧度getRotationMatrix 方法根據 地磁場、加速度傳感器對象 獲取 旋轉矩陣R代碼參考資料概述 Sensor.TYPE_ORIENTATION 常數在 API 8 中已棄用&#xff0c;官方推薦使用 SensorManager.getOrientation() 替代。關…

【JAVA 開發小問題】 | String操作合集

文章目錄截取特定兩個字符之間的字符串截取特定兩個字符之間的字符串 利用正則表達式&#xff0c;圖片來源

uniapp 刷新后數據都沒有了_環境溫度傳感器都沒有連接,竟然還會有數據?

福田歐曼GTL(福康發動機、康明斯2880系統)匹配ECoffit尿素泵●故障現象&#xff1a;OBD故障燈點亮&#xff0c;不燒尿素&#xff0c;油耗高&#xff0c;動力不足●故障碼&#xff1a;●維修分析&#xff1a;①故障指出加熱器問題&#xff0c;摸下尿素箱溫度&#xff0c;發現燙手…

Android | 再探 RecyclerView 之名詞解析

文章目錄Adapter、ViewHolderchild viewLayoutManagerRecyclerScrapDirtyIndexPositionlayout position 和 adapter position四級緩存瀏覽本文前推薦先閱讀 Android入門&#xff08;九&#xff09;| 滾動控件 ListView 與 RecyclerView Adapter、ViewHolder Adapter: A subcla…

Linux學習:第一章-Linux簡介

一 UNIX發展史1 1965年&#xff0c;美國麻省理工學院&#xff08;MIT&#xff09;、通用電氣公司&#xff08;GE&#xff09;及AT&T的貝爾實驗室聯合開發Multics工程計劃&#xff0c;其目標是開發一種交互式的具有多道程序處理能力的分時操作系統&#xff0c;但因Multics追…

尼爾機器人技能快捷鍵_《尼爾機械紀元》連招操作技巧

《尼爾機械紀元》中的每個角色都可以裝備一個輕武器和一個重武器&#xff0c;技能招式也很豐富&#xff0c;下面為大家帶來了《尼爾機械紀元》連招操作技巧&#xff0c;希望對你們有所幫助。連擊技能展示視頻視頻原址&#xff1a;點擊進入基本沒有什么太難的連段&#xff0c;只…