BottomNavigation

  • 导航布局navigation.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <navigation 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:id="@+id/navigation"
  6. app:startDestination="@id/firstFragment">
  7. <fragment
  8. android:id="@+id/firstFragment"
  9. android:name="com.example.mybottomnavigation.fragment.FirstFragment"
  10. android:label="旋转"
  11. tools:layout="@layout/first_fragment" />
  12. <fragment
  13. android:id="@+id/thirdFragment"
  14. android:name="com.example.mybottomnavigation.fragment.ThirdFragment"
  15. android:label="移动"
  16. tools:layout="@layout/third_fragment" />
  17. <fragment
  18. android:id="@+id/secondFragment"
  19. android:name="com.example.mybottomnavigation.fragment.SecondFragment"
  20. android:label="缩放"
  21. tools:layout="@layout/second_fragment" />
  22. </navigation>
  • 菜单布局menu.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item
  4. android:id="@+id/firstFragment"
  5. android:icon="@drawable/ic_baseline_looks_one_24"
  6. android:title="旋转" />
  7. <item
  8. android:id="@+id/secondFragment"
  9. android:icon="@drawable/ic_baseline_looks_two_24"
  10. android:title="缩放" />
  11. <item
  12. android:id="@+id/thirdFragment"
  13. android:icon="@drawable/ic_baseline_looks_3_24"
  14. android:title="移动" />
  15. </menu>
  • activity_main.xml
  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=".activity.MainActivity">
  8. <fragment
  9. android:id="@+id/fragment"
  10. android:name="androidx.navigation.fragment.NavHostFragment"
  11. android:layout_width="0dp"
  12. android:layout_height="0dp"
  13. app:defaultNavHost="true"
  14. app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
  15. app:layout_constraintEnd_toEndOf="parent"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent"
  18. app:navGraph="@navigation/navigation" />
  19. <com.google.android.material.bottomnavigation.BottomNavigationView
  20. android:id="@+id/bottomNavigationView"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. app:layout_constraintBottom_toBottomOf="parent"
  24. app:layout_constraintEnd_toEndOf="parent"
  25. app:layout_constraintStart_toStartOf="parent"
  26. app:menu="@menu/menu" />
  27. </androidx.constraintlayout.widget.ConstraintLayout>
  • first_fragment.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.FirstFragment">
  8. <ImageView
  9. android:id="@+id/imageView1"
  10. android:layout_width="100dp"
  11. android:layout_height="100dp"
  12. android:layout_gravity="center"
  13. android:src="@drawable/ic_baseline_pedal_bike_24" />
  14. </FrameLayout>
  • MainActivity.java
  1. package com.example.mybottomnavigation.activity;
  2. import android.os.Bundle;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.navigation.NavController;
  5. import androidx.navigation.Navigation;
  6. import androidx.navigation.ui.AppBarConfiguration;
  7. import androidx.navigation.ui.NavigationUI;
  8. import com.example.mybottomnavigation.R;
  9. import com.google.android.material.bottomnavigation.BottomNavigationView;
  10. public class MainActivity extends AppCompatActivity {
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
  16. NavController navController = Navigation.findNavController(this, R.id.fragment);
  17. // AppBarConfiguration configuration = new AppBarConfiguration.Builder(navController.getGraph()).build(); // 向上按钮会显示
  18. // AppBarConfiguration configuration = new AppBarConfiguration.Builder(R.id.firstFragment, R.id.secondFragment, R.id.thirdFragment).build();
  19. AppBarConfiguration configuration = new AppBarConfiguration.Builder(bottomNavigationView.getMenu()).build();
  20. NavigationUI.setupActionBarWithNavController(this, navController, configuration);
  21. NavigationUI.setupWithNavController(bottomNavigationView, navController);
  22. }
  23. }
  • FirstFragment.java
  1. package com.example.mybottomnavigation.fragment;
  2. import androidx.lifecycle.ViewModelProvider;
  3. import android.animation.ObjectAnimator;
  4. import android.os.Bundle;
  5. import androidx.annotation.NonNull;
  6. import androidx.annotation.Nullable;
  7. import androidx.fragment.app.Fragment;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ImageView;
  12. import com.example.mybottomnavigation.viewmodel.FirstViewModel;
  13. import com.example.mybottomnavigation.R;
  14. public class FirstFragment extends Fragment {
  15. private FirstViewModel mViewModel;
  16. private View root;
  17. private ImageView imageView;
  18. public static FirstFragment newInstance() {
  19. return new FirstFragment();
  20. }
  21. @Override
  22. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
  23. @Nullable Bundle savedInstanceState) {
  24. root = inflater.inflate(R.layout.first_fragment, container, false);
  25. imageView = root.findViewById(R.id.imageView1);
  26. return root;
  27. }
  28. @Override
  29. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  30. super.onActivityCreated(savedInstanceState);
  31. // mViewModel = new ViewModelProvider(this).get(FirstViewModel.class); // 范围是fragment,切换到别的fragment再回来数据就没了
  32. mViewModel = new ViewModelProvider(requireActivity()).get(FirstViewModel.class);
  33. imageView.setRotation(mViewModel.rotationPosition);
  34. // 属性动画
  35. ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0, 0);
  36. objectAnimator.setDuration(500);
  37. imageView.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View view) {
  40. if (!objectAnimator.isRunning()) {
  41. objectAnimator.setFloatValues(imageView.getRotation(), imageView.getRotation() + 100);
  42. mViewModel.rotationPosition += 100;
  43. objectAnimator.start();
  44. }
  45. }
  46. });
  47. }
  48. }
  • SecondFragment.java
  1. package com.example.mybottomnavigation.fragment;
  2. import androidx.lifecycle.ViewModelProvider;
  3. import android.animation.AnimatorSet;
  4. import android.animation.ObjectAnimator;
  5. import android.os.Bundle;
  6. import androidx.annotation.NonNull;
  7. import androidx.annotation.Nullable;
  8. import androidx.fragment.app.Fragment;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.ImageView;
  13. import com.example.mybottomnavigation.R;
  14. import com.example.mybottomnavigation.viewmodel.SecondViewModel;
  15. public class SecondFragment extends Fragment {
  16. private ImageView imageView;
  17. private View root;
  18. private SecondViewModel mViewModel;
  19. public static SecondFragment newInstance() {
  20. return new SecondFragment();
  21. }
  22. @Override
  23. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
  24. @Nullable Bundle savedInstanceState) {
  25. root = inflater.inflate(R.layout.second_fragment, container, false);
  26. imageView = root.findViewById(R.id.imageView2);
  27. return root;
  28. }
  29. @Override
  30. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  31. super.onActivityCreated(savedInstanceState);
  32. mViewModel = new ViewModelProvider(requireActivity()).get(SecondViewModel.class);
  33. // 初始化
  34. imageView.setScaleX(mViewModel.scaleFactor);
  35. imageView.setScaleY(mViewModel.scaleFactor);
  36. ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(imageView, "scaleX", 0);
  37. ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(imageView, "scaleY", 0);
  38. // 一起操作
  39. AnimatorSet animatorSet = new AnimatorSet();
  40. animatorSet.playTogether(objectAnimatorX, objectAnimatorY);
  41. animatorSet.setDuration(500);
  42. // objectAnimatorX.setDuration(500);
  43. // objectAnimatorY.setDuration(500);
  44. imageView.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View view) {
  47. if (!animatorSet.isRunning()){
  48. objectAnimatorX.setFloatValues(imageView.getScaleX() + 0.1f);
  49. objectAnimatorY.setFloatValues(imageView.getScaleY() + 0.1f);
  50. mViewModel.scaleFactor += 0.1;
  51. animatorSet.start();
  52. }
  53. }
  54. });
  55. }
  56. }
  • ThirdFragment.java
  1. package com.example.mybottomnavigation.fragment;
  2. import androidx.lifecycle.ViewModelProvider;
  3. import android.animation.ObjectAnimator;
  4. import android.os.Bundle;
  5. import androidx.annotation.NonNull;
  6. import androidx.annotation.Nullable;
  7. import androidx.fragment.app.Fragment;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ImageView;
  12. import com.example.mybottomnavigation.R;
  13. import com.example.mybottomnavigation.viewmodel.ThirdViewModel;
  14. import java.util.Random;
  15. public class ThirdFragment extends Fragment {
  16. private ImageView imageView;
  17. private View root;
  18. private ThirdViewModel mViewModel;
  19. public static ThirdFragment newInstance() {
  20. return new ThirdFragment();
  21. }
  22. @Override
  23. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
  24. @Nullable Bundle savedInstanceState) {
  25. root = inflater.inflate(R.layout.third_fragment, container, false);
  26. imageView = root.findViewById(R.id.imageView3);
  27. return root;
  28. }
  29. @Override
  30. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  31. super.onActivityCreated(savedInstanceState);
  32. mViewModel = new ViewModelProvider(requireActivity()).get(ThirdViewModel.class);
  33. // 初始化
  34. imageView.setX(mViewModel.dx);
  35. ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "x", 0, 0);
  36. objectAnimator.setDuration(500);
  37. imageView.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View view) {
  40. if (!objectAnimator.isRunning()) {
  41. float dx = new Random().nextBoolean() ? 100 : -100;
  42. objectAnimator.setFloatValues(imageView.getX(), imageView.getX() + dx);
  43. objectAnimator.start();
  44. mViewModel.dx += dx;
  45. }
  46. }
  47. });
  48. }
  49. }
  • FirstViewModel.java
  1. package com.example.mybottomnavigation.viewmodel;
  2. import androidx.lifecycle.ViewModel;
  3. public class FirstViewModel extends ViewModel {
  4. public float rotationPosition = 0;
  5. }
  • SecondViewModel.java
  1. package com.example.mybottomnavigation.viewmodel;
  2. import androidx.lifecycle.ViewModel;
  3. public class SecondViewModel extends ViewModel {
  4. public float scaleFactor = 1;
  5. }
  • ThirdViewModel.java
  1. package com.example.mybottomnavigation.viewmodel;
  2. import androidx.lifecycle.ViewModel;
  3. public class ThirdViewModel extends ViewModel {
  4. public float dx;
  5. }

