当前位置:   article > 正文

Android Studio 实现网易新闻App (简单方便易懂)_android studio新闻app

android studio新闻app

一、高质量源码(非开源,白嫖低价勿扰)

关注公众号:《编程乐学》

后台回复:24011001

目录

前言

一、任务介绍

1.1 背景

1.2目的和意义

二、 实现介绍

视频演示

2.1 启动页实现

2.2 注册页面实现

2.3 登陆页面实现

2.4 首页实现

2.5 详情页面实现

三、获取源码


前言

        随着移动互联网的持续发展,人们对于获取最新新闻和资讯的需求日益增长。因此,我们选择网易新闻作为开发项目的主题,旨在提供一款便捷、全面的新闻阅读客户端,以满足用户对于多样化新闻内容的需求。

一、任务介绍

1.1 背景

  1. 移动互联网发展:随着移动互联网的迅速发展,用户对获取及时、全面的新闻资讯的需求不断增加。人们希望能够在移动设备上方便快捷地浏览各类新闻,包括时事报道、娱乐八卦、科技趋势等多样化内容。

  2. 网易新闻平台:网易新闻作为一家知名的综合性新闻客户端,提供了多种新闻资讯,涵盖时政、财经、娱乐、体育等领域。为了满足用户对不同类型新闻的需求,提供了个性化的推荐服务,以及用户互动的评论、分享功能等。

  3. 开发动机:基于移动互联网的发展趋势以及对全面、快捷新闻阅读的需求,我们立足于Android端开发,选择实现网易新闻客户端作为本次项目。通过该项目,旨在提供一款便捷、多功能的新闻客户端应用,为用户提供优质的新闻阅读体验并满足他们的个性化需求。

    

1.2目的和意义

        通过开发网易新闻客户端,旨在为用户提供一种方便、快捷的途径来获取各类新闻资讯。用户可以随时随地通过移动设备浏览新闻内容,无论是在公交车上、办公室里还是家庭中,都能够享受到全面、多样化的新闻服务。

二、 实现介绍

视频演示

Android Studio 实现网易新闻App (简单方便易懂)

2.1 启动页实现

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@drawable/start"
  8. tools:context=".Start.StartActivity">
  9. </androidx.constraintlayout.widget.ConstraintLayout>

