一、静态页面

FragmentProject.zip

二、整体页面

FragmentProject.zip

三、页面实现的步骤:

1.上部分RecyclerView的实现:

1.1实现主布局,创建item_list_lesson.xml布局文件,上部分用recyclerview实现,下部分用分段实现

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1616055176041-cbced1fe-4fb1-46b2-bb6e-85628573e8e0.png#align=left&display=inline&height=520&margin=%5Bobject%20Object%5D&name=image.png&originHeight=520&originWidth=530&size=51628&status=done&style=none&width=530)

1.2创建recyclerview_lesson.xml布局文件,实现recyclerview的内容部分样式

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1616055275850-0512e18e-47fc-4f89-bc0a-2d302e993aba.png#align=left&display=inline&height=340&margin=%5Bobject%20Object%5D&name=image.png&originHeight=340&originWidth=559&size=32557&status=done&style=none&width=559)

1.3创建LessonDataBean文件,封装数据

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1616055430915-c3e800e3-0085-4118-a56f-bec6d00e084b.png#align=left&display=inline&height=278&margin=%5Bobject%20Object%5D&name=image.png&originHeight=278&originWidth=390&size=16330&status=done&style=none&width=390)

1.4创建LessonAdapter适配器文件,实现数据传输

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1616055581453-1acfa710-27b5-4165-a691-70180acd923e.png#align=left&display=inline&height=652&margin=%5Bobject%20Object%5D&name=image.png&originHeight=652&originWidth=710&size=64790&status=done&style=none&width=710)

1.5在主活动LessonListActivity中,加入数据,实现静态布局

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1616055787128-74874f6b-536c-4e95-8954-fcf4d139656d.png#align=left&display=inline&height=777&margin=%5Bobject%20Object%5D&name=image.png&originHeight=777&originWidth=708&size=77950&status=done&style=none&width=708)

2.下部分片段嵌套Recyclerview的实现:

2.1创建activity_lesson_fragment_one.xml一个片段recyclerview的页面

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1616055934366-357123ed-2e82-4192-8a1e-0574150f269b.png#align=left&display=inline&height=296&margin=%5Bobject%20Object%5D&name=image.png&originHeight=296&originWidth=580&size=32110&status=done&style=none&width=580)

2.2创建item_lesson_fragment.xml页面布局内容

image.png

2.3创建layout_base.xml布局文件,将容器放入

image.png

2.4创建ListDataBean文件,将数据封装

image.png

2.5创建ListAdapter适配器文件,传输数据

image.png

2.6创建BaseFragment文件,将容器放入,BaseFragment(子片段)继承Fragment(片段)

image.png

2.7创建LessonFragmentOne文件,LessonFragmentOne继承BaseFragment

image.png

2.8添加剩下三个页面

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1616057282056-6f5e5dbe-9dca-4116-86f6-3e09caeffec1.png#align=left&display=inline&height=413&margin=%5Bobject%20Object%5D&name=image.png&originHeight=413&originWidth=399&size=23487&status=done&style=none&width=399)

3.下部分片段点击切换的实现:

3.1在LessonListActivity文件中判断,进行片段隐藏

image.png

3.2在LessonListActivity文件中添加管理器,进行判断,点击则显示该页面

image.png

3.3在LessonAdapter文件中添加按钮,获取按钮的位置

image.png

3.4在LessonAdapter文件中添加点击事件,实现点击效果

image.png

四、整体代码

1.item_list_lesson.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 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=".LessonListActivity"
  8. android:orientation="vertical"
  9. android:background="#fff6f6f6"
  10. >
  11. <androidx.recyclerview.widget.RecyclerView
  12. android:id="@+id/rv_project"
  13. android:layout_width="match_parent"
  14. android:layout_height="50dp"
  15. android:paddingTop="20dp"
  16. />
  17. <FrameLayout
  18. android:id="@+id/fra_project"
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:layout_weight="1"
  22. android:paddingTop="20dp"
  23. android:paddingLeft="60dp"
  24. />
  25. </LinearLayout>