BottomNavigation的更多相关文章

  1. Jetpack Compose之隐藏Scaffold的BottomNavigation

    做主页导航时会用到底部导航栏,Jetpack Compose提供了基础槽位的布局Scaffold,使用Scaffold可以构建底部导航栏,例如: @Composable fun Greeting(vm ...

  2. 自定义CoordinatorLayout Behavior 隐藏Footer View

    在用新的控件中,我们可以用Toolbar与CoordinatorLayout实现 向上滚动隐藏的效果,可是官方并没有找到向上隐藏底部导航的功能,有一些第三方的框架实现了. 在Android M,Coo ...

  3. bottomNavigationBar 底部导航tab MD

    1.先上图: 此底部Tab完全可以满足日常的开发 2.使用: 很简单,使用Gradle构建:compile ‘com.ashokvarma.android:bottom-navigation-bar: ...

  4. Android底部导航栏创建——ViewPager + RadioGroup

    原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...

  5. Android开源项目库汇总

    最近做了一个Android开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个star. 抽 ...

  6. Android开发之漫漫长途 XIII——Fragment最佳实践

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  7. GitHub上受欢迎的Android UI Library

    GitHub上受欢迎的Android UI Library 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 Vi ...

  8. Android BottomNavigationBar底部导航控制器的使用(包含默认postion的设置)

    转载请标明出处:http://blog.csdn.net/u010046908/article/details/50962081本文出自:[李东的博客] 最近Google在自己推出的Material ...

  9. BottomNavigationBar使用详解

    gitHub地址:https://github.com/Ashok-Varma/BottomNavigation 一.基本使用 1.在AndroidStudio下添加依赖: compile 'com. ...

  10. Android UI相关开源项目库汇总

    最近做了一个Android UI相关开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个st ...

随机推荐

  1. 华为最高学术成果发表 —— 《Nature》正刊发表论文《Accurate medium-range global weather forecasting with 3D neural networks》

    论文<Accurate medium-range global weather forecasting with 3D neural networks>的<Nature>地址: ...

  2. 【转载】 NeuroEvolution with MarI/O —— 使用人工智能来通关超级玛丽

    原文地址: http://glenn-roberts.com/posts/tech/2015/07/08/neuroevolution-with-mario.html  参考: https://v.q ...

  3. 数据结构 分块 & 莫队

    分块 一种优化暴力的思想. 通常是将原数据划分成适当块(一般为 \(\sqrt{n}\)),对每块数据进行预处理,进而达到比暴力更优的时间复杂度. 划分 确定块长后,一般需要开两个数组存储每一块的右边 ...

  4. Vim:从光标位置开始全局搜索和替换

    /\v SEARCHTERM :%s/\vBEFORE/AFTER/gc

  5. Win32 ListCtrl控件点击列标题排序

    1.在WM_NOTIFY消息中捕获鼠标点点击列的索引 int MyMainDialog::OnNotify(NMHDR* pNMhdr) { if (pNMhdr->idFrom == IDC_ ...

  6. 零基础学习人工智能—Python—Pytorch学习(九)

    前言 本文主要介绍卷积神经网络的使用的下半部分. 另外,上篇文章增加了一点代码注释,主要是解释(w-f+2p)/s+1这个公式的使用. 所以,要是这篇文章的代码看不太懂,可以翻一下上篇文章. 代码实现 ...

  7. Java Swing Loading转圈的进度提示框

    Java Swing Loading转圈的进度提示框 具体只需要两个类 AnimatedPanel.java InfiniteProgressPanel.java 前因:我们开发的web应用,有个奇葩 ...

  8. 记 某List.sort()后排序结果异常

    背景:某次查看日志,发现数据不符合预期,希望获取的是降序排序,但是部分数据是乱序的 已知List.sort()方法应该不会出异常,所以应该是判断先后方法出问题了 果然,因为一开始写代码时,没有考虑到差 ...

  9. 修改python打包后的窗体图标、任务栏图标、exe图标

    前言 我python开发的GUI界面(图形用户界面)一直是tkinter,打包exe一直是Pyinstaller.但是打包出来的exe图标.状态栏图标.窗体左上角图标一直是默认的羽毛,我想自定义. 效 ...

  10. 五子棋AI:实现逻辑与相关背景探讨(上)

    绪论 本合集将详细讲述如何实现基于群只能遗传算法的五子棋AI,采用C++作为底层编程语言 本篇将简要讨论实现思路,并在后续的文中逐一展开 了解五子棋 五子棋规则 五子棋是一种经典的棋类游戏,规则简单却 ...