我点击导航栏中的项目底部导航栏我正在替换片段.我有3个片段A,B,C所以点击b项B片段加载,在B我调用3-4个API.所以,现在如果我转到C然后再次来到B,就会创建一个新的B Fragment实例,并再次调用这些API,如何保存片段实例状态,而不是在更改片段时再次调用API.这是我的代码.
mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Fragment currentLoaded = fgMan.findFragmentById(R.id.container_body);
switch (id) {
case R.id.nearby_fragment:
if (!(currentLoaded instanceof SpotFeedMapFragment)) {
removeScroll();
mNearByFragment = fgMan.findFragmentByTag(NEARBY_FRAGMENT_TAG) != null ? fgMan.findFragmentByTag(NEARBY_FRAGMENT_TAG) : mNearByFragment;
fgMan.beginTransaction().setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out);
fgMan.beginTransaction().replace(R.id.container_body, mNearByFragment, NEARBY_FRAGMENT_TAG).commit();
fgMan.executePendingTransactions();
getSupportActionBar().setTitle(getString(R.string.nearby_fragment));
}
break;
case R.id.route_fragment:
if (!(currentLoaded instanceof BusLocationsFragment)) {
if (!inParent) {
mRl.removeView(fixLayout);
p.addRule(RelativeLayout.BELOW, toolbar.getId());
scrollView.setLayoutParams(p);
scrollView.addView(fixLayout);
mRl.addView(scrollView);
inParent = true;
}
//mFragment = new BusLocationsFragment();
mBusLocFragment = fgMan.findFragmentByTag(BUS_LOC_FRAGMENT_TAG) != null ? fgMan.findFragmentByTag(BUS_LOC_FRAGMENT_TAG) …Run Code Online (Sandbox Code Playgroud) android navigationbar android-fragments bottomnavigationview
一周前BottomNavigationView发布时,我真的很开心,但是我遇到了一些问题,这使得我无法解决它,就像在BottomNavigationView上看到阴影一样,就像谷歌照片Android应用程序向我们展示的那样:
如果我们点击Google相册菜单项,我们就会看到一个涟漪效果,就像图标和文字颜色(选中时)一样呈蓝色.
仅实施Google提供的解决方案会显示灰色涟漪效果颜色,更糟糕的是,当我们更改bottomnavigationview(design:itemBackground="...")的背景颜色时,它不会显示.
有人知道怎么解决吗?
我使用BottomNavigationView来切换片段.如何获取当前选中的菜单项,以防止重新打开片段?
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_1:
// open fragment 1
break;
case R.id.action_2:
// open fragment 2
break;
case R.id.action_3:
// open fragment 3
break;
}
return false;
}
});
}
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序上集成了Bottom Bar导航栏.但是当我滑动时,标签的颜色不会改变.因为我有选择器文件,这很奇怪.有什么想法解决这个问题吗?
Activity.java
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.bb_menu_arac:
fragment = new AraclarimFragment();
break;
case R.id.bb_menu_yaklasan:
fragment = new YaklasanlarFragment();
break;
case R.id.bb_menu_yakin:
fragment = new Yakinimdakiler();
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/beyaz" android:state_enabled="true" />
<item android:color="@color/colorPrimaryDark" android:state_enabled="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)
activiy.xml
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/beyaz"
app:itemTextColor="@color/beyaz"
app:menu="@menu/bottombar_menu" …Run Code Online (Sandbox Code Playgroud) 我在切换到底部导航的最后一个片段时遇到此错误,因此最后一个片段加载有点慢
E/FrameEvents: updateAcquireFence: Did not find frame. 2022-01-03 12:35:41.373 14186-15042/com.pandojo E/BufferQueueProducer: [ImageReader-329x48f1u256m2-14186-447](id:376a000001bf,api:0,p:-1,c:14186) disconnect: not connected (req=1) 2022-01-03 12:35:41.373 14186-15042/com.pandojo W/libEGL: EGLNativeWindowType 0x7b81c7d010 disconnect failed
android material-design bottomnavigationview android-bottomnav
我为我的片段尝试了Google支持库BottomNavigationView和Framelayout.
这是我的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.bottombarnavigation.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<include layout="@layout/toolbar"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.BottomNavigationView
android:background="#fcfcfc"
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:menu="@menu/bottom_navigation" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
当我在Fragment中填充我的Recyclerview时,它的内容被BottomNavigationView所掩盖.
我不知道为什么会这样.我浏览了其他人的教程,它运行正常.
编辑 这是我的content_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@android:color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.bottombarnavigation.MainActivity"
tools:showIn="@layout/activity_main">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 我在Android中使用最近由设计库25中的谷歌引入的底部布局导航样式.在我看到的所有教程和问题中,图标中的图像是正常大小,但是我的图像是非常小的,尽管事实是我保存到drawable文件夹的图像是72x72.这是一个截图:
图标应至少为2,甚至可能是该尺寸的3倍.我该怎么做?这是我在bottom_layout.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_home"
android:title="test"
android:icon="@drawable/tabbarglossary"
app:showAsAction="always|withText"
/>
<item
android:id="@+id/menu_search"
android:title="test2"
android:icon="@drawable/mediationtabbar"
app:showAsAction="always|withText"
/>
<item
android:id="@+id/menu_notifications"
android:title="test3"
android:icon="@drawable/ic_action_name"
app:showAsAction="always|withText"
/>
</menu>
Run Code Online (Sandbox Code Playgroud)
在我的activity_main.xml中:
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:focusable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
design:menu="@menu/bottom_layout" />
Run Code Online (Sandbox Code Playgroud)
谢谢
我想在我的项目中制作一个底部导航栏.每个视图都有自己的片段.问题是,每次我点击按钮更改视图,例如从最近更改为收藏夹,它会创建具有全新状态的新片段(例如滚动位置,文本更改我的片段包含的任何内容).我知道在官方Android文档中有人写道,底部导航栏应该重置任务状态,但我认为这对用户来说太不舒服了.我希望有一些像instagram这样的功能,你可以从feed更改为探索,然后返回到滚动位置,图像缓存一切都保存.我几乎尝试了解决这个问题的方法,唯一有效的方法就是根据情况设置可视性GONE和设置可见性VISIBLE但我明白这不是正确的方式应该有更好的方法这样做我不是在谈论手动保存所需的实例.我几乎每个关于底部导航片段的教程都会遵循,但有趣的是,没有人有兴趣在没有每次调用new的情况下使用它.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, FirstFragment.newInstance());
fragmentTransaction.commit();
bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.menu_dialer:
fragment = FirstFragment.newInstance();
break;
case R.id.menu_email:
fragment = SecondFragment.newInstance();
break;
case R.id.menu_map:
fragment = ThirdFragment.newInstance();
break;
}
if (fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, fragment);
fragmentTransaction.commit();
}
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
我也试过onAttach和Deattach解决方案,但再次没有成功.
视频链接:新的我试过Nino Handler版本它只在我点击相同的片段按钮时才有效
也许它是连接我使用金丝雀版本或我的gradle依赖项中的错误?