2.2 注册页面实现

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="#ebebeb"
  8. tools:context=".ui.RegisterActivity">
  9. <ImageView
  10. android:id="@+id/imageView5"
  11. android:layout_width="0dp"
  12. android:layout_height="250dp"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent"
  16. app:srcCompat="@drawable/logo" />
  17. <View
  18. android:id="@+id/view2"
  19. android:layout_width="0dp"
  20. android:layout_height="140dp"
  21. android:background="#fff"
  22. app:layout_constraintEnd_toEndOf="parent"
  23. app:layout_constraintStart_toStartOf="parent"
  24. app:layout_constraintTop_toBottomOf="@+id/imageView5" />
  25. <LinearLayout
  26. android:layout_width="0dp"
  27. android:layout_height="0dp"
  28. android:gravity="center|left"
  29. android:orientation="vertical"
  30. app:layout_constraintBottom_toBottomOf="@+id/view2"
  31. app:layout_constraintEnd_toEndOf="@+id/view2"
  32. app:layout_constraintStart_toStartOf="@+id/view2"
  33. app:layout_constraintTop_toBottomOf="@+id/imageView5">
  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:gravity="center|left"
  38. android:orientation="horizontal">
  39. <ImageView
  40. android:id="@+id/imageView4"
  41. android:layout_width="20dp"
  42. android:layout_height="20dp"
  43. android:layout_marginLeft="25dp"
  44. app:srcCompat="@drawable/zhanghao" />
  45. <View
  46. android:id="@+id/view4"
  47. android:layout_width="2dp"
  48. android:layout_height="24dp"
  49. android:layout_marginLeft="10dp"
  50. android:background="#cdcdcd" />
  51. <EditText
  52. android:id="@+id/username_edittext"
  53. android:layout_width="match_parent"
  54. android:layout_height="25dp"
  55. android:layout_marginLeft="10dp"
  56. android:layout_weight="1"
  57. android:background="#fff"
  58. android:ems="10"
  59. android:textColor="#000"
  60. android:textSize="14sp"
  61. android:hint="请输入账号"
  62. android:inputType="textPersonName" />
  63. </LinearLayout>
  64. <LinearLayout
  65. android:layout_width="match_parent"
  66. android:layout_height="wrap_content"
  67. android:layout_marginTop="20dp"
  68. android:orientation="horizontal">
  69. <ImageView
  70. android:id="@+id/imageView"
  71. android:layout_width="20dp"
  72. android:layout_height="20dp"
  73. android:layout_marginLeft="25dp"
  74. app:srcCompat="@drawable/mima" />
  75. <View
  76. android:id="@+id/view3"
  77. android:layout_width="2dp"
  78. android:layout_height="24dp"
  79. android:layout_marginLeft="10dp"
  80. android:background="#cdcdcd" />
  81. <EditText
  82. android:id="@+id/password_edittext"
  83. android:layout_width="match_parent"
  84. android:layout_height="25dp"
  85. android:layout_marginLeft="10dp"
  86. android:layout_weight="1"
  87. android:background="#fff"
  88. android:ems="10"
  89. android:hint="请输入密码"
  90. android:inputType="textPassword"
  91. android:textColor="#000"
  92. android:textSize="14sp" />
  93. </LinearLayout>
  94. <LinearLayout
  95. android:layout_width="match_parent"
  96. android:layout_height="wrap_content"
  97. android:layout_marginTop="20dp"
  98. android:orientation="horizontal">
  99. <ImageView
  100. android:id="@+id/imageView6"
  101. android:layout_width="20dp"
  102. android:layout_height="20dp"
  103. android:layout_marginLeft="25dp"
  104. app:srcCompat="@drawable/mima" />
  105. <View
  106. android:id="@+id/view7"
  107. android:layout_width="2dp"
  108. android:layout_height="24dp"
  109. android:layout_marginLeft="10dp"
  110. android:background="#cdcdcd" />
  111. <EditText
  112. android:id="@+id/repassword"
  113. android:layout_width="match_parent"
  114. android:layout_height="25dp"
  115. android:layout_marginLeft="10dp"
  116. android:layout_weight="1"
  117. android:background="#fff"
  118. android:ems="10"
  119. android:hint="请再次输入密码"
  120. android:inputType="textPassword"
  121. android:textColor="#000"
  122. android:textSize="14sp" />
  123. </LinearLayout>
  124. </LinearLayout>
  125. <Button
  126. android:id="@+id/register_button"
  127. android:layout_width="0dp"
  128. android:layout_height="40dp"
  129. android:layout_marginStart="32dp"
  130. android:layout_marginTop="32dp"
  131. android:layout_marginEnd="32dp"
  132. android:background="@drawable/login"
  133. android:text="立 即 注 册 "
  134. android:textColor="#fff"
  135. android:textSize="16sp"
  136. app:layout_constraintEnd_toEndOf="parent"
  137. app:layout_constraintStart_toStartOf="parent"
  138. app:layout_constraintTop_toBottomOf="@+id/view2" />
  139. <TextView
  140. android:id="@+id/tv_login"
  141. android:layout_width="wrap_content"
  142. android:layout_height="wrap_content"
  143. android:layout_marginTop="16dp"
  144. android:text="已有帐号,立即登陆!"
  145. android:textColor="#5dc2d0"
  146. android:textStyle="bold"
  147. app:layout_constraintEnd_toEndOf="@+id/register_button"
  148. app:layout_constraintStart_toStartOf="@+id/register_button"
  149. app:layout_constraintTop_toBottomOf="@+id/register_button" />
  150. </androidx.constraintlayout.widget.ConstraintLayout>

1. 获取用户名、密码和重复密码的输入内容。

2. 检查用户名和密码是否为空,如果为空则显示一个 Toast 提示用户输入账号或密码,并结束方法的执行。

3. 检查两次输入的密码是否一致,如果一致则执行以下操作:

   a. 调用 mDatabaseHelper 的 insertData 方法将用户名和密码插入数据库。

   b. 如果插入成功,则显示一个注册成功的 Toast 提示,并跳转到登录页面,最后关闭当前页面。

   c. 如果插入失败,则显示一个注册失败的 Toast 提示。

