• Android碎片Fragment之多标签切换效果


    目录

    一、效果展示

    二、整体结构

    三、代码


    一、效果展示

    二、整体结构

      

    三、代码

    build.gradle

    1. dependencies {
    2. //添加以下两行代码
    3. implementation fileTree(dir: 'libs', include: ['*.jar'])
    4. implementation 'androidx.recyclerview:recyclerview:1.2.1'
    5. implementation 'androidx.appcompat:appcompat:1.4.1'
    6. implementation 'com.google.android.material:material:1.5.0'
    7. implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    8. testImplementation 'junit:junit:4.13.2'
    9. androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    10. androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    11. }

    MainActivity.java

    1. package com.example.navigation;
    2. import android.graphics.Color;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.Button;
    6. import android.widget.LinearLayout;
    7. import androidx.appcompat.app.AppCompatActivity;
    8. import androidx.fragment.app.Fragment;
    9. import androidx.fragment.app.FragmentManager;
    10. import androidx.fragment.app.FragmentTransaction;
    11. import java.util.ArrayList;
    12. import java.util.List;
    13. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    14. public String[] btnTitles = new String[]{"微信","通信录", "发现","我"};// 按钮标题
    15. public List contextFragments = new ArrayList<>();// 用来存放Fragments的集合
    16. public LinearLayout linearLayout;
    17. @Override
    18. protected void onCreate(Bundle savedInstanceState) {
    19. super.onCreate(savedInstanceState);
    20. setContentView(R.layout.activity_main);
    21. init();// 初始化控件
    22. }
    23. private void init() {
    24. // 初始化按钮
    25. initButton();
    26. // 初始化Fragment
    27. initFragment();
    28. }
    29. public void initButton() {
    30. // 获取存放按钮的LinearLayout
    31. linearLayout = findViewById(R.id.buttonLayout);
    32. // 遍历按钮标题数组,动态添加按钮
    33. for (String btnStr: btnTitles) {
    34. Button btn = new Button(this);
    35. btn.setText(btnStr);
    36. btn.setTag(btnStr);// 存放Tag,值为按钮标题文本
    37. // 设置按钮样式
    38. btn.setBackgroundColor(Color.WHITE);
    39. LinearLayout.LayoutParams btnLayoutParams =
    40. new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
    41. // 添加点击事件
    42. btn.setOnClickListener(this);
    43. // 将按钮加入LinearLayout
    44. linearLayout.addView(btn, btnLayoutParams);
    45. }
    46. }
    47. /**
    48. * 初始化Fragment
    49. */
    50. public void initFragment() {
    51. // 获取FragmentManager
    52. FragmentManager fragmentManager = getSupportFragmentManager();
    53. // 开始事务管理
    54. FragmentTransaction transaction = fragmentManager.beginTransaction();
    55. // 添加按钮对应的Fragment
    56. for (String btnStr: btnTitles) {
    57. // 声明一个ContextFragment
    58. ContextFragment contextFragment = new ContextFragment();
    59. // 将ContextFragment添加到contextFrameLayout,并设置tag为按钮的标题
    60. // (这里的Tag和按钮的Tag是一样的,按钮点击事件中用按钮的Tag查找Fragment)
    61. transaction.add(R.id.contextFrameLayout, contextFragment, btnStr);
    62. // 设置ContextFragment中文本的值,这里用Bundle传值
    63. Bundle bundle = new Bundle();
    64. bundle.putString("textValue", btnStr);
    65. contextFragment.setArguments(bundle);
    66. // 将contextFragment加入Fragment集合中
    67. contextFragments.add(contextFragment);
    68. }
    69. // 提交事务
    70. transaction.commit();
    71. // 显示第一个Fragment,隐藏其它的Fragment
    72. showFragment(btnTitles[0]);
    73. }
    74. public void showFragment(String tag) {
    75. FragmentManager fragmentManager = getSupportFragmentManager();
    76. FragmentTransaction transaction = fragmentManager.beginTransaction();
    77. // 遍历contextFragments
    78. for (Fragment fragment: contextFragments) {
    79. if (fragment.getTag().equals(tag)) {// tag一样,显示Fragment
    80. transaction.show(fragment);
    81. } else {// 隐藏Fragment
    82. transaction.hide(fragment);
    83. }
    84. }
    85. transaction.commit();
    86. }
    87. @Override
    88. public void onClick(View view) {
    89. // 显示相应的Fragment
    90. showFragment(view.getTag().toString());
    91. }
    92. }

    ContextFragment.java 

    1. package com.example.navigation;
    2. import android.os.Bundle;
    3. import android.view.LayoutInflater;
    4. import android.view.View;
    5. import android.view.ViewGroup;
    6. import android.widget.TextView;
    7. import androidx.fragment.app.Fragment;
    8. public class ContextFragment extends Fragment {
    9. TextView textView;
    10. @Override
    11. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    12. View view = inflater.inflate(R.layout.context_fragment, container, false);
    13. // 获取文本控件
    14. textView = view.findViewById(R.id.content_text);
    15. // 获取Bundle,该对象是Activity创建Fragment时,传入的
    16. Bundle bundle = getArguments();
    17. if (bundle != null) {
    18. String textValue = bundle.getString("textValue");// 将文本框的值赋值为传入的textValue
    19. textView.setText(textValue);
    20. }
    21. return view;
    22. }
    23. }

     activity_main.xml

    1. "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:background="#FFFFFF"
    6. android:orientation="vertical">
    7. <FrameLayout
    8. android:id="@+id/contextFrameLayout"
    9. android:layout_width="match_parent"
    10. android:layout_height="0dp"
    11. android:layout_weight="1"
    12. android:background="#dadada">FrameLayout>
    13. <LinearLayout
    14. android:id="@+id/buttonLayout"
    15. android:layout_width="match_parent"
    16. android:layout_height="60dp"
    17. android:orientation="horizontal">LinearLayout>
    18. LinearLayout>

    context_fragment.xml

    1. "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="wrap_content"
    5. android:layout_gravity="center"
    6. android:orientation="vertical">
    7. <TextView
    8. android:id="@+id/content_text"
    9. android:layout_width="wrap_content"
    10. android:layout_height="wrap_content"
    11. android:layout_gravity="center_horizontal"
    12. android:text="内容"
    13. android:textSize="47sp" />
    14. LinearLayout>

    参考博客:Android碎片Fragment之多标签切换效果(微信和QQ底部多标签切换) - 简书 (jianshu.com)

  • 相关阅读:
    [附源码]计算机毕业设计基于Springboot的手机电商网站
    Jetson nano 安装Ubuntu20.04系统
    Docker镜像解析获取Dockerfile文件
    vue中动态引入图片为什么要是require, 你不知道的那些事
    R3LIVE代码详解(三)
    40.【C++最全文件操作,少一个你打我】
    Win11怎么禁止软件后台运行?Win11系统禁止应用在后台运行的方法
    优思学院|中质协六西格玛考试形式是什么样的?
    嵌入式软件开发之程序架构设计-任务调度
    MyBatis调用SqlServer存储过程
  • 原文地址:https://blog.csdn.net/qq_51165234/article/details/127655480