android - 从一行中的单击监听器导航到另一个 fragment

标签 android navigation android-architecture-navigation

我需要单击 fragment A 中的按钮才能导航到 fragment C

正在使用Navigation Component , Data bindingAndroid ViewModel

row.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:padding="@dimen/padding">
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/img_edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_small"
                android:layout_marginRight="@dimen/margin_small"
                app:srcCompat="@drawable/edit"
                app:tint="@color/grey"
                android:background="@drawable/circle"
                android:onClick="@{(v) -> handler.onEdit(v,var.accId)}"
                 />
    </RelativeLayout>
<data>
    <variable
        name="var"            type="Result" />
    <variable
        name="handler"            type="FragmentACallback" />
</data>
</layout>

FragmentA

    private void setListAdapter() {
    final FragmentACallback callback = new FragmentACallback() {
        @Override
        public void onEdit(View v, int accID) {
  /*
  i tried all these solutions but not working
   */

   // Navigation.createNavigateOnClickListener(R.id.action_nav).onClick(v);

    //                Navigation.findNavController(v.itemView).navigate(R.id.action_nav);
    //                NavHostFragment.findNavController(v).navigate(R.id.action_nav);
    Navigation.findNavController(getActivity(),R.id.nav_host_fragment).navigate(R.id.action_nav);
    //                Navigation.findNavController((MainActivity)(v.getContext()), R.id.nav_host_fragment).navigate(R.id.action_nav);
        }
    };

    adapter = new PageListAdapter(getActivity(), data, callback);
    binding.rcvList.setAdapter(adapter);
}

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/nav_home"> 

<fragment
    android:id="@+id/nav_home"
    android:name=".HomeFragment"
    tools:layout="@layout/fragment_home" />

 <fragment
    android:id="@+id/nav_viewpager1"
    android:name=".ViewPager1"
    tools:layout="@layout/fragment_pager1">
    <action
        android:id="@+id/action_nav"
        app:destination="@id/fragment_pager2" />
</fragment>

 <fragment
    android:id="@+id/nav_viewpager2"
    android:name=".ViewPager2"
    tools:layout="@layout/fragment_pager2" />

<fragment
    android:id="@+id/nav_fragment_a"
    android:name=".FragmentA"
    tools:layout="@layout/fragment_a" />

<fragment
    android:id="@+id/nav_fragment_c"
    android:name=".FragmentC"
    tools:layout="@layout/fragment_C" />

</navigation>

我还尝试从nav_fragment_a导航到nav_fragment_c,但它总是给我一个异常(exception)

java.lang.IllegalArgumentException:导航 objective-c om.appname:id/action_nav 对于此 NavController 来说是未知的

最佳答案

要解决此问题,请将操作作为全局操作,如下所示:

<?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/nav_home"> 

<fragment
android:id="@+id/nav_home"
android:name=".HomeFragment"
tools:layout="@layout/fragment_home" />

 <fragment
android:id="@+id/nav_viewpager1"
android:name=".ViewPager1"
tools:layout="@layout/fragment_pager1"/>

 <fragment
android:id="@+id/nav_viewpager2"
android:name=".ViewPager2"
tools:layout="@layout/fragment_pager2" />

<fragment
android:id="@+id/nav_fragment_a"
android:name=".FragmentA"
tools:layout="@layout/fragment_a" />

<fragment
android:id="@+id/nav_fragment_c"
android:name=".FragmentC"
tools:layout="@layout/fragment_C" />

<action
    android:id="@+id/action_nav"
    app:destination="@id/fragment_pager2" />
</navigation>

您可以在导航中阅读有关全局操作的更多信息 here

关于android - 从一行中的单击监听器导航到另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60025414/

相关文章:

Dot Net 中的 Android 应用内计费收据验证(C#)

css - 根据内部列表元素的数量调整 div 宽度

swift - 如何告诉用户朝北或朝东的准确度数?

android - 非法状态异常 : Activity does not have a NavController set on

带有导航组件的 Android 深层链接无法按预期工作

android - 在Android分页库数据源内部处理Observable

android - 在 android 和 IOS phonegap 应用程序中读取 pdf 文件

android - 网络用户目录的设置gradle

jsf - 警告 JSF1090 : Navigation case not resolved for component j_idt51

android - 自定义 NavController 和自定义 NavHost