4. 如果两次输入的密码不一致,则显示一个 Toast 提示用户两次密码不同。

  1. package com.example.news.ui;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import com.example.news.Data.DatabaseHelper;
  11. import com.example.news.R;
  12. public class RegisterActivity extends AppCompatActivity {
  13. private EditText mUserNameEditText;
  14. private EditText mPasswordEditText,mRePasswordEditText;
  15. private Button registerButton;
  16. private TextView loginTv;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_register);
  21. // 获取控件
  22. mUserNameEditText = findViewById(R.id.username_edittext);
  23. mPasswordEditText = findViewById(R.id.password_edittext);
  24. mRePasswordEditText = findViewById(R.id.repassword);
  25. registerButton = findViewById(R.id.register_button);
  26. loginTv = findViewById(R.id.tv_login);
  27. // 点击跳转登录页
  28. loginTv.setOnClickListener(new View.OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
  32. startActivity(intent);
  33. }
  34. });
  35. // 注册成功后跳转登录页
  36. registerButton.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. // 获取用户输入的内容
  40. String username = mUserNameEditText.getText().toString().trim();
  41. String password = mPasswordEditText.getText().toString().trim();
  42. String repassword = mRePasswordEditText.getText().toString().trim();
  43. if (username.isEmpty() || password.isEmpty()|| repassword.isEmpty()) {
  44. Toast.makeText(getApplicationContext(), "请输入账号或密码或重复密码", Toast.LENGTH_SHORT).show();
  45. return;
  46. }
  47. if (!password.equals(repassword)) {
  48. Toast.makeText(getApplicationContext(), "两次密码不一致", Toast.LENGTH_SHORT).show();
  49. return;
  50. }
  51. if (result) {
  52. Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
  53. Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
  54. startActivity(intent);
  55. finish();
  56. } else {
  57. Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();
  58. }
  59. }
  60. });
  61. }
  62. }