最新消息:
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private …Run Code Online (Sandbox Code Playgroud) 我遵循了 android.developers 的指南并navigation component在我的应用程序中实现了一个。当我需要一些屏幕带有或不带有工具栏/底部导航栏时,我偶然发现了一个问题。
Android 开发者示例的布局
<androidx.appcompat.widget.Toolbar
.../>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
.../>
Run Code Online (Sandbox Code Playgroud)
强迫我OnDestinationChanged在 MainActivity 的回调中隐藏/显示工具栏/底部导航栏:
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination) {
R.id.topLevelDestination-> {
toolbar.visibility = View.GONE
bottomNav.visibility = View.VISIBLE
}
R.id.lowLevelDestination -> {
toolbar.visibility = View.VISIBLE
bottomNav.visibility = View.GONE
}
Run Code Online (Sandbox Code Playgroud)
而且,当然,如果我这样做,我会在看到新片段之前调整布局大小。我的意思是我看到底部导航如何在片段 A 上消失,并且当片段 A 仍在屏幕上时,我在底部导航栏所在的位置看到片段的 B 部分,在该片段之后,B 出现。
如何解决?我需要嵌套的导航图吗?
更新:添加了问题的 gif
视频描述:它是我屏幕的一部分。在视频中,您可以看到系统 UI、底部导航栏和带有按钮的主要片段。当我单击按钮导航图时,我将导航到没有底部导航栏的目的地。所以,当 OnDestinationChanged 时,我会执行 bottomNavBar.hide()。如您所见,在我实际导航之前,bottomNavBar 消失了,并且在bottomnavBar 消失后,您可以看到我的目标片段的一部分可见。那就是问题所在。
navigation android android-toolbar bottomnavigationview android-architecture-navigation
我在片段中使用滚动视图,但是当键盘按原样显示时屏幕没有向上滚动。这是场景:我有 mainActivity,它有底部导航栏和一个框架布局,它用作活动的容器。这是代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<android.support.design.widget.BottomNavigationView
android:background="@color/clear_white"
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
app:itemIconTint="@color/blue_navbar_icon"
app:itemTextColor="@color/blue_navbar_icon"
app:menu="@menu/navigation" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
片段屏幕有一些输入字段和按钮,它们包裹在约束布局中,然后约束布局包裹在 scrollView 中,以便在键盘处于活动状态时可以向上滚动。这是片段xml文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="@+id/app_bar_new"
layout="@layout/app_bar_new"
app:layout_constraintBottom_toTopOf="@+id/btn_get_location"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_get_location"
style="@style/btn_style"
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="11dp"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:layout_marginStart="11dp"
android:layout_marginTop="12dp"
android:text="@string/get_location_btn_str"
app:layout_constraintBottom_toTopOf="@+id/tv_long"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/app_bar_new" />
<EditText
android:id="@+id/et_name_of_attr"
style="@style/input_field_style"
android:layout_width="0dp"
android:layout_height="47dp"
android:layout_marginBottom="7dp"
android:layout_marginEnd="11dp"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp" …Run Code Online (Sandbox Code Playgroud) android ×10
android-architecture-navigation ×1
java ×1
navigation ×1
scrollview ×1
shadow ×1
xml ×1