2.recyclerview_lesson.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="36dp"
  5. android:orientation="horizontal"
  6. android:gravity="center"
  7. android:paddingLeft="60dp">
  8. <TextView
  9. android:id="@+id/tv_title"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="党员支部大会"
  13. android:textSize="12sp"
  14. android:textColor="#73000000"
  15. />
  16. </LinearLayout>

3.LessonDataBean文件代码

  1. package com.example.fragmentproject.bean;
  2. public class LessonDataBean {
  3. private int title;
  4. public LessonDataBean(int title) {
  5. this.title = title;
  6. }
  7. public int getTitle() {
  8. return title;
  9. }
  10. public void setTitle(int title) {
  11. this.title = title;
  12. }
  13. }

4.LessonAdapter文件代码

  1. package com.example.fragmentproject.adapter;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.TextView;
  7. import androidx.annotation.NonNull;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import com.example.fragmentproject.R;
  10. import com.example.fragmentproject.bean.LessonDataBean;
  11. import java.util.List;
  12. public class LessonAdapter extends RecyclerView.Adapter<LessonAdapter.MyViewHolder>{
  13. private Context context;
  14. private List<LessonDataBean> list;
  15. private View inflate;
  16. public LessonAdapter(Context context, List<LessonDataBean> list) {
  17. this.context = context;
  18. this.list = list;
  19. }
  20. //第一步,定义一个接口
  21. public interface OnItemClickListener{
  22. void onClick(int position);
  23. }
  24. private OnItemClickListener listener;
  25. //第二步,写一个公共的方法
  26. public void setOnItemClickListener(OnItemClickListener listener){
  27. this.listener=listener;
  28. }
  29. @NonNull
  30. @Override
  31. public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  32. inflate= LayoutInflater.from(context).inflate(R.layout.recyclerview_lesson,parent,false);
  33. MyViewHolder viewHolder=new MyViewHolder(inflate);
  34. return viewHolder;
  35. }
  36. @Override
  37. public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
  38. holder.tv_title.setText(list.get(position).getTitle());
  39. holder.itemView.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. if (listener!=null){
  43. listener.onClick(position);
  44. }
  45. }
  46. });
  47. }
  48. @Override
  49. public int getItemCount() {
  50. return list.size();
  51. }
  52. class MyViewHolder extends RecyclerView.ViewHolder{
  53. private TextView tv_title;
  54. public MyViewHolder(@NonNull View itemView) {
  55. super(itemView);
  56. tv_title=itemView.findViewById(R.id.tv_title);
  57. }
  58. }
  59. }

