【问题标题】:Navigating from click listener in a row to another fragment从连续点击监听器导航到另一个片段
【发布时间】:2020-05-18 09:26:59
【问题描述】:

我需要点击Fragment A中的按钮导航到Fragment C

我正在使用 Navigation ComponentData bindingAndroid ViewModel

行.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>

片段A

    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);
}

导航.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_anav_fragment_c 的导航,但它总是给我一个例外

java.lang.IllegalArgumentException:导航目的地 com.appname:id/action_nav 对此 NavController 是未知的

【问题讨论】:

    标签: android navigation android-architecture-navigation


    【解决方案1】:

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

    <?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 的导航中阅读有关全局操作的更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 2013-12-19
      • 2013-05-12
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多