2.3 登陆页面实现

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="#ebebeb"
  8. tools:context=".ui.LoginActivity">
  9. <ImageView
  10. android:id="@+id/imageView3"
  11. android:layout_width="0dp"
  12. android:layout_height="250dp"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent"
  16. app:srcCompat="@drawable/logo" />
  17. <View
  18. android:id="@+id/view"
  19. android:layout_width="0dp"
  20. android:layout_height="110dp"
  21. android:background="#FFFFFF"
  22. app:layout_constraintEnd_toEndOf="parent"
  23. app:layout_constraintStart_toStartOf="parent"
  24. app:layout_constraintTop_toBottomOf="@+id/imageView3" />
  25. <LinearLayout
  26. android:layout_width="0dp"
  27. android:layout_height="0dp"
  28. android:gravity="center|left"
  29. android:orientation="vertical"
  30. app:layout_constraintBottom_toBottomOf="@+id/view"
  31. app:layout_constraintEnd_toEndOf="parent"
  32. app:layout_constraintStart_toStartOf="@+id/view"
  33. app:layout_constraintTop_toBottomOf="@+id/imageView3">
  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:gravity="center|left"
  38. android:orientation="horizontal">
  39. <ImageView
  40. android:id="@+id/imageView4"
  41. android:layout_width="20dp"
  42. android:layout_height="20dp"
  43. android:layout_marginLeft="25dp"
  44. app:srcCompat="@drawable/zhanghao" />
  45. <View
  46. android:id="@+id/view5"
  47. android:layout_width="2dp"
  48. android:layout_height="24dp"
  49. android:layout_marginLeft="10dp"
  50. android:background="#cdcdcd" />
  51. <EditText
  52. android:id="@+id/user"
  53. android:layout_width="match_parent"
  54. android:layout_height="25dp"
  55. android:layout_marginLeft="10dp"
  56. android:layout_weight="1"
  57. android:background="#fff"
  58. android:ems="10"
  59. android:inputType="textPersonName"
  60. android:textColor="#000"
  61. android:textSize="14sp"
  62. android:hint="请输入账号"
  63. />
  64. <ImageView
  65. android:id="@+id/img_user_delete"
  66. android:layout_width="20dp"
  67. android:layout_height="20dp"
  68. android:layout_marginRight="20dp"
  69. app:srcCompat="@drawable/delete" />
  70. </LinearLayout>
  71. <LinearLayout
  72. android:layout_width="match_parent"
  73. android:layout_height="wrap_content"
  74. android:layout_marginTop="30dp"
  75. android:gravity="center|left"
  76. android:orientation="horizontal">
  77. <ImageView
  78. android:id="@+id/imageView"
  79. android:layout_width="20dp"
  80. android:layout_height="20dp"
  81. android:layout_marginLeft="25dp"
  82. app:srcCompat="@drawable/mima" />
  83. <View
  84. android:id="@+id/view6"
  85. android:layout_width="2dp"
  86. android:layout_height="24dp"
  87. android:layout_marginLeft="10dp"
  88. android:background="#cdcdcd" />
  89. <EditText
  90. android:id="@+id/pass"
  91. android:layout_width="match_parent"
  92. android:layout_height="25dp"
  93. android:layout_marginLeft="10dp"
  94. android:layout_weight="1"
  95. android:background="#fff"
  96. android:ems="10"
  97. android:inputType="textPassword"
  98. android:textColor="#000"
  99. android:textSize="14sp"
  100. android:hint="请输入密码"
  101. />
  102. <ImageView
  103. android:id="@+id/img_pass_delete"
  104. android:layout_width="20dp"
  105. android:layout_height="20dp"
  106. android:layout_marginRight="20dp"
  107. app:srcCompat="@drawable/delete" />
  108. </LinearLayout>
  109. </LinearLayout>
  110. <TextView
  111. android:id="@+id/textView"
  112. android:layout_width="wrap_content"
  113. android:layout_height="wrap_content"
  114. android:layout_marginTop="16dp"
  115. android:layout_marginEnd="24dp"
  116. android:text="忘 记 密 码 ?"
  117. android:textColor="#5dc2d0"
  118. android:textStyle="bold"
  119. app:layout_constraintEnd_toEndOf="parent"
  120. app:layout_constraintTop_toBottomOf="@+id/view" />
  121. <TextView
  122. android:id="@+id/login_register"
  123. android:layout_width="wrap_content"
  124. android:layout_height="wrap_content"
  125. android:layout_marginTop="16dp"
  126. android:text="新用户,立即注册!"
  127. android:textColor="#5dc2d0"
  128. android:textStyle="bold"
  129. app:layout_constraintEnd_toEndOf="@+id/login_button"
  130. app:layout_constraintStart_toStartOf="@+id/login_button"
  131. app:layout_constraintTop_toBottomOf="@+id/login_button" />
  132. <Button
  133. android:id="@+id/login_button"
  134. android:layout_width="0dp"
  135. android:layout_height="40dp"
  136. android:layout_marginStart="32dp"
  137. android:layout_marginTop="32dp"
  138. android:layout_marginEnd="32dp"
  139. android:background="@drawable/login"
  140. android:text="立 即 登 录 "
  141. android:textColor="#fff"
  142. android:textSize="16sp"
  143. app:layout_constraintEnd_toEndOf="parent"
  144. app:layout_constraintStart_toStartOf="parent"
  145. app:layout_constraintTop_toBottomOf="@+id/textView" />
  146. </androidx.constraintlayout.widget.ConstraintLayout>

1. 获取用户名和密码的输入内容。

2. 检查用户名和密码是否为空,如果为空则显示一个 Toast 提示用户输入账号或密码,并结束方法的执行。

3. 调用 mDatabaseHelper 的 checkUser 方法来检查用户名和密码是否匹配。

4. 如果匹配成功,则显示一个登录成功的 Toast 提示,并跳转到主页面 MainActivity。