5.LessonListActivity文件代码

  1. package com.example.fragmentproject;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.fragment.app.FragmentManager;
  4. import androidx.fragment.app.FragmentTransaction;
  5. import androidx.recyclerview.widget.DefaultItemAnimator;
  6. import androidx.recyclerview.widget.DividerItemDecoration;
  7. import androidx.recyclerview.widget.GridLayoutManager;
  8. import androidx.recyclerview.widget.LinearLayoutManager;
  9. import androidx.recyclerview.widget.RecyclerView;
  10. import android.content.Context;
  11. import android.os.Bundle;
  12. import android.text.BoringLayout;
  13. import android.widget.FrameLayout;
  14. import com.example.fragmentproject.adapter.LessonAdapter;
  15. import com.example.fragmentproject.adapter.ListAdapter;
  16. import com.example.fragmentproject.bean.LessonDataBean;
  17. import com.example.fragmentproject.bean.ListDataBean;
  18. import com.example.fragmentproject.fragment.LessonFragmentFour;
  19. import com.example.fragmentproject.fragment.LessonFragmentOne;
  20. import com.example.fragmentproject.fragment.LessonFragmentThree;
  21. import com.example.fragmentproject.fragment.LessonFragmentTwo;
  22. import java.util.ArrayList;
  23. public class LessonListActivity extends AppCompatActivity {
  24. private RecyclerView recyclerView;
  25. private LessonAdapter lessonAdapter;
  26. private ArrayList<LessonDataBean> list=new ArrayList<>();
  27. private Context context;
  28. private int[] title={R.string.title_one,R.string.title_two,R.string.title_three,R.string.title_four};
  29. private LessonFragmentOne lessonFragmentOne;
  30. private LessonFragmentTwo lessonFragmentTwo;
  31. private LessonFragmentThree lessonFragmentThree;
  32. private LessonFragmentFour lessonFragmentFour;
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.item_list_lesson);
  37. context=this;
  38. initData();
  39. initView();
  40. setFragment(0);
  41. }
  42. private void initData(){
  43. for (int i=0;i<4;i++){
  44. LessonDataBean lessonDataBean=new LessonDataBean(title[i]);
  45. list.add(lessonDataBean);
  46. }
  47. }
  48. private void initView(){
  49. recyclerView=findViewById(R.id.rv_project);
  50. lessonAdapter=new LessonAdapter(context,list);
  51. lessonAdapter.setOnItemClickListener(new LessonAdapter.OnItemClickListener() {
  52. @Override
  53. public void onClick(int position) {
  54. setFragment(position);
  55. }
  56. });
  57. LinearLayoutManager manager=new LinearLayoutManager(context);
  58. manager.setOrientation(LinearLayoutManager.HORIZONTAL);
  59. recyclerView.setLayoutManager(manager);
  60. recyclerView.setItemAnimator(new DefaultItemAnimator());
  61. recyclerView.setAdapter(lessonAdapter);
  62. setFragment(0);
  63. }
  64. private void hideFragments(FragmentTransaction transaction){
  65. if (lessonFragmentOne!=null){
  66. transaction.hide(lessonFragmentOne);
  67. }
  68. if (lessonFragmentTwo!=null){
  69. transaction.hide(lessonFragmentTwo);
  70. }
  71. if (lessonFragmentThree!=null){
  72. transaction.hide(lessonFragmentThree);
  73. }
  74. if (lessonFragmentFour!=null){
  75. transaction.hide(lessonFragmentFour);
  76. }
  77. }
  78. public void setFragment(int position){
  79. //获取Fragment管理器
  80. FragmentManager fragmentManager=getSupportFragmentManager();
  81. //开启事务
  82. FragmentTransaction transaction=fragmentManager.beginTransaction();
  83. //隐藏所有Fragment
  84. hideFragments(transaction);
  85. switch (position) {
  86. default:
  87. break;
  88. case 0:
  89. //显示对应Fragment
  90. if (lessonFragmentOne == null) {
  91. lessonFragmentOne = new LessonFragmentOne();
  92. transaction.add(R.id.fra_project, lessonFragmentOne, "lessonFragmentOne");
  93. } else {
  94. transaction.show(lessonFragmentOne);
  95. }
  96. break;
  97. case 1:
  98. if (lessonFragmentTwo == null) {
  99. lessonFragmentTwo = new LessonFragmentTwo();
  100. transaction.add(R.id.fra_project, lessonFragmentTwo, "lessonFragmentTwo");
  101. } else {
  102. transaction.show(lessonFragmentTwo);
  103. }
  104. break;
  105. case 2:
  106. if (lessonFragmentThree==null){
  107. lessonFragmentThree=new LessonFragmentThree();
  108. transaction.add(R.id.fra_project,lessonFragmentThree,"lessonFragmentThree");
  109. }else {
  110. transaction.show(lessonFragmentThree);
  111. }
  112. break;
  113. case 3:
  114. if (lessonFragmentFour == null) {
  115. lessonFragmentFour=new LessonFragmentFour();
  116. transaction.add(R.id.fra_project,lessonFragmentFour,"lessonFragmentFour");
  117. }else {
  118. transaction.show(lessonFragmentFour);
  119. }
  120. break;
  121. }
  122. //提交事务
  123. transaction.commitAllowingStateLoss();
  124. }
  125. }

6.activity_lesson_fragment_one.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout 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=".fragment.LessonFragmentOne"
  8. >
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/rv_fra_one"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. />
  14. </FrameLayout>

