我正在尝试使用这样的导航参数将类别数据(ArrayList)从一个片段发送到另一个片段。
fun setUpArgumentForSearchDestination(categories: ArrayList<Category>) {
val searchDestination = fragmentView.findNavController().graph.findNode(R.id.destination_search)
searchDestination?.addArgument(
"allCategories", NavArgument.Builder()
.setType(NavType.ParcelableArrayType(Category::class.java))
.setDefaultValue(categories)
.build())
}
Run Code Online (Sandbox Code Playgroud)
在搜索片段中,我从参数中收到数据,如下所示:
arguments?.let {
// I get error in the code below:
val categories = it.getParcelableArrayList<Category>("allCategories")
}
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
java.util.ArrayList 无法转换为 android.os.Parcelable[]
我试图找到答案,即使我不确定这个问题的原因,似乎我必须Category像这个线程一样自定义我的类:Read &writing arrays of Parcelable objects
但我是一个初学者,并不太理解该线程的答案。我已经尝试实施parcelable,但仍然不起作用。这是我的Category课
class Category() : Parcelable {
var id : Int = 0
var name : String = ""
var parentID : Int = 0
var imageURL : String = ""
constructor(parcel: …Run Code Online (Sandbox Code Playgroud) android kotlin android-navigation android-jetpack android-architecture-navigation
说我有 2 个片段。登录和主页。我将 Home 片段设置为以 userData 作为参数的全局目的地
如果用户还没有登录,那么它将从登录片段开始。登录并从服务器获取 userData 后,它将导航到 Home 并使用此代码传递 userData
val home = AuthenticationFragmentDirections.actionGlobalHomeFragment(userData)
Navigation.findNavController(view).navigate(home, navOptions)
Run Code Online (Sandbox Code Playgroud)
但问题是当用户已经登录并打开应用程序时。在这种情况下,他们将直接打开主页片段。我的应用程序因错误而崩溃
java.lang.IllegalArgumentException:缺少必需的参数“userData”并且没有 android:defaultValue
所以我假设我可以在像User? 这样的参数中设置可为空的数据类型,所以如果 Home 片段不是来自 Login 则不需要 userData
从这里阅读后,我设置了这样的论点来实现User?
<argument
android:name="userData"
android:defaultValue="@null"
app:argType="User"
app:nullable="true" />
Run Code Online (Sandbox Code Playgroud)
但问题是,当我将用户数据从登录片段发送到家时,它说我有太多参数
我在gradle中使用这个
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha04"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha04"
Run Code Online (Sandbox Code Playgroud) android android-navigation android-architecture-components android-architecture-navigation android-jetpack-navigation
我正在使用导航组件进行导航,但结果很奇怪。当我尝试从对话框片段(购物车)按下后退按钮时。当前片段堆栈图(Home -> Notification)清晰,如下所示。
当我按下购物车页面上的后退按钮时,我期望的是返回通知页面,而不是主页。
我没有在这些片段中添加任何自定义后退事件。看起来很奇怪,但我在官方中找不到任何相关文档。
感谢您对此问题的任何评论或建议
这是我的 mobile-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/mobile_navigation"
app:startDestination="@id/navigation_explore">
<fragment
android:id="@+id/navigation_explore"
android:name="com.cn29.hkbvmall.ui.home.HomeFragment"
tools:layout="@layout/fragment_explore" >
<action
android:id="@+id/action_navigation_explore_self"
app:destination="@id/navigation_explore"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popExitAnim="@anim/slide_out_right" />
<argument
android:name="activeCategoryId"
app:argType="integer"
android:defaultValue="0" />
</fragment>
<fragment
android:id="@+id/navigation_notifications"
android:name="com.cn29.hkbvmall.ui.notifications.NotificationsFragment"
tools:layout="@layout/fragment_notifications" />
<dialog
android:id="@+id/navigation_mylist"
android:name="com.cn29.hkbvmall.ui.mylist.MyListFragment"
tools:layout="@layout/fragment_mylist" />
<dialog
android:id="@+id/navigation_cart"
android:name="com.cn29.hkbvmall.ui.shoppingkart.GoodsListDialogFragment"
android:label="GoodsListDialogFragment"
tools:layout="@layout/fragment_shoppingkart"
/>
<dialog
app:moduleName="account"
android:id="@+id/navigation_account"
android:name="com.cn29.account.ui.AccountListDialogFragment"
tools:layout="@layout/dynamic_feature_install_fragment"
/>
</navigation>
Run Code Online (Sandbox Code Playgroud)
这是我的 MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var toolbar: Toolbar
private lateinit var appBarConfiguration: AppBarConfiguration
private val navController: NavController by …Run Code Online (Sandbox Code Playgroud) android android-fragments android-navigation android-architecture-navigation
我在这里写的是关于我们从 AppCompat 库迁移到 AndroidX 库时引入的一个问题。在此过程中,当以下问题开始时,我们从 切换android.support.design.widget.NavigationView到 and that\xe2\x80\x99s。com.google.android.material.navigation.NavigationView
在我们的NavigationView设计中,为了节省空间,我们实现了一个可展开的菜单,这样当用户点击\xe2\x80\x9cmore\xe2\x80\x9d按钮时,菜单就会展开以显示更多选项。一开始只有一些选项可见,其余选项不可见,如下所示;
\n\n单击 \xe2\x80\x9cMore...\xe2\x80\x9d 按钮后,菜单将扩展为;
\n\n为此,我们使用以下代码;
\n\n@Override\npublic boolean onNavigationItemSelected(MenuItem item)\n{\n ....\n if (item.getItemId() == R.id.nav_more)\n {\n item.setVisible(false); // hide the \xe2\x80\x9cMore\xe2\x80\x9d item\n getMenu().findItem(R.id.nav_option_3).setVisible(true);\n getMenu().findItem(R.id.nav_option_4).setVisible(true);\n getMenu().findItem(R.id.nav_option_5).setVisible(true);\n getMenu().findItem(R.id.nav_option_6).setVisible(true);\n return true;\n }\n .......\n return false;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n好吧,这段代码在过去是有效的,但是当我们迁移到使用 androidx 库时,噗,它停止工作了。嗯,确实起到了一点作用。\xe2\x80\x9c更多...\xe2\x80\x9d 按钮被隐藏,但之前隐藏的选项没有显示。\n因为,我花了很多小时才解决这个问题,并避免其他人头痛,我会解释一下问题和解决方案。
\n\n在这种情况下要做的第一件事就是查看源代码。由于代码是开源的,我可以在 github …
android navigation-drawer android-navigation android-navigationview
我有一个带有 Android Studio 中默认布局的工具栏的活动,我可以通过以下方法轻松更改工具栏中的标题:
Bundle bundle = new Bundle();
bundle.putString("toolbarTitle", "A new title");
NavHostFragment
.findNavController(this)
.navigate(R.id.action_navigation_conversation_to_placeholder, bundle);
Run Code Online (Sandbox Code Playgroud)
toolbarTitle导航菜单布局中已经实现了安全参数。
但是现在我尝试更改通过上述方法打开的片段的标题,例如;
我的活动->打开->我的片段
MyFragment 更改标题
我确实知道如何在不使用导航 UI 组件的情况下更新它,但如果可能的话我想使用它们。
其他类似的问题仅与我上面提供的示例有关,即打开新片段时或建议使用不使用导航 UI 组件的典型方法,没有一个涵盖此特定场景。
android android-titlebar android-navigation android-components
我正在尝试使用BottomNavigationViewAndroid 导航来创建一个包含三个片段的标准选项卡式应用程序:StatusFragment、MapFragment和AlertsFragment。
我的main_activity.xmlaFragmentContainerView和BottomNavigationView定义如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
tools:context=".ui.main.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/mainNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
app:defaultNavHost="true"
app:navGraph="@navigation/main_nav_graph" />
<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_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:menu="@menu/main_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
我的main_nav_graph.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/main_nav_graph"
app:startDestination="@id/mainActivity">
<fragment
android:id="@+id/statusFragment"
android:name="<redacted>.ui.status.StatusFragment"
android:label="StatusFragment" />
<fragment
android:id="@+id/mapFragment"
android:name="<redacted>.ui.map.MapFragment"
android:label="MapFragment" />
<fragment
android:id="@+id/alertsFragment"
android:name="<redacted>.ui.alert.AlertsFragment"
android:label="AlertsFragment" />
<activity
android:id="@+id/mainActivity"
android:name="<redacted>.ui.main.MainActivity"
android:label="main_activity" …Run Code Online (Sandbox Code Playgroud) android kotlin android-navigation android-architecture-navigation
我的活动中有三个顶级目标片段:
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.trackerFragment,
R.id.nutrientFragment,
R.id.settingsFragment
),
drawerLayout
)
Run Code Online (Sandbox Code Playgroud)
这意味着所有这些片段将显示汉堡按钮而不是正常的后退箭头。问题是我的应用程序中有一个“转到 Nutrient”按钮,可以导航到 NutrientFragment,这是顶级目的地。我希望这个片段在使用抽屉导航时仍然显示汉堡包图标,但在通过“转到营养物”按钮导航时显示正常的后退/向上按钮。
我最初的想法是在导航到 时传递一个布尔值作为参数NutrientFragment,并在内部onCreateView()根据布尔值,删除汉堡包图标或添加后退按钮。但这似乎有点丑陋且效率低下,特别是如果将来我添加更多可能存在相同问题的目的地。我也不知道如何“删除汉堡包图标并替换为正常的后退按钮”。关于如何实施有什么想法吗?我找到的所有答案要么是旧的已弃用的java代码,要么不完全是我正在寻找的。
谢谢。
android kotlin navigation-drawer android-navigation android-architecture-components
我在我的主要活动中使用导航抽屉定义为
private DrawerLayout drawerlayout;
Run Code Online (Sandbox Code Playgroud)
我在主要活动中使用它打开和关闭我的导航片段,但导航片段类中有3个特定按钮,我想使用导航片段功能drawerlayout.closedrawer(r.id.drawer)
但每次我在nav片段类中再次定义它并尝试添加它在这些按钮的onclick中,单击按钮后,应用程序崩溃并出现一个nullpointer异常.我如何进行相同的操作?
谢谢!以下是我的应用程序中的示例代码:public class navfragment extends fragment {
public interface OnCloseDrawerListener
{
void onCloseDrawer();
}
@Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.button_logout:
final DialogFragment dialog = new LogoutCancelSignoutDialogFragment(mLogoutListener);
dialog.show(getActivity().getSupportFragmentManager(), AbsBaseaActivity.TAG_LOGOUT_DIALOG);
BangoHelper.eventLogout();
((OnCloseDrawerListener)getActivity()).onCloseDrawer();
break;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的班级MainActivity中:
public class MainActivity extends AbsBaseaActivity implements OnBackStackChangedListener {
private DrawerLayout drawerLayout;
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//----------Code for Navigation Drawer setup
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 2.1 …Run Code Online (Sandbox Code Playgroud) 嗨我正在使用以下代码显示用户在当前位置和他的汽车位置之间的方向.
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f(%s)&daddr=%f,%f (%s)", curLat, curLong, "Your location", carLat, carLong, "Your vehicle location");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
这很好但我需要在进入导航活动之前按下开始导航.我想要的是我需要直接进入导航活动,而无需按下开始导航按钮.
你可以在下面看到.左边的图像是我先得到的图像.点击底部的开始导航按钮后,我将轮流转向导航(右图).是否有可能直接转向默认Android应用程序的导航转向?

我对我创建的布局有问题.我使用导航抽屉作为主要导航模式.它看起来和我想要的工作,但问题是,在返回到保持的片段后ViewPager- 内部片段未显示.但是,它们会在首次打开应用程序时显示,并默认显示 - ViewPager保留Fragment.
其他导航抽屉Fragments显示正常,所以我不希望我的导航抽屉实现有任何问题
RecordFragment.java(片段持有ViewPager Fragments):
public class RecordFragment extends Fragment {
private ViewPager mViewPager;
public RecordFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.record_fragment, container, false);
//getActivity().setTitle(R.string.record);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = (ViewPager) view.findViewById(R.id.record_pager);
FragmentManager manager = getFragmentManager();
mViewPager.setAdapter(new MyFragmentPagerAdapter(manager));
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm); …Run Code Online (Sandbox Code Playgroud) android android-fragments android-viewpager android-navigation
我想在我的MainActivity上为我的用户提供汉堡包和后箭头的属性导航.
当我只有一个片段设置汉堡包时,但如果我在MainActivity上添加了更多片段,请设置后退箭头.
我该如何实现?
这是我的实施......
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private DrawerLayout drawer;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(toolbar);
final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
//TODO: Insert back arrow button if have more than one fragment on backstack
/*getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
int stackHeight = getSupportFragmentManager().getBackStackEntryCount();
if (stackHeight > 0) {
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeButtonEnabled(true); …Run Code Online (Sandbox Code Playgroud) android navigation-drawer android-navigation android-toolbar
我正在尝试将navigation功能应用于我的项目。
我有这个错误:
This navigation graph is not referenced to any layout files(expected to find it in at least one layout file with a NavHostFragment with app:navGraph="@navigation/@navigation" attribute
Run Code Online (Sandbox Code Playgroud)
<?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"
android:label="fragment_init"
app:startDestination="@id/initFragment"
>
<fragment
android:id="@+id/initFragment"
android:label="fragment_init"
tools:layout="@layout/fragment_init">
<action
android:id="@+id/action_initFragment_to_authenticationFragment5"
app:destination="@id/authenticationFragment"
/>
<action
android:id="@+id/action_initFragment_to_settingFragment3"
app:destination="@id/settingFragment" />
</fragment>
<fragment
android:id="@+id/authenticationFragment"
android:name="com.example.AuthenticationFragment"
android:label="fragment_authentication"
tools:layout="@layout/fragment_authentication" />
<fragment
android:id="@+id/settingFragment"
android:name="com.example.view.main.fragment.SettingFragment"
android:label="SettingFragment"
tools:layout="@layout/fragment_setting" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
我在各处添加了该属性(导航和片段)。另外,导航.xml中使用的布局文件夹中的布局文件。但是没有用。
这是 activity_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="#000"
android:tint="#555" …Run Code Online (Sandbox Code Playgroud) 请不要将其标记为重复,我已经阅读了这些问题,但仍然无法正常工作 导航组件 popUpTo bug Android 导航组件 popUpTo 行为 Android 导航组件 + 登录流程 + 嵌套 BottomNavigationView
我在用
def nav_version = "2.2.1"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
Run Code Online (Sandbox Code Playgroud)
这是我的导航代码:
<fragment
android:id="@+id/splashFragment"
android:name="com.view.SplashFragment"
android:label="SplashFragment" >
<action
android:id="@+id/action_splashFragment_to_loginFragment"
app:destination="@id/loginFragment"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popUpTo="@id/loginFragment"
app:popUpToInclusive="true"/>
<action
android:id="@+id/action_splashFragment_to_mainFragment"
app:destination="@id/mainFragment"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popUpTo="@id/mainFragment"
app:popUpToInclusive="true"/>
</fragment>
Run Code Online (Sandbox Code Playgroud)
当我在开机状态下按后退按钮MainFragment或LoginFragment仍然能够导航回splashFragment 时。我已经有app:popUpTo和app:popUpToInclusive标签了。我希望我的应用程序不要导航回splashFragment
android ×13
android-architecture-navigation ×4
kotlin ×3
android-architecture-components ×2
geolocation ×1
gps ×1