BottomNavigation
BottomNavigation
- 导航布局navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation"
app:startDestination="@id/firstFragment">
<fragment
android:id="@+id/firstFragment"
android:name="com.example.mybottomnavigation.fragment.FirstFragment"
android:label="旋转"
tools:layout="@layout/first_fragment" />
<fragment
android:id="@+id/thirdFragment"
android:name="com.example.mybottomnavigation.fragment.ThirdFragment"
android:label="移动"
tools:layout="@layout/third_fragment" />
<fragment
android:id="@+id/secondFragment"
android:name="com.example.mybottomnavigation.fragment.SecondFragment"
android:label="缩放"
tools:layout="@layout/second_fragment" />
</navigation>
- 菜单布局menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/firstFragment"
android:icon="@drawable/ic_baseline_looks_one_24"
android:title="旋转" />
<item
android:id="@+id/secondFragment"
android:icon="@drawable/ic_baseline_looks_two_24"
android:title="缩放" />
<item
android:id="@+id/thirdFragment"
android:icon="@drawable/ic_baseline_looks_3_24"
android:title="移动" />
</menu>
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<fragment
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
- first_fragment.xml(另外两个类似)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.FirstFragment">
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/ic_baseline_pedal_bike_24" />
</FrameLayout>
- MainActivity.java
package com.example.mybottomnavigation.activity;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.mybottomnavigation.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
NavController navController = Navigation.findNavController(this, R.id.fragment);
// AppBarConfiguration configuration = new AppBarConfiguration.Builder(navController.getGraph()).build(); // 向上按钮会显示
// AppBarConfiguration configuration = new AppBarConfiguration.Builder(R.id.firstFragment, R.id.secondFragment, R.id.thirdFragment).build();
AppBarConfiguration configuration = new AppBarConfiguration.Builder(bottomNavigationView.getMenu()).build();
NavigationUI.setupActionBarWithNavController(this, navController, configuration);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
}
- FirstFragment.java
package com.example.mybottomnavigation.fragment;
import androidx.lifecycle.ViewModelProvider;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.mybottomnavigation.viewmodel.FirstViewModel;
import com.example.mybottomnavigation.R;
public class FirstFragment extends Fragment {
private FirstViewModel mViewModel;
private View root;
private ImageView imageView;
public static FirstFragment newInstance() {
return new FirstFragment();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.first_fragment, container, false);
imageView = root.findViewById(R.id.imageView1);
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// mViewModel = new ViewModelProvider(this).get(FirstViewModel.class); // 范围是fragment,切换到别的fragment再回来数据就没了
mViewModel = new ViewModelProvider(requireActivity()).get(FirstViewModel.class);
imageView.setRotation(mViewModel.rotationPosition);
// 属性动画
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0, 0);
objectAnimator.setDuration(500);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!objectAnimator.isRunning()) {
objectAnimator.setFloatValues(imageView.getRotation(), imageView.getRotation() + 100);
mViewModel.rotationPosition += 100;
objectAnimator.start();
}
}
});
}
}
- SecondFragment.java
package com.example.mybottomnavigation.fragment;
import androidx.lifecycle.ViewModelProvider;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.mybottomnavigation.R;
import com.example.mybottomnavigation.viewmodel.SecondViewModel;
public class SecondFragment extends Fragment {
private ImageView imageView;
private View root;
private SecondViewModel mViewModel;
public static SecondFragment newInstance() {
return new SecondFragment();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.second_fragment, container, false);
imageView = root.findViewById(R.id.imageView2);
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = new ViewModelProvider(requireActivity()).get(SecondViewModel.class);
// 初始化
imageView.setScaleX(mViewModel.scaleFactor);
imageView.setScaleY(mViewModel.scaleFactor);
ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(imageView, "scaleX", 0);
ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(imageView, "scaleY", 0);
// 一起操作
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimatorX, objectAnimatorY);
animatorSet.setDuration(500);
// objectAnimatorX.setDuration(500);
// objectAnimatorY.setDuration(500);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!animatorSet.isRunning()){
objectAnimatorX.setFloatValues(imageView.getScaleX() + 0.1f);
objectAnimatorY.setFloatValues(imageView.getScaleY() + 0.1f);
mViewModel.scaleFactor += 0.1;
animatorSet.start();
}
}
});
}
}
- ThirdFragment.java
package com.example.mybottomnavigation.fragment;
import androidx.lifecycle.ViewModelProvider;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.mybottomnavigation.R;
import com.example.mybottomnavigation.viewmodel.ThirdViewModel;
import java.util.Random;
public class ThirdFragment extends Fragment {
private ImageView imageView;
private View root;
private ThirdViewModel mViewModel;
public static ThirdFragment newInstance() {
return new ThirdFragment();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.third_fragment, container, false);
imageView = root.findViewById(R.id.imageView3);
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = new ViewModelProvider(requireActivity()).get(ThirdViewModel.class);
// 初始化
imageView.setX(mViewModel.dx);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "x", 0, 0);
objectAnimator.setDuration(500);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!objectAnimator.isRunning()) {
float dx = new Random().nextBoolean() ? 100 : -100;
objectAnimator.setFloatValues(imageView.getX(), imageView.getX() + dx);
objectAnimator.start();
mViewModel.dx += dx;
}
}
});
}
}
- FirstViewModel.java
package com.example.mybottomnavigation.viewmodel;
import androidx.lifecycle.ViewModel;
public class FirstViewModel extends ViewModel {
public float rotationPosition = 0;
}
- SecondViewModel.java
package com.example.mybottomnavigation.viewmodel;
import androidx.lifecycle.ViewModel;
public class SecondViewModel extends ViewModel {
public float scaleFactor = 1;
}
- ThirdViewModel.java
package com.example.mybottomnavigation.viewmodel;
import androidx.lifecycle.ViewModel;
public class ThirdViewModel extends ViewModel {
public float dx;
}
BottomNavigation的更多相关文章
- Jetpack Compose之隐藏Scaffold的BottomNavigation
做主页导航时会用到底部导航栏,Jetpack Compose提供了基础槽位的布局Scaffold,使用Scaffold可以构建底部导航栏,例如: @Composable fun Greeting(vm ...
- 自定义CoordinatorLayout Behavior 隐藏Footer View
在用新的控件中,我们可以用Toolbar与CoordinatorLayout实现 向上滚动隐藏的效果,可是官方并没有找到向上隐藏底部导航的功能,有一些第三方的框架实现了. 在Android M,Coo ...
- bottomNavigationBar 底部导航tab MD
1.先上图: 此底部Tab完全可以满足日常的开发 2.使用: 很简单,使用Gradle构建:compile ‘com.ashokvarma.android:bottom-navigation-bar: ...
- Android底部导航栏创建——ViewPager + RadioGroup
原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...
- Android开源项目库汇总
最近做了一个Android开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个star. 抽 ...
- Android开发之漫漫长途 XIII——Fragment最佳实践
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- GitHub上受欢迎的Android UI Library
GitHub上受欢迎的Android UI Library 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 Vi ...
- Android BottomNavigationBar底部导航控制器的使用(包含默认postion的设置)
转载请标明出处:http://blog.csdn.net/u010046908/article/details/50962081本文出自:[李东的博客] 最近Google在自己推出的Material ...
- BottomNavigationBar使用详解
gitHub地址:https://github.com/Ashok-Varma/BottomNavigation 一.基本使用 1.在AndroidStudio下添加依赖: compile 'com. ...
- Android UI相关开源项目库汇总
最近做了一个Android UI相关开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个st ...
随机推荐
- 华为最高学术成果发表 —— 《Nature》正刊发表论文《Accurate medium-range global weather forecasting with 3D neural networks》
论文<Accurate medium-range global weather forecasting with 3D neural networks>的<Nature>地址: ...
- 【转载】 NeuroEvolution with MarI/O —— 使用人工智能来通关超级玛丽
原文地址: http://glenn-roberts.com/posts/tech/2015/07/08/neuroevolution-with-mario.html 参考: https://v.q ...
- 数据结构 分块 & 莫队
分块 一种优化暴力的思想. 通常是将原数据划分成适当块(一般为 \(\sqrt{n}\)),对每块数据进行预处理,进而达到比暴力更优的时间复杂度. 划分 确定块长后,一般需要开两个数组存储每一块的右边 ...
- Vim:从光标位置开始全局搜索和替换
/\v SEARCHTERM :%s/\vBEFORE/AFTER/gc
- Win32 ListCtrl控件点击列标题排序
1.在WM_NOTIFY消息中捕获鼠标点点击列的索引 int MyMainDialog::OnNotify(NMHDR* pNMhdr) { if (pNMhdr->idFrom == IDC_ ...
- 零基础学习人工智能—Python—Pytorch学习(九)
前言 本文主要介绍卷积神经网络的使用的下半部分. 另外,上篇文章增加了一点代码注释,主要是解释(w-f+2p)/s+1这个公式的使用. 所以,要是这篇文章的代码看不太懂,可以翻一下上篇文章. 代码实现 ...
- Java Swing Loading转圈的进度提示框
Java Swing Loading转圈的进度提示框 具体只需要两个类 AnimatedPanel.java InfiniteProgressPanel.java 前因:我们开发的web应用,有个奇葩 ...
- 记 某List.sort()后排序结果异常
背景:某次查看日志,发现数据不符合预期,希望获取的是降序排序,但是部分数据是乱序的 已知List.sort()方法应该不会出异常,所以应该是判断先后方法出问题了 果然,因为一开始写代码时,没有考虑到差 ...
- 修改python打包后的窗体图标、任务栏图标、exe图标
前言 我python开发的GUI界面(图形用户界面)一直是tkinter,打包exe一直是Pyinstaller.但是打包出来的exe图标.状态栏图标.窗体左上角图标一直是默认的羽毛,我想自定义. 效 ...
- 五子棋AI:实现逻辑与相关背景探讨(上)
绪论 本合集将详细讲述如何实现基于群只能遗传算法的五子棋AI,采用C++作为底层编程语言 本篇将简要讨论实现思路,并在后续的文中逐一展开 了解五子棋 五子棋规则 五子棋是一种经典的棋类游戏,规则简单却 ...