7.item_lesson_fragment.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="400dp"
  4. android:layout_height="71dp"
  5. android:orientation="vertical"
  6. android:background="@drawable/fra_radius_border"
  7. android:padding="12dp"
  8. android:layout_marginBottom="20dp"
  9. >
  10. <TextView
  11. android:id="@+id/tv_headline"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府"
  15. android:textSize="14sp"
  16. android:textColor="#d9000000"
  17. android:lines="1"
  18. android:ellipsize="end"
  19. />
  20. <TextView
  21. android:id="@+id/tv_time"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="2020-12-17 09:00"
  25. android:textSize="10sp"
  26. android:textColor="#73000000"
  27. android:layout_gravity="end"
  28. android:layout_marginTop="12dp"
  29. />
  30. </LinearLayout>

8.layout_base.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <FrameLayout
  7. android:id="@+id/fl_content"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent" >
  10. </FrameLayout>
  11. </LinearLayout>

9.ListDataBean文件代码

  1. package com.example.fragmentproject.bean;
  2. public class ListDataBean {
  3. private String headline;
  4. private String time;
  5. public ListDataBean(String headline, String time) {
  6. this.headline = headline;
  7. this.time = time;
  8. }
  9. public String getHeadline() {
  10. return headline;
  11. }
  12. public void setHeadline(String headline) {
  13. this.headline = headline;
  14. }
  15. public String getTime() {
  16. return time;
  17. }
  18. public void setTime(String time) {
  19. this.time = time;
  20. }
  21. }

10.ListAdapter文件代码

  1. package com.example.fragmentproject.adapter;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.TextView;
  7. import androidx.annotation.NonNull;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import com.example.fragmentproject.R;
  10. import com.example.fragmentproject.bean.ListDataBean;
  11. import java.util.List;
  12. public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyViewHolder>{
  13. private Context context;
  14. private List<ListDataBean> list;
  15. private View inflate;
  16. public ListAdapter(Context context, List<ListDataBean> list) {
  17. this.context = context;
  18. this.list = list;
  19. }
  20. @NonNull
  21. @Override
  22. public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  23. inflate= LayoutInflater.from(context).inflate(R.layout.item_lesson_fragment,parent,false);
  24. MyViewHolder viewHolder=new MyViewHolder(inflate);
  25. return viewHolder;
  26. }
  27. @Override
  28. public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
  29. holder.tv_headline.setText(list.get(position).getHeadline());
  30. holder.tv_time.setText(list.get(position).getTime());
  31. }
  32. @Override
  33. public int getItemCount() {
  34. return list.size();
  35. }
  36. class MyViewHolder extends RecyclerView.ViewHolder{
  37. private TextView tv_headline;
  38. private TextView tv_time;
  39. public MyViewHolder(@NonNull View itemView) {
  40. super(itemView);
  41. tv_headline=itemView.findViewById(R.id.tv_headline);
  42. tv_time=itemView.findViewById(R.id.tv_time);
  43. }
  44. }
  45. }

11.BaseFragment件代码

  1. package com.example.fragmentproject.base;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import androidx.annotation.NonNull;
  8. import androidx.annotation.Nullable;
  9. import androidx.fragment.app.Fragment;
  10. import com.example.fragmentproject.R;
  11. public abstract class BaseFragment extends Fragment {
  12. protected Context context;
  13. public View rootView;
  14. @Override
  15. public void onCreate(@Nullable Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. context = getActivity();
  18. }
  19. @Nullable
  20. @Override
  21. public View onCreateView(@NonNull LayoutInflater inflater,
  22. @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  23. rootView = inflater.inflate(R.layout.layout_base, container, false);
  24. ((ViewGroup) rootView.findViewById(R.id.fl_content)).
  25. addView(getLayoutInflater().inflate(getLayoutId(), null));
  26. return rootView;
  27. }
  28. @Override
  29. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  30. super.onViewCreated(view, savedInstanceState);
  31. initView();
  32. }
  33. /**
  34. * 返回页面layout
  35. *
  36. * @return layout
  37. */
  38. protected abstract int getLayoutId();
  39. /**
  40. * 初始化View
  41. */
  42. protected abstract void initView();
  43. }