5. 如果匹配失败,则显示一个账号或密码错误的 Toast 提示。

  1. package com.example.news.ui;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. import com.example.news.Data.DatabaseHelper;
  12. import com.example.news.R;
  13. public class LoginActivity extends AppCompatActivity {
  14. private TextView loginRegister;
  15. private EditText user;
  16. private EditText pass;
  17. private Button mLoginButton;
  18. private ImageView imgUserDelete;
  19. private ImageView imgPassDelete;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_login);
  24. initView();
  25. clear();
  26. register();
  27. home();
  28. }
  29. // 登陆成功后跳转首页
  30. private void home() {
  31. mLoginButton.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. // 获取用户输入的内容
  35. String username = user.getText().toString().trim();
  36. String password = pass.getText().toString().trim();
  37. if (username.isEmpty() || password.isEmpty()) {
  38. Toast.makeText(getApplicationContext(), "请输入账号或密码", Toast.LENGTH_SHORT).show();
  39. return;
  40. }
  41. boolean result = mDatabaseHelper.checkUser(username, password);
  42. if (result) {
  43. Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT).show();
  44. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  45. startActivity(intent);
  46. } else {
  47. Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT).show();
  48. }
  49. }
  50. });
  51. }
  52. // 点击跳转注册页
  53. private void register() {
  54. loginRegister.setOnClickListener(new View.OnClickListener() {
  55. @Override
  56. public void onClick(View v) {
  57. Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
  58. startActivity(intent);
  59. }
  60. });
  61. }
  62. // 点击清空输入的内容
  63. private void clear() {
  64. imgUserDelete.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View v) {
  67. user.setText("");
  68. }
  69. });
  70. imgPassDelete.setOnClickListener(new View.OnClickListener() {
  71. @Override
  72. public void onClick(View v) {
  73. pass.setText("");
  74. }
  75. });
  76. }
  77. // 获取控件
  78. private void initView() {
  79. user = findViewById(R.id.user);
  80. pass = findViewById(R.id.pass);
  81. mLoginButton = findViewById(R.id.login_button);
  82. loginRegister = findViewById(R.id.login_register);
  83. imgUserDelete = findViewById(R.id.img_user_delete);
  84. imgPassDelete = findViewById(R.id.img_pass_delete);
  85. }
  86. }

