当前位置:   article > 正文

新闻列表小程序_编写android程序,在一个activity中展示两个fragment:一个用于展示新闻列表;当单

编写android程序,在一个activity中展示两个fragment:一个用于展示新闻列表;当单击

要求:

编写Android程序,在一个Activity中展示两个Fragment:一个用于展示新闻列表;当单击列表中的某一条新闻标题时,在另一个Fragment中显示新闻详情。

代码实现:

activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.example.maotingting.news_procedure.MainActivity">
  8. <FrameLayout
  9. android:id="@+id/frame_left"
  10. android:layout_width="0dp"
  11. android:layout_height="match_parent"
  12. android:layout_weight="1"/>
  13. <FrameLayout
  14. android:id="@+id/frame_right"
  15. android:layout_width="0dp"
  16. android:layout_height="match_parent"
  17. android:layout_weight="1" />
  18. </LinearLayout>

fragment_left.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:background="#FFCCCC"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="30dp"
  11. android:layout_gravity="center_horizontal"
  12. android:text="新闻热点"
  13. android:textColor="#3399FF"/>
  14. <Button
  15. android:id="@+id/button1"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:onClick="click1"
  19. android:textColor="#000000"
  20. android:background="#FFFFCC"
  21. android:layout_marginTop="20dp"
  22. android:text="#钟南山称想更快复工复学要继续保持距离#"/>
  23. <Button
  24. android:id="@+id/button2"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:onClick="click2"
  28. android:textColor="#000000"
  29. android:background="#FFFFCC"
  30. android:layout_marginTop="20dp"
  31. android:text="#最后一颗北斗三号组网卫星将于5月发射#"/>
  32. <Button
  33. android:id="@+id/button3"
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:onClick="click3"
  37. android:textColor="#000000"
  38. android:background="#FFFFCC"
  39. android:layout_marginTop="20dp"
  40. android:text="#与我国接壤国家疫情输入风险攀升#"/>
  41. <Button
  42. android:id="@+id/button4"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:onClick="click4"
  46. android:textColor="#000000"
  47. android:background="#FFFFCC"
  48. android:layout_marginTop="20dp"
  49. android:text="#美国一只老虎新冠病毒检测呈阳性#"/>
  50. <Button
  51. android:id="@+id/button5"
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:onClick="click5"
  55. android:textColor="#000000"
  56. android:background="#FFFFCC"
  57. android:layout_marginTop="20dp"
  58. android:text="#多省最后一批援鄂医疗队回家#"/>
  59. </LinearLayout>

fragment_right.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:background="#99FF99"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="30dp"
  11. android:layout_gravity="center_horizontal"
  12. android:text="详情介绍"
  13. android:textColor="#000000"/>
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:textSize="20dp"
  18. android:textColor="#000000"
  19. android:layout_marginTop="20dp"
  20. android:text="详细内容请点击,前往原文了解——>" />
  21. </LinearLayout>

fragment_right1.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:background="#CCFFFF"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="24dp"
  11. android:textColor="#FF6666"
  12. android:layout_gravity="center_horizontal"
  13. android:text="【#钟南山呼吁公众继续保持距离#】" />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:textSize="20dp"
  18. android:textColor="#000000"
  19. android:text=" 本周,中国工程院院士钟南山通过世界卫生组织,给大家提醒:不管是在居家、公司或者公共场所,保持一定的距离是非常重要。当前不要参加集会,也不要聚餐。低风险不等于无风险。随着返工复工的进行和学生的陆续复学,大众依然要高度注意个人卫生健康习惯。
  20. ——人民日报" />
  21. </LinearLayout>