12.LessonFragmentOne文件代码

  1. package com.example.fragmentproject.fragment;
  2. import androidx.annotation.NonNull;
  3. import androidx.annotation.Nullable;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import androidx.fragment.app.Fragment;
  6. import androidx.recyclerview.widget.GridLayoutManager;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. import android.content.Context;
  9. import android.os.Bundle;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.FrameLayout;
  14. import com.example.fragmentproject.LessonListActivity;
  15. import com.example.fragmentproject.R;
  16. import com.example.fragmentproject.adapter.ListAdapter;
  17. import com.example.fragmentproject.base.BaseFragment;
  18. import com.example.fragmentproject.bean.ListDataBean;
  19. import java.util.ArrayList;
  20. public class LessonFragmentOne extends BaseFragment {
  21. private RecyclerView mRecyclerView;
  22. private ListAdapter listAdapter;
  23. private ArrayList<ListDataBean> arrayList=new ArrayList<>();
  24. private String[] headline={"国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  25. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  26. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  27. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  28. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府"};
  29. private String[] time={"2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00",
  30. "2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00"};
  31. @Override
  32. public void onCreate(Bundle savedInstanceState){
  33. super.onCreate(savedInstanceState);
  34. }
  35. @Override
  36. protected int getLayoutId(){
  37. return R.layout.activity_lesson_fragment_one;
  38. }
  39. @Override
  40. protected void initView(){
  41. for (int i=0;i<10;i++){
  42. ListDataBean listDataBean=new ListDataBean(headline[i],time[i]);
  43. arrayList.add(listDataBean);
  44. }
  45. mRecyclerView=rootView.findViewById(R.id.rv_fra_one);
  46. listAdapter=new ListAdapter(getActivity(),arrayList);
  47. GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
  48. gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
  49. mRecyclerView.setLayoutManager(gridLayoutManager);
  50. mRecyclerView.setAdapter(listAdapter);
  51. }
  52. @Override
  53. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  54. super.onViewCreated(view, savedInstanceState);
  55. }
  56. }

13.activity_lesson_fragment_two.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout 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=".fragment.LessonFragmentOne"
  8. >
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/rv_fra_two"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. />
  14. </FrameLayout>

14.activity_lesson_fragment_three.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout 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=".fragment.LessonFragmentOne"
  8. >
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/rv_fra_three"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. />
  14. </FrameLayout>

15.activity_lesson_fragment_four.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout 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=".fragment.LessonFragmentOne"
  8. >
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/rv_fra_four"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. />
  14. </FrameLayout>

16.LessonFragmentTwo文件代码

  1. package com.example.fragmentproject.fragment;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import androidx.annotation.NonNull;
  5. import androidx.annotation.Nullable;
  6. import androidx.recyclerview.widget.GridLayoutManager;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. import com.example.fragmentproject.R;
  9. import com.example.fragmentproject.adapter.ListAdapter;
  10. import com.example.fragmentproject.base.BaseFragment;
  11. import com.example.fragmentproject.bean.ListDataBean;
  12. import java.util.ArrayList;
  13. public class LessonFragmentTwo extends BaseFragment {
  14. private RecyclerView mRecyclerView;
  15. private ListAdapter listAdapter;
  16. private ArrayList<ListDataBean> arrayList=new ArrayList<>();
  17. private String[] headline={"网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  18. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  19. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  20. };
  21. private String[] time={"2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00",
  22. "2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00"};
  23. @Override
  24. public void onCreate(Bundle savedInstanceState){
  25. super.onCreate(savedInstanceState);
  26. }
  27. @Override
  28. protected int getLayoutId(){
  29. return R.layout.activity_lesson_fragment_two;
  30. }
  31. @Override
  32. protected void initView(){
  33. for (int i=0;i<6;i++){
  34. ListDataBean listDataBean=new ListDataBean(headline[i],time[i]);
  35. arrayList.add(listDataBean);
  36. }
  37. mRecyclerView=rootView.findViewById(R.id.rv_fra_two);
  38. listAdapter=new ListAdapter(getActivity(),arrayList);
  39. GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
  40. gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
  41. mRecyclerView.setLayoutManager(gridLayoutManager);
  42. mRecyclerView.setAdapter(listAdapter);
  43. }
  44. @Override
  45. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  46. super.onViewCreated(view, savedInstanceState);
  47. }
  48. }