2.4 首页实现

  首页主要实现新闻列表的显示与切换

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="#f1f1f1"
  8. tools:context=".MainActivity">
  9. <ScrollView
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. app:layout_constraintBottom_toBottomOf="parent"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent">
  16. <androidx.constraintlayout.widget.ConstraintLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content">
  19. <androidx.constraintlayout.widget.Guideline
  20. android:id="@+id/guideline"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:orientation="vertical"
  24. app:layout_constraintGuide_percent="0.05" />
  25. <androidx.constraintlayout.widget.Guideline
  26. android:id="@+id/guideline2"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:orientation="vertical"
  30. app:layout_constraintGuide_percent="0.95" />
  31. <com.youth.banner.Banner
  32. android:id="@+id/banner"
  33. android:layout_width="0dp"
  34. android:layout_height="140dp"
  35. android:layout_marginTop="8dp"
  36. app:layout_constraintEnd_toStartOf="@+id/guideline2"
  37. app:layout_constraintStart_toStartOf="@+id/guideline"
  38. app:layout_constraintTop_toTopOf="parent">
  39. </com.youth.banner.Banner>
  40. <View
  41. android:id="@+id/view"
  42. android:layout_width="0dp"
  43. android:layout_height="256dp"
  44. android:layout_marginTop="8dp"
  45. android:background="@drawable/main_view"
  46. app:layout_constraintEnd_toStartOf="@+id/guideline2"
  47. app:layout_constraintStart_toStartOf="@+id/guideline"
  48. app:layout_constraintTop_toBottomOf="@+id/banner" />
  49. <LinearLayout
  50. android:layout_width="357dp"
  51. android:layout_height="240dp"
  52. android:orientation="vertical"
  53. app:layout_constraintEnd_toStartOf="@+id/guideline2"
  54. app:layout_constraintStart_toStartOf="@+id/guideline"
  55. app:layout_constraintTop_toTopOf="@+id/view">
  56. <LinearLayout
  57. android:layout_width="match_parent"
  58. android:layout_height="wrap_content"
  59. android:orientation="horizontal">
  60. <TextView
  61. android:id="@+id/textView3"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:layout_marginStart="16dp"
  65. android:layout_marginTop="8dp"
  66. android:text="正在热映"
  67. android:textColor="#000"
  68. android:textSize="18sp"
  69. android:textStyle="bold" />
  70. </LinearLayout>
  71. <androidx.recyclerview.widget.RecyclerView
  72. android:id="@+id/rv_hot"
  73. android:layout_width="350dp"
  74. android:layout_height="match_parent"
  75. android:layout_marginLeft="3dp" />
  76. </LinearLayout>
  77. <View
  78. android:id="@+id/view2"
  79. android:layout_width="0dp"
  80. android:layout_height="250dp"
  81. android:layout_marginTop="8dp"
  82. android:background="@drawable/main_view"
  83. app:layout_constraintEnd_toStartOf="@+id/guideline2"
  84. app:layout_constraintHorizontal_bias="0.0"
  85. app:layout_constraintStart_toStartOf="@+id/guideline"
  86. app:layout_constraintTop_toBottomOf="@+id/view" />
  87. <TextView
  88. android:id="@+id/textView"
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content"
  91. android:layout_marginStart="16dp"
  92. android:layout_marginTop="8dp"
  93. android:text="即将上映"
  94. android:textColor="#000"
  95. android:textSize="18sp"
  96. android:textStyle="bold"
  97. app:layout_constraintStart_toStartOf="@+id/guideline"
  98. app:layout_constraintTop_toTopOf="@+id/view2" />
  99. <androidx.recyclerview.widget.RecyclerView
  100. android:id="@+id/rv_future"
  101. android:layout_width="350dp"
  102. android:layout_height="220dp"
  103. android:orientation="horizontal"
  104. app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
  105. app:layout_constraintEnd_toStartOf="@+id/guideline2"
  106. app:layout_constraintStart_toStartOf="@+id/guideline"
  107. app:layout_constraintTop_toBottomOf="@+id/textView"
  108. tools:listitem="@layout/tobeshown_item">
  109. </androidx.recyclerview.widget.RecyclerView>
  110. <ImageView
  111. android:id="@+id/imageView4"
  112. android:layout_width="10dp"
  113. android:layout_height="10dp"
  114. android:layout_marginTop="24dp"
  115. android:layout_marginEnd="20dp"
  116. app:layout_constraintEnd_toStartOf="@+id/guideline2"
  117. app:layout_constraintTop_toBottomOf="@+id/banner"
  118. app:srcCompat="@drawable/jiantou" />
  119. <TextView
  120. android:id="@+id/tv_all"
  121. android:layout_width="wrap_content"
  122. android:layout_height="wrap_content"
  123. android:layout_marginEnd="3dp"
  124. android:text="查看全部"
  125. android:textColor="#000"
  126. android:textSize="15sp"
  127. app:layout_constraintBottom_toBottomOf="@+id/imageView4"
  128. app:layout_constraintEnd_toStartOf="@+id/imageView4"
  129. app:layout_constraintTop_toTopOf="@+id/imageView4" />
  130. </androidx.constraintlayout.widget.ConstraintLayout>
  131. </ScrollView>
  132. </androidx.constraintlayout.widget.ConstraintLayout>

        在onCreate方法中,首先设置了布局文件为activity_main,然后调用了initView和tabLayout方法。

        在initView方法中,获取了TabLayout控件的引用。

        在tabLayout方法中,首先定义了一个字符串数组types,用于存储TabLayout的标题。然后通过调用removeAllTabs方法,清空TabLayout中原有的标签。

        接下来设置了TabLayout的样式,包括文字颜色和选中标签的指示器颜色。

        然后使用for循环遍历types数组,在每次循环中,创建一个新的TabLayout.Tab对象,并设置标签的文字内容,最后调用addTab方法将标签添加到TabLayout中。

        最后,设置了TabLayout的点击事件监听器,在点击事件回调方法中,根据点击的标签位置调用setFragment方法切换显示不同的Fragment。

        setFragment方法中,通过传入的id选择要加载的Fragment,并开启事务,使用replace方法将指定的Fragment替换当前的Fragment,并最终调用commit方法提交事务。

        通过以上逻辑,完成了在MainActivity中显示TabLayout,并根据点击的标签切换显示不同的Fragment的功能。

  1. package com.example.news.ui;
  2. import android.graphics.Color;
  3. import android.os.Bundle;
  4. import android.widget.FrameLayout;
  5. import androidx.appcompat.app.AppCompatActivity;
  6. import androidx.fragment.app.Fragment;
  7. import com.example.news.Fragment.EntertainmentNewsFragment;
  8. import com.example.news.Fragment.FilmsFragment;
  9. import com.example.news.Fragment.HeadlinesFragment;
  10. import com.example.news.Fragment.NewsHighlightskFragment;
  11. import com.example.news.Fragment.SportsNewsFragment;
  12. import com.example.news.R;
  13. import com.google.android.material.tabs.TabLayout;
  14. public class MainActivity extends AppCompatActivity {
  15. private FrameLayout fr;
  16. private TabLayout tabLayout;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. initView();
  22. tabLayout();
  23. setFragment(0);//默认第一个页面
  24. }
  25. // 固定标题
  26. private void tabLayout() {
  27. String[] types = {"头条", "精选", "娱乐", "运动", "影视"};
  28. tabLayout.removeAllTabs();
  29. // 设置TabLayout的样式为自定义样式
  30. tabLayout.setTabTextColors(getResources().getColor(R.color.black), getResources().getColor(R.color.red));
  31. tabLayout.setSelectedTabIndicatorColor(Color.RED);
  32. for (String type : types) {
  33. TabLayout.Tab tab = tabLayout.newTab().setText(type);
  34. tabLayout.addTab(tab);
  35. }
  36. // 点击标题进行页面转换
  37. tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  38. @Override
  39. public void onTabSelected(TabLayout.Tab tab) {
  40. int position = tab.getPosition();
  41. setFragment(position);
  42. }
  43. @Override
  44. public void onTabUnselected(TabLayout.Tab tab) {
  45. }
  46. @Override
  47. public void onTabReselected(TabLayout.Tab tab) {
  48. }
  49. });
  50. }
  51. private void initView() {
  52. tabLayout = findViewById(R.id.tablayput);
  53. }
  54. }