fragment_right2.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:background="#CCFFFF"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="24dp"
  11. android:textColor="#ff6666"
  12. android:layout_gravity="center_horizontal"
  13. android:text="【#最后一颗北斗三号组网卫星将于5月发射#!】" />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:textSize="20dp"
  18. android:textColor="#000000"
  19. android:text=" 中国卫星导航系统管理办公室今天发布消息称,4月4日,第五十五颗北斗导航卫星,即#北斗三号最后一颗组网卫星#,如期运抵西昌卫星发射中心,这标志着全球星座组网进入最后冲刺阶段。这颗卫星属地球静止轨道卫星,将于西昌卫星发射中心开展测试、总装、加注等工作,计划于5月份发射。
  20. ——​​​​中国青年报" />
  21. </LinearLayout>

fragment_right3.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:background="#CCFFFF"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="24dp"
  11. android:textColor="#FF6666"
  12. android:layout_gravity="center_horizontal"
  13. android:text="【#与我国接壤国家疫情输入风险攀升#】" />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:textSize="20dp"
  18. android:textColor="#000000"
  19. android:text=" 6日,国家卫健委新闻发言人米锋表示,4月5日#湖北外本土重症病例降至9例#,湖北除武汉外重症病例降至3例。与我国接壤的边境国家疫情输入我国风险持续攀升,多省报告了境外输入病例的关联病例,防输入压力持续加大,要始终保持警惕,防范本土疫情反弹。
  20. ——人民日报" />
  21. </LinearLayout>

fragment_right4.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:background="#CCFFFF"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:textSize="24dp"
  11. android:textColor="#FF6666"
  12. android:layout_gravity="center_horizontal"
  13. android:text="【#美国一只老虎新冠病毒检测呈阳性#】" />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:textSize="20dp"
  18. android:textColor="#000000"
  19. android:text=" 【美国首次出现动物“中招”老虎病毒检测呈阳性】据彭博社报道,美国首次出现动物“中招”,纽约布朗克斯动物园一只4岁的老虎新冠病毒检测呈阳性。
  20. ——中国日报,LS" />
  21. </LinearLayout>

fragment_right5.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:background="#99FFFF"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:weightSum="1">
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:textSize="24dp"
  12. android:textColor="#FF6666"
  13. android:layout_gravity="center_horizontal"
  14. android:text="【英雄凯旋!#多省最后一批援鄂医疗队回家#】" />
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:textSize="20dp"
  19. android:textColor="#000000"
  20. android:text=" “你们冒着生命的危险,为湖北救了多少人!”近日,随着疫情防控形势好转,多省援鄂医疗队最后一批队员陆续踏上归程。他们在最冷的冬天出征,最最美的春天归来。他们用生命守护生命,#坚守战疫最后一班岗#,顺利凯旋!辛苦了白衣天使,请好好休养!
  21. ——人民日报"/>
  22. </LinearLayout>

LeftFragment.java:

  1. package com.example.********.news_procedure;
  2. import android.os.Bundle;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. /**
  10. * Created by ******** on 2020/4/7.
  11. */
  12. public class LeftFragment extends Fragment {
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. }
  17. @Override
  18. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  19. View view = inflater.inflate(R.layout.fragment_left , container ,false);
  20. return view;
  21. }
  22. @Override
  23. public void onPause() {
  24. super.onPause();
  25. }
  26. }

RightFragment.java:

  1. package com.example.*******.news_procedure;
  2. import android.os.Bundle;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. /**
  10. * Created by ******* on 2020/4/7.
  11. */
  12. public class RightFragment extends Fragment {
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. }
  17. @Override
  18. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  19. View view = inflater.inflate(R.layout.fragment_right , container ,false);
  20. return view;
  21. }
  22. @Override
  23. public void onPause() {
  24. super.onPause();
  25. }
  26. }

Right1Fragment.java:

  1. package com.example.********.news_procedure;
  2. import android.os.Bundle;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. /**
  10. * Created by ******** on 2020/4/7.
  11. */
  12. public class Right1Fragment extends Fragment {
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. }
  17. @Override
  18. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  19. View view = inflater.inflate(R.layout.fragment_right1, container ,false);
  20. return view;
  21. }
  22. @Override
  23. public void onPause() {
  24. super.onPause();
  25. }
  26. }