17.LessonFragmentThree文件代码

  1. package com.example.fragmentproject.fragment;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import androidx.annotation.NonNull;
  5. import androidx.annotation.Nullable;
  6. import androidx.recyclerview.widget.GridLayoutManager;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. import com.example.fragmentproject.R;
  9. import com.example.fragmentproject.adapter.ListAdapter;
  10. import com.example.fragmentproject.base.BaseFragment;
  11. import com.example.fragmentproject.bean.ListDataBean;
  12. import java.util.ArrayList;
  13. public class LessonFragmentThree extends BaseFragment {
  14. private RecyclerView mRecyclerView;
  15. private ListAdapter listAdapter;
  16. private ArrayList<ListDataBean> arrayList=new ArrayList<>();
  17. private String[] headline={"国办通报政府网站、政务新媒体检查结果","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  18. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  19. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  20. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  21. };
  22. private String[] time={"2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00",
  23. "2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00"};
  24. @Override
  25. public void onCreate(Bundle savedInstanceState){
  26. super.onCreate(savedInstanceState);
  27. }
  28. @Override
  29. protected int getLayoutId(){
  30. return R.layout.activity_lesson_fragment_three;
  31. }
  32. @Override
  33. protected void initView(){
  34. for (int i=0;i<8;i++){
  35. ListDataBean listDataBean=new ListDataBean(headline[i],time[i]);
  36. arrayList.add(listDataBean);
  37. }
  38. mRecyclerView=rootView.findViewById(R.id.rv_fra_three);
  39. listAdapter=new ListAdapter(getActivity(),arrayList);
  40. GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
  41. gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
  42. mRecyclerView.setLayoutManager(gridLayoutManager);
  43. mRecyclerView.setAdapter(listAdapter);
  44. }
  45. @Override
  46. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  47. super.onViewCreated(view, savedInstanceState);
  48. }
  49. }

18.LessonFragmentFour文件代码

  1. package com.example.fragmentproject.fragment;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import androidx.annotation.NonNull;
  5. import androidx.annotation.Nullable;
  6. import androidx.recyclerview.widget.GridLayoutManager;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. import com.example.fragmentproject.R;
  9. import com.example.fragmentproject.adapter.ListAdapter;
  10. import com.example.fragmentproject.base.BaseFragment;
  11. import com.example.fragmentproject.bean.ListDataBean;
  12. import java.util.ArrayList;
  13. public class LessonFragmentFour extends BaseFragment {
  14. private RecyclerView mRecyclerView;
  15. private ListAdapter listAdapter;
  16. private ArrayList<ListDataBean> arrayList=new ArrayList<>();
  17. private String[] headline={"政务新媒体检查结果 网上办尖办成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  18. "国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府","国办通报政府网站、政务新媒体检查结果 网上办尖办”成态国办通报政府",
  19. };
  20. private String[] time={"2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00","2020-12-17 09:00"};
  21. @Override
  22. public void onCreate(Bundle savedInstanceState){
  23. super.onCreate(savedInstanceState);
  24. }
  25. @Override
  26. protected int getLayoutId(){
  27. return R.layout.activity_lesson_fragment_four;
  28. }
  29. @Override
  30. protected void initView(){
  31. for (int i=0;i<4;i++){
  32. ListDataBean listDataBean=new ListDataBean(headline[i],time[i]);
  33. arrayList.add(listDataBean);
  34. }
  35. mRecyclerView=rootView.findViewById(R.id.rv_fra_four);
  36. listAdapter=new ListAdapter(getActivity(),arrayList);
  37. GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
  38. gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
  39. mRecyclerView.setLayoutManager(gridLayoutManager);
  40. mRecyclerView.setAdapter(listAdapter);
  41. }
  42. @Override
  43. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  44. super.onViewCreated(view, savedInstanceState);
  45. }
  46. }

19.结果图:
image.png
20.设计图:
image.png