赞
踩
要求:
编写Android程序,在一个Activity中展示两个Fragment:一个用于展示新闻列表;当单击列表中的某一条新闻标题时,在另一个Fragment中显示新闻详情。
代码实现:
activity_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.maotingting.news_procedure.MainActivity">
-
- <FrameLayout
- android:id="@+id/frame_left"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"/>
- <FrameLayout
- android:id="@+id/frame_right"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1" />
- </LinearLayout>
fragment_left.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#FFCCCC"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="30dp"
- android:layout_gravity="center_horizontal"
- android:text="新闻热点"
- android:textColor="#3399FF"/>
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="click1"
- android:textColor="#000000"
- android:background="#FFFFCC"
- android:layout_marginTop="20dp"
- android:text="#钟南山称想更快复工复学要继续保持距离#"/>
- <Button
- android:id="@+id/button2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="click2"
- android:textColor="#000000"
- android:background="#FFFFCC"
- android:layout_marginTop="20dp"
- android:text="#最后一颗北斗三号组网卫星将于5月发射#"/>
- <Button
- android:id="@+id/button3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="click3"
- android:textColor="#000000"
- android:background="#FFFFCC"
- android:layout_marginTop="20dp"
- android:text="#与我国接壤国家疫情输入风险攀升#"/>
- <Button
- android:id="@+id/button4"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="click4"
- android:textColor="#000000"
- android:background="#FFFFCC"
- android:layout_marginTop="20dp"
- android:text="#美国一只老虎新冠病毒检测呈阳性#"/>
- <Button
- android:id="@+id/button5"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="click5"
- android:textColor="#000000"
- android:background="#FFFFCC"
- android:layout_marginTop="20dp"
- android:text="#多省最后一批援鄂医疗队回家#"/>
- </LinearLayout>
fragment_right.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#99FF99"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="30dp"
- android:layout_gravity="center_horizontal"
- android:text="详情介绍"
- android:textColor="#000000"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:textColor="#000000"
- android:layout_marginTop="20dp"
- android:text="详细内容请点击,前往原文了解——>" />
- </LinearLayout>
fragment_right1.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#CCFFFF"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="24dp"
- android:textColor="#FF6666"
- android:layout_gravity="center_horizontal"
- android:text="【#钟南山呼吁公众继续保持距离#】" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:textColor="#000000"
- android:text=" 本周,中国工程院院士钟南山通过世界卫生组织,给大家提醒:不管是在居家、公司或者公共场所,保持一定的距离是非常重要。当前不要参加集会,也不要聚餐。低风险不等于无风险。随着返工复工的进行和学生的陆续复学,大众依然要高度注意个人卫生健康习惯。
- ——人民日报" />
- </LinearLayout>
fragment_right2.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#CCFFFF"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="24dp"
- android:textColor="#ff6666"
- android:layout_gravity="center_horizontal"
- android:text="【#最后一颗北斗三号组网卫星将于5月发射#!】" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:textColor="#000000"
- android:text=" 中国卫星导航系统管理办公室今天发布消息称,4月4日,第五十五颗北斗导航卫星,即#北斗三号最后一颗组网卫星#,如期运抵西昌卫星发射中心,这标志着全球星座组网进入最后冲刺阶段。这颗卫星属地球静止轨道卫星,将于西昌卫星发射中心开展测试、总装、加注等工作,计划于5月份发射。
- ——中国青年报" />
- </LinearLayout>
fragment_right3.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#CCFFFF"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="24dp"
- android:textColor="#FF6666"
- android:layout_gravity="center_horizontal"
- android:text="【#与我国接壤国家疫情输入风险攀升#】" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:textColor="#000000"
- android:text=" 6日,国家卫健委新闻发言人米锋表示,4月5日#湖北外本土重症病例降至9例#,湖北除武汉外重症病例降至3例。与我国接壤的边境国家疫情输入我国风险持续攀升,多省报告了境外输入病例的关联病例,防输入压力持续加大,要始终保持警惕,防范本土疫情反弹。
- ——人民日报" />
- </LinearLayout>
fragment_right4.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#CCFFFF"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="24dp"
- android:textColor="#FF6666"
- android:layout_gravity="center_horizontal"
- android:text="【#美国一只老虎新冠病毒检测呈阳性#】" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:textColor="#000000"
- android:text=" 【美国首次出现动物“中招”老虎病毒检测呈阳性】据彭博社报道,美国首次出现动物“中招”,纽约布朗克斯动物园一只4岁的老虎新冠病毒检测呈阳性。
- ——中国日报,LS" />
- </LinearLayout>
fragment_right5.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:background="#99FFFF"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:weightSum="1">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="24dp"
- android:textColor="#FF6666"
- android:layout_gravity="center_horizontal"
- android:text="【英雄凯旋!#多省最后一批援鄂医疗队回家#】" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:textColor="#000000"
- android:text=" “你们冒着生命的危险,为湖北救了多少人!”近日,随着疫情防控形势好转,多省援鄂医疗队最后一批队员陆续踏上归程。他们在最冷的冬天出征,最最美的春天归来。他们用生命守护生命,#坚守战疫最后一班岗#,顺利凯旋!辛苦了白衣天使,请好好休养!
- ——人民日报"/>
-
- </LinearLayout>
LeftFragment.java:
- package com.example.********.news_procedure;
-
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- /**
- * Created by ******** on 2020/4/7.
- */
-
- public class LeftFragment extends Fragment {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_left , container ,false);
- return view;
- }
- @Override
- public void onPause() {
- super.onPause();
- }
- }
RightFragment.java:
- package com.example.*******.news_procedure;
-
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- /**
- * Created by ******* on 2020/4/7.
- */
-
- public class RightFragment extends Fragment {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_right , container ,false);
- return view;
- }
- @Override
- public void onPause() {
- super.onPause();
- }
- }
Right1Fragment.java:
- package com.example.********.news_procedure;
-
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- /**
- * Created by ******** on 2020/4/7.
- */
-
- public class Right1Fragment extends Fragment {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_right1, container ,false);
- return view;
- }
- @Override
- public void onPause() {
- super.onPause();
- }
- }
Right2Fragment.java:
- package com.example.********.news_procedure;
-
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- /**
- * Created by ******** on 2020/4/7.
- */
-
- public class Right2Fragment extends Fragment {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_right2, container ,false);
- return view;
- }
- @Override
- public void onPause() {
- super.onPause();
- }
- }
Right3Fragment.java:
- package com.example.********.news_procedure;
-
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- /**
- * Created by ******** on 2020/4/7.
- */
-
- public class Right3Fragment extends Fragment {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_right3, container ,false);
- return view;
- }
- @Override
- public void onPause() {
- super.onPause();
- }
- }
Right4Fragment.java:
- package com.example.********.news_procedure;
-
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- /**
- * Created by ******** on 2020/4/7.
- */
-
- public class Right4Fragment extends Fragment {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_right4, container ,false);
- return view;
- }
- @Override
- public void onPause() {
- super.onPause();
- }
- }
Right5Fragment.java:
- package com.example.********.news_procedure;
-
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- /**
- * Created by ******** on 2020/4/7.
- */
-
- public class Right5Fragment extends Fragment {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_right5, container ,false);
- return view;
- }
- @Override
- public void onPause() {
- super.onPause();
- }
- }
MainActivity.java:
- package com.example.**********.news_procedure;
-
- import android.support.v7.app.AppCompatActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
-
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- FragmentManager manager = getSupportFragmentManager();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.replace(R.id.frame_left, new LeftFragment());
- transaction.replace(R.id.frame_right, new RightFragment());
- transaction.commit();
- }
-
- public void click1(View view) {
- FragmentManager manager = getSupportFragmentManager();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.replace(R.id.frame_right, new Right1Fragment());
- transaction.commit();
- }
-
- public void click2(View view) {
- FragmentManager manager = getSupportFragmentManager();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.replace(R.id.frame_right, new Right2Fragment());
- transaction.commit();
- }
-
- public void click3(View view) {
- FragmentManager manager = getSupportFragmentManager();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.replace(R.id.frame_right, new Right3Fragment());
- transaction.commit();
- }
-
- public void click4(View view) {
- FragmentManager manager = getSupportFragmentManager();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.replace(R.id.frame_right, new Right4Fragment());
- transaction.commit();
- }
-
- public void click5(View view) {
- FragmentManager manager = getSupportFragmentManager();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.replace(R.id.frame_right, new Right5Fragment());
- transaction.commit();
- }
- }
运行结果显示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。