Right2Fragment.java:

  1. package com.example.********.news_procedure;
  2. import android.os.Bundle;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. /**
  10. * Created by ******** on 2020/4/7.
  11. */
  12. public class Right2Fragment extends Fragment {
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. }
  17. @Override
  18. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  19. View view = inflater.inflate(R.layout.fragment_right2, container ,false);
  20. return view;
  21. }
  22. @Override
  23. public void onPause() {
  24. super.onPause();
  25. }
  26. }

Right3Fragment.java:

  1. package com.example.********.news_procedure;
  2. import android.os.Bundle;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. /**
  10. * Created by ******** on 2020/4/7.
  11. */
  12. public class Right3Fragment extends Fragment {
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. }
  17. @Override
  18. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  19. View view = inflater.inflate(R.layout.fragment_right3, container ,false);
  20. return view;
  21. }
  22. @Override
  23. public void onPause() {
  24. super.onPause();
  25. }
  26. }

Right4Fragment.java:

  1. package com.example.********.news_procedure;
  2. import android.os.Bundle;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. /**
  10. * Created by ******** on 2020/4/7.
  11. */
  12. public class Right4Fragment extends Fragment {
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. }
  17. @Override
  18. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  19. View view = inflater.inflate(R.layout.fragment_right4, container ,false);
  20. return view;
  21. }
  22. @Override
  23. public void onPause() {
  24. super.onPause();
  25. }
  26. }

Right5Fragment.java:

  1. package com.example.********.news_procedure;
  2. import android.os.Bundle;
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. /**
  10. * Created by ******** on 2020/4/7.
  11. */
  12. public class Right5Fragment extends Fragment {
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. }
  17. @Override
  18. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  19. View view = inflater.inflate(R.layout.fragment_right5, container ,false);
  20. return view;
  21. }
  22. @Override
  23. public void onPause() {
  24. super.onPause();
  25. }
  26. }

MainActivity.java:

  1. package com.example.**********.news_procedure;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.support.v4.app.FragmentManager;
  4. import android.support.v4.app.FragmentTransaction;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.Window;
  8. import android.view.WindowManager;
  9. public class MainActivity extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. FragmentManager manager = getSupportFragmentManager();
  15. FragmentTransaction transaction = manager.beginTransaction();
  16. transaction.replace(R.id.frame_left, new LeftFragment());
  17. transaction.replace(R.id.frame_right, new RightFragment());
  18. transaction.commit();
  19. }
  20. public void click1(View view) {
  21. FragmentManager manager = getSupportFragmentManager();
  22. FragmentTransaction transaction = manager.beginTransaction();
  23. transaction.replace(R.id.frame_right, new Right1Fragment());
  24. transaction.commit();
  25. }
  26. public void click2(View view) {
  27. FragmentManager manager = getSupportFragmentManager();
  28. FragmentTransaction transaction = manager.beginTransaction();
  29. transaction.replace(R.id.frame_right, new Right2Fragment());
  30. transaction.commit();
  31. }
  32. public void click3(View view) {
  33. FragmentManager manager = getSupportFragmentManager();
  34. FragmentTransaction transaction = manager.beginTransaction();
  35. transaction.replace(R.id.frame_right, new Right3Fragment());
  36. transaction.commit();
  37. }
  38. public void click4(View view) {
  39. FragmentManager manager = getSupportFragmentManager();
  40. FragmentTransaction transaction = manager.beginTransaction();
  41. transaction.replace(R.id.frame_right, new Right4Fragment());
  42. transaction.commit();
  43. }
  44. public void click5(View view) {
  45. FragmentManager manager = getSupportFragmentManager();
  46. FragmentTransaction transaction = manager.beginTransaction();
  47. transaction.replace(R.id.frame_right, new Right5Fragment());
  48. transaction.commit();
  49. }
  50. }

运行结果显示:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/355327?site
推荐阅读
相关标签