关键词:
【中文标题】导航抽屉活动:在按钮单击时从片段移动到片段【英文标题】:Navigation Drawer Activity: move from fragment to fragment on button click 【发布时间】:2020-12-26 14:36:17 【问题描述】:通过单击按钮从片段移动到另一个片段,会导致以下内容重叠。当我点击按钮时,主页和幻灯片片段都会显示。
我使用HomeFragment.java
中的按钮从主页片段切换到幻灯片片段的代码
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>()
@Override
public void onChanged(@Nullable String s)
textView.setText(s);
);
Button button = root.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
requireActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.nav_host_fragment, new SlideshowFragment())
.commit();
);
return root;
fragment_home.xml
中按钮的 XML
<Button
android:id="@+id/button"
android:layout_
android:layout_
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.854"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.241" />
container
在content_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_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_
android:layout_
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
重现问题的步骤:
-
使用导航抽屉活动创建新项目
在
fragment_home.xml
中添加一个按钮
为HomeFragment.java
中的按钮创建监听器
单击按钮时单独显示幻灯片片段我缺少什么?
MainActivity.java
供参考
public class MainActivity extends AppCompatActivity
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
@Override
public boolean onSupportNavigateUp()
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
【问题讨论】:
你在哪里定义容器 nav_host_fragment? 在content_main.xml
.
你为什么要使用 navcontroller 还要自己替换片段?
【参考方案1】:
好的,你的问题,用 slide_show_container (FrameLayout) 替换 nav_host_fragment。 name 可以是相同的想法是用 Framelayout 替换片段。
在 Home 活动中创建添加此代码
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.nav_host_fragment, new HomeFragment())
.commit();
并删除之前建议的代码。
<FrameLayout
android:id="@+id/nav_host_fragment"
android:layout_
android:layout_
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
【讨论】:
使用slide_show_container
的结果也相同,因为两个片段都在重叠
更新了答案。将此块放在按钮下方的主页片段中。并使用 getChildFragmentManager() 代替 getragmentManager()
两者都在显示如何在主片段中单击按钮时打开导航抽屉?
】如何在主片段中单击按钮时打开导航抽屉?【英文标题】:Howtoopennavigationdraweronbuttonclickinmainfragment?【发布时间】:2013-10-2621:28:17【问题描述】:我制作了一个带有一个活动的应用程序,该活动使用导航抽屉打开许多不同的片... 查看详情
想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉(代码片段)
...我的抽屉活动旁边添加片段,以便在整个应用程序中看到导航抽屉。我已尝试过stackoverflow和其他许多方法,但仍然没有成功。然后我必须从第一个片段移动到第二个片段,依此类推,直到需要导航抽屉。当我从我的活动移动到... 查看详情
从片段中的按钮单击打开导航抽屉(Kotlin)
】从片段中的按钮单击打开导航抽屉(Kotlin)【英文标题】:OpenNavigationDrawerfromButtonClickwithinFragment(Kotlin)【发布时间】:2020-06-0108:45:48【问题描述】:我在片段中创建了一个自定义按钮,并且我试图在单击时打开导航抽屉在活动... 查看详情
如何在 Android 中单击 ImageView 时从一个片段移动到另一个片段?
...从一个片段移动到另一个片段,就像我们可以使用从一个活动移动到另一个活动一样Intent 查看详情
如何在单击 RecyclerView 项目时从一个片段移动到另一个片段
...含该项目的一些数据的详细信息片段。我已经知道如何在活动中使用意图,但我找不到与Recyc 查看详情
如何使用导航抽屉打开和关闭更改按钮图像
】如何使用导航抽屉打开和关闭更改按钮图像【英文标题】:HowdoIchangemybuttonimagewithnavigationdraweropeningandclosing【发布时间】:2013-12-1307:26:55【问题描述】:我的导航片段有一个导航按钮,该按钮变为活动状态并在单击时打开一个... 查看详情
单击导航抽屉的项目不会打开片段
】单击导航抽屉的项目不会打开片段【英文标题】:ClickingonitemofNavigationDrawerdoesn’topenfragments【发布时间】:2020-01-1516:46:05【问题描述】:我想使用AndroidStudio(v.3.5)的默认导航抽屉活动。创建此默认活动(新项目->导航抽屉活... 查看详情
在导航抽屉上调用不是片段活动的活动
】在导航抽屉上调用不是片段活动的活动【英文标题】:CallanActivitythatisnotafragmentactivityonNavigationDrawer【发布时间】:2022-01-1013:07:02【问题描述】:我是AndroidStudio的新手。我有一个导航抽屉,我试图调用一个不是片段活动的活动... 查看详情
导航抽屉的片段管理
】导航抽屉的片段管理【英文标题】:NavigationDrawer\'sFragmentManagement【发布时间】:2018-12-2119:56:08【问题描述】:我正在开发一个应用程序,我只使用1个主要活动和多个片段,包括ViewPager、自定义视频/图片库、全屏片段(没有工... 查看详情
导航抽屉,处理后退按钮以转到以前的片段?
】导航抽屉,处理后退按钮以转到以前的片段?【英文标题】:Navigationdrawer,handlingthebackbuttontogotopreviousfragments?【发布时间】:2014-02-0107:36:12【问题描述】:我正在使用内置的导航抽屉来运行我的应用程序。我不太清楚如何处理... 查看详情
带有许多片段的 Android 导航抽屉
】带有许多片段的Android导航抽屉【英文标题】:AndroidNavigationDrawerwithmanyfragments【发布时间】:2014-04-0512:16:41【问题描述】:我已经为此苦苦挣扎了一段时间,我想从我的问题的图表开始:三个导航抽屉按钮是我的基本活动的一... 查看详情
在导航抽屉管理的片段之间移动
】在导航抽屉管理的片段之间移动【英文标题】:MovingfromfragmenttofragmentmanagedbyNavigationDrawer【发布时间】:2021-04-2406:13:26【问题描述】:我从AndroidStudio模板中获取了NavigationDrawer。一切正常,但有几个与不理解此组件相关的问题... 查看详情
如何在导航抽屉中添加活动
】如何在导航抽屉中添加活动【英文标题】:HowtoaddactivityinNavigationDrawer【发布时间】:2021-07-0514:55:06【问题描述】:我正在尝试在我的导航抽屉中添加一个活动。我已经在我的主要活动中加入了带有项目id的switchcase的意图。我... 查看详情
如何从片段返回主要活动
...时间】:2021-03-0415:33:09【问题描述】:我成功实现了底部导航抽屉并使用了片段。现在我可以从抽屉菜单中切换片段。我的问题是,当我从抽屉菜单中打开一个项目(例如;关于开发人员)时,当用户单击后退按钮时,它会关闭... 查看详情
禁用导航抽屉,在片段中切换主页按钮/向上指示器
】禁用导航抽屉,在片段中切换主页按钮/向上指示器【英文标题】:Disablingnavigationdrawer,togglinghome-button/up-indicatorinfragments【发布时间】:2013-10-2616:29:00【问题描述】:设置我有一个Activity,它的contentView是DrawerLayout的一个实例,... 查看详情
导航抽屉背压片段无法加载(代码片段)
...ondrawerwithfragments。一开始,我将Homefragment显示为默认值。导航菜单上有选项。在2片段我有SwipeRefreshLayout。在获取所有recyclerview数据之前,我显示数据和不可见的SwipeRefreshLayout。其中一个片段(包括SwipeRefReshLayout)工作正常,但... 查看详情
工具栏 - 只有一个活动从抽屉切换到后退按钮
】工具栏-只有一个活动从抽屉切换到后退按钮【英文标题】:Toolbar-SwitchingfromdrawertobackbuttonwithonlyoneActivity【发布时间】:2015-04-1609:04:33【问题描述】:我一直在寻找有关如何在抽屉打开/关闭图标(从汉堡包到箭头)到简单的后... 查看详情
如何在 swift UI 中单击按钮时从 swift UI 导航到情节提要?
】如何在swiftUI中单击按钮时从swiftUI导航到情节提要?【英文标题】:HowtonavigatefromswiftUItostoryboardonbuttonclickinswiftUI?【发布时间】:2021-08-0112:12:51【问题描述】:我正在开发一个同时具有swiftUI和情节提要的应用程序。我在swiftUI中... 查看详情