- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在迁移到 android 导航组件,当在 BottomNavigationView 中选择一个新项目时,无法使 fragment 转换正常工作。我按照官方文档中的说明进行操作,但没有发现所选 fragment 未显示的任何问题。
在 onCreate 方法的 Activity 中设置导航 Controller :
NavController navController = Navigation.findNavController(this, R.id.fragment_main_layout_nav_host);
NavigationUI.setupActionBarWithNavController(this, navController);
NavigationUI.setupWithNavController(binding.includedAppbarMain.bottomNavigationViewMainAppbar, navController);
布局包含导航主机 fragment 和 BottomNavigationView:
<fragment
android:id="@+id/fragment_main_layout_nav_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/activity_main"
app:defaultNavHost="true"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view_main_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/activity_main_bottom_navigation" />
BottomNavigationView 的菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_activity_main_home"
android:title="Home"
android:enabled="true"
android:icon="@drawable/ic_home_24dp"/>
<item
android:id="@+id/action_activity_main_notebooks"
android:title="Notebooks"
android:enabled="true"
android:icon="@drawable/ic_file_24dp"/>
<item
android:id="@+id/action_activity_main_search"
android:title="Search"
android:enabled="true"
android:icon="@drawable/ic_search_24dp"/>
</menu>
导航:
<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/activity_main"
app:startDestination="@id/action_activity_main_home">
<fragment
android:id="@+id/action_activity_main_home"
android:name="com.inknotes.view.fragment.MainHomeFragment"
android:label="@string/main_vertical_navigation_home"
tools:layout="@layout/fragment_main_home" />
<fragment
android:id="@+id/action_activity_main_notebooks"
android:name="com.inknotes.view.fragment.MainNotebookFragment"
android:label="@string/main_vertical_navigation_notebooks"
tools:layout="@layout/fragment_main_notebook" />
<fragment
android:id="@+id/action_activity_main_search"
android:name="com.inknotes.view.fragment.MainSearchFragment"
android:label="@string/main_vertical_navigation_search"
tools:layout="@layout/fragment_main_search" />
</navigation>
菜单项和 fragment 的 ID 也匹配,我想不出为什么当我在 BottomNavigationView 中选择另一个项目时没有显示新 fragment 。
我做了一些更多的测试,发现从后台弹出 fragment 也不起作用,也许我的导航 Controller 有一些一般性问题?
MainActivity(我删除了一些东西,因为它太长了):
@MainActivityScope
public class MainActivity extends AppCompatActivity implements ComponentCallbacks2, MainActivityHandler,
SpeedDialView.OnActionSelectedListener, BottomNavigationView.OnNavigationItemSelectedListener {
// Static variables
public static final String EXTRA_PATH = "com.inknotes.EXTRA_PATH";
// Injected objects
@Inject MainHomeFragment mainHomeFragment;
@Inject MainFolderFragment mainFolderFragment;
@Inject MainNotebookFragment mainNotebookFragment;
@Inject MainSearchFragment mainSearchFragment;
@Inject MainFolderAddDialog mainFolderAddDialog;
@Inject MainNotebookAddDialog mainNotebookAddDialog;
@Inject MainNotebookActionModeCallback mainNotebookActionModeCallback;
@Inject MainFolderActionModeCallback mainFolderActionModeCallback;
@Inject FileHelper fileHelper;
@Inject ClipboardHelper clipboardHelper;
@Inject ViewModelProvider.Factory viewModelFactory;
@Inject MainVerticalNavigationAdapter mainVerticalNavigationAdapter;
@Inject XmlParser<OptionItem> xmlParser;
// Objects
public MainActivityComponent daggerMainActivityComponent;
private ActivityMainBinding binding;
private MainViewModel mainViewModel;
private MainFolderViewModel mainFolderViewModel;
private MainNotebookViewModel mainNotebookViewModel;
private GestureDetectorCompat gestureDetectorCompat;
private MenuItem searchMenuItem;
private SelectionTracker<Long> verticalNavigationSelectionTracker;
private NavController navController;
// Variables
private boolean isBackPressed = false;
// =============================================================================================
//region Base methods
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(
this,
R.layout.activity_main,
new BindingComponent(this));
binding.setLifecycleOwner(this);
// Set the main dagger component
daggerMainActivityComponent = ((InkNotesApplication) getApplication())
.component()
.mainActivityComponentFactory()
.create(this);
daggerMainActivityComponent.inject(this);
// Get all viewModels
mainViewModel = new ViewModelProvider(this, viewModelFactory).get(MainViewModel.class);
mainFolderViewModel = new ViewModelProvider(this, viewModelFactory).get(MainFolderViewModel.class);
mainNotebookViewModel = new ViewModelProvider(this, viewModelFactory).get(MainNotebookViewModel.class);
// Set the default file
mainViewModel.setDefaultFile(getExternalFilesDir("notes"));
mainViewModel.setCurrentFile(getExternalFilesDir("notes"));
gestureDetectorCompat = new GestureDetectorCompat(this, new GestureListener());
// Set variables of binding
binding.setHandler(this);
binding.setViewModel(mainViewModel);
// Setup main toolbar
setSupportActionBar(binding.includedAppbarMain.materialToolbarMainAppbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_24dp);
// Setup explorer toolbar
binding.includedAppbarFolder.materialToolbarFolder.setOnMenuItemClickListener(this::onOptionsItemSelected);
// Setup drawer and navigation layout
binding.navigationViewMainFolder.setVisibility(View.GONE);
if (binding.drawerLayoutMain != null) {
binding.drawerLayoutMain.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
binding.drawerLayoutMain.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
if (mainFolderActionModeCallback != null) {
mainFolderActionModeCallback.finish();
}
}
});
}
// Setup navigation
navController = Navigation.findNavController(this, R.id.fragment_main_layout_nav_host);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
navController.getGraph()
).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
if (binding.includedAppbarMain.bottomNavigationViewMainAppbar != null) {
NavigationUI.setupWithNavController(binding.includedAppbarMain.bottomNavigationViewMainAppbar, navController);
}
// Set the explorer and file card fragment
/*
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout_main_layout_fragment_container, mainHomeFragment).commit();*/
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout_main_folder_container, mainFolderFragment).commit();
// Setup the floating action button
binding.includedAppbarMain.speedDialViewMainAppbar.inflate(R.menu.activity_main_fab);
binding.includedAppbarMain.speedDialViewMainAppbar.setOnActionSelectedListener(this);
// Setup bottom navigation view
if (binding.includedAppbarMain.bottomNavigationViewMainAppbar != null) {
binding.includedAppbarMain.bottomNavigationViewMainAppbar.setOnNavigationItemSelectedListener(this);
}
// Setup the vertical navigation view
if (binding.recyclerViewMainVerticalNavigation != null) {
binding.recyclerViewMainVerticalNavigation.setAdapter(mainVerticalNavigationAdapter);
List<OptionItem> items = xmlParser.parse(getResources().getXml(R.xml.menu_main_vertical_navigation), OptionItem.class);
// Create the selection tracker
// Add observer to the selection tracker
mainVerticalNavigationAdapter.setSelectionTracker(verticalNavigationSelectionTracker);
verticalNavigationSelectionTracker.select((long) R.id.action_activity_main_vertical_navigation_home);
mainVerticalNavigationAdapter.submitList(items);
binding.includedAppbarMain.speedDialViewMainAppbar.setVisibility(View.INVISIBLE);
toggleFolderNavigationView(View.VISIBLE);
}
}
@Override
public void onBackPressed() {
if (binding.drawerLayoutMain != null) {
if (binding.drawerLayoutMain.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayoutMain.closeDrawer(GravityCompat.START);
}
}
super.onBackPressed();
}
@Override
public boolean onSupportNavigateUp() {
if (binding.drawerLayoutMain != null) {
return NavigationUI.navigateUp(navController, binding.drawerLayoutMain);
}
return super.onSupportNavigateUp();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetectorCompat.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent event) {
super.dispatchTouchEvent(event);
return gestureDetectorCompat.onTouchEvent(event);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectNavigationItem(item.getItemId());
return true;
}
//endregion
// =============================================================================================
//region Custom methods
private void selectNavigationItem(int id) {
switch (id) {
case R.id.action_activity_main_home:
case R.id.action_activity_main_vertical_navigation_home:
//navController.navigate(R.id.action_activity_main_home);
if (binding.drawerLayoutMain != null) {
binding.drawerLayoutMain.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(false);
if (searchMenuItem != null) {
searchMenuItem.collapseActionView();
}
binding.includedAppbarMain.appBarLayoutMainAppbar.setExpanded(true);
binding.includedAppbarMain.speedDialViewMainAppbar.setVisibility(View.INVISIBLE);
toggleFolderNavigationView(View.VISIBLE);
break;
case R.id.action_activity_main_notebooks:
case R.id.action_activity_main_vertical_navigation_notebooks:
//navController.navigate(R.id.action_activity_main_notebooks);
if (binding.drawerLayoutMain != null) {
binding.drawerLayoutMain.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
if (searchMenuItem != null) {
searchMenuItem.collapseActionView();
}
binding.includedAppbarMain.appBarLayoutMainAppbar.setExpanded(true);
binding.includedAppbarMain.speedDialViewMainAppbar.setVisibility(View.VISIBLE);
toggleFolderNavigationView(View.GONE);
break;
case R.id.action_activity_main_search:
case R.id.action_activity_main_vertical_navigation_search:
//navController.navigate(R.id.action_activity_main_search);
if (binding.drawerLayoutMain != null) {
binding.drawerLayoutMain.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(false);
if (searchMenuItem != null) {
searchMenuItem.expandActionView();
}
binding.includedAppbarMain.appBarLayoutMainAppbar.setExpanded(false);
binding.includedAppbarMain.speedDialViewMainAppbar.setVisibility(View.INVISIBLE);
toggleFolderNavigationView(View.VISIBLE);
break;
}
isBackPressed = false;
}
在 selectNavigationItem 方法中,您还可以看到我测试了使用 navController.navigate(R.id.action_activity_main_notebooks);
进行手动导航,该方法有效但弹出后台堆栈也无效。但是使用 NavigationUI.setupWithNavController(binding.includedAppbarMain.bottomNavigationViewMainAppbar, navController);
设置 BottomNavigationView 应该不需要调用 navigate。
主页 fragment :
@MainActivityScope
public class MainHomeFragment extends Fragment {
public final static String NAME = "MainHomeFragment";
@Inject
public MainHomeFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main_home, container, false);
}
}
笔记本 fragment :
@MainActivityScope
public class MainNotebookFragment extends Fragment {
// Static variables
public final static String NAME = "MainNotebookFragment";
private static final String ARG_COLUMN_COUNT = "column-count";
// Injected objects
@Inject ViewModelProvider.Factory viewModelFactory;
@Inject MainNotebookAdapter mainNotebookAdapter;
@Inject MainNotebookItemTouchHelperCallback mainNotebookItemTouchHelperCallback;
// Objects
private RecyclerView recyclerView;
private MainNotebookViewModel mainNotebookViewModel;
// Variables
private int columnCount = 4;
@Inject
public MainNotebookFragment() {
}
// =============================================================================================
//region Base methods
@Override
public void onCreate(Bundle savedInstanceState) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
columnCount = getResources().getInteger(R.integer.column_count_portrait);
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
columnCount = getResources().getInteger(R.integer.column_count_landscape);
}
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_notebook, container, false);
((MainActivity) requireActivity()).daggerMainActivityComponent.inject(this);
mainNotebookViewModel = new ViewModelProvider(requireActivity(), viewModelFactory).get(MainNotebookViewModel.class);
if (getArguments() != null) {
columnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
// Set the mainNotebookAdapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
recyclerView = (RecyclerView) view;
if (columnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, columnCount));
}
recyclerView.setAdapter(mainNotebookAdapter);
}
// Set the item touch helper
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(mainNotebookItemTouchHelperCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mainNotebookViewModel.getItems().observe(
getViewLifecycleOwner(),
fileCardItems -> mainNotebookAdapter.setItems(fileCardItems)
);
mainNotebookViewModel.getSelectedItems().observe(
getViewLifecycleOwner(),
selectedExplorerItems -> mainNotebookAdapter.setSelectedItems(selectedExplorerItems)
);
mainNotebookViewModel.getQueryText().observe(
getViewLifecycleOwner(),
queryText -> mainNotebookAdapter.getFilter().filter(queryText)
);
}
@Override
public void onResume() {
super.onResume();
if (!EventBus.getDefault().isRegistered(mainNotebookAdapter)) {
EventBus.getDefault().register(mainNotebookAdapter);
}
}
@Override
public void onPause() {
super.onPause();
EventBus.getDefault().unregister(mainNotebookAdapter);
}
//endregion
}
搜索 fragment :
@MainActivityScope
public class MainSearchFragment extends Fragment {
public final static String NAME = "MainSearchFragment";
@Inject
public MainSearchFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main_search, container, false);
}
}
最佳答案
我终于找到了解决方案,问题出在 onNavigationItemSelected
函数上。因此,从 MainActivity 中删除这两个部分可以解决问题:
if (binding.includedAppbarMain.bottomNavigationViewMainAppbar != null) {
binding.includedAppbarMain.bottomNavigationViewMainAppbar.setOnNavigationItemSelectedListener(this);
}
...
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectNavigationItem(item.getItemId());
return true;
}
或者我所做的,因为我仍然需要该函数,返回 NavigationUI.onNavDestinationSelected(item, navController)
而不是 true。
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectNavigationItem(item.getItemId());
return NavigationUI.onNavDestinationSelected(item, navController);
}
关于android - 带有导航组件的 BottomNavigationView - 选定的 fragment 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63220118/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!