2.5 详情页面实现

        在onCreate方法中,它首先从意图中获取传递的数据类型和数据字符串,并使用Gson库将数据字符串解析为相应的数据对象。然后根据数据类型调用不同的updateUI方法来更新界面。

        在updateUI方法中,它根据数据对象的属性构建一个URL,然后使用WebView加载该URL。WebView用于显示新闻内容,并设置了JavaScript的支持和一个WebViewClient用于处理链接的点击事件。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".ui.DetailsActivity">
  8. <View
  9. android:id="@+id/view8"
  10. android:layout_width="0dp"
  11. android:layout_height="50dp"
  12. android:background="#5dc2d0"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent" />
  16. <ImageView
  17. android:id="@+id/img_back"
  18. android:layout_width="20dp"
  19. android:layout_height="20dp"
  20. android:layout_marginStart="8dp"
  21. app:layout_constraintBottom_toBottomOf="@+id/view8"
  22. app:layout_constraintStart_toStartOf="@+id/view8"
  23. app:layout_constraintTop_toTopOf="parent"
  24. app:srcCompat="@drawable/back" />
  25. <TextView
  26. android:id="@+id/textView2"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="新闻详情"
  30. android:textColor="#fff"
  31. android:textSize="20sp"
  32. android:textStyle="bold"
  33. app:layout_constraintBottom_toBottomOf="@+id/view8"
  34. app:layout_constraintEnd_toEndOf="parent"
  35. app:layout_constraintStart_toStartOf="parent"
  36. app:layout_constraintTop_toTopOf="parent" />
  37. <WebView
  38. android:id="@+id/webview"
  39. android:layout_width="0dp"
  40. android:layout_height="0dp"
  41. app:layout_constraintBottom_toBottomOf="parent"
  42. app:layout_constraintEnd_toEndOf="parent"
  43. app:layout_constraintStart_toStartOf="parent"
  44. app:layout_constraintTop_toBottomOf="@+id/view8" />
  45. </androidx.constraintlayout.widget.ConstraintLayout>

  1. package com.example.news.ui;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.webkit.WebView;
  5. import android.webkit.WebViewClient;
  6. import android.widget.ImageView;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import com.example.news.R;
  9. import com.example.news.bean.EntertainmentNewsBean;
  10. import com.example.news.bean.FilmsBean;
  11. import com.example.news.bean.HeadlinesBean;
  12. import com.example.news.bean.NewsHighlightsBean;
  13. import com.example.news.bean.SportsNewsBean;
  14. public class DetailsActivity extends AppCompatActivity {
  15. private ImageView imgBack;
  16. private WebView webview;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_details);
  21. initView();
  22. back();
  23. String dataType = getIntent().getStringExtra("dataType");
  24. String dataString = getIntent().getStringExtra("data");
  25. }
  26. // 返回
  27. private void back() {
  28. imgBack.setOnClickListener(new View.OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. finish();
  32. }
  33. });
  34. }
  35. // 获取控件
  36. private void initView() {
  37. imgBack = findViewById(R.id.img_back);
  38. webview = findViewById(R.id.webview);
  39. }
  40. }

        到此为止,我们的项目就已经完成了! 

三、获取源码

关注公众号《编程乐学》,后台回复:24011001

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