简体   繁体   English

带viewpager2的导航图android

[英]Navigation graph with viewpager2 android

My viewpager consists of 2 fragments which are displayed correctly and working fine.我的 viewpager 由 2 个片段组成,它们显示正确并且工作正常。

From viewpager's fragment I want to move to another fragment via navigation graph从viewpager的片段我想通过导航图移动到另一个片段

This is my graph这是我的图表

<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/offer_nav_graph"
app:startDestination="@id/offersFragment">

<fragment
    android:id="@+id/offersFragment"
    android:name="com.octave.offers.OffersFragment"
    android:label="offers_fragment"
    tools:layout="@layout/offers_fragment" />

<fragment
    android:id="@+id/availableOfferDetailFragment"
    android:name="com.octave.offers.available.AvailableOfferDetailFragment"
    android:label="fragment_available_offer_detail"
    tools:layout="@layout/fragment_available_offer_detail" />
<fragment
    android:id="@+id/availableOffersFragment"
    android:name="com.octave.offers.available.AvailableOffersFragment"
    android:label="fragment_available_offers"
    tools:layout="@layout/fragment_available_offers" >

    <action
        android:id="@+id/action_availableOffersFragment_to_availableOfferDetailFragment"
        app:destination="@id/availableOfferDetailFragment" >
        <argument
            android:name="offerId"
            app:argType="integer"
            android:defaultValue="-1" />
    </action>
</fragment>
</navigation>

Offers fragment - has view pager Available offer fragment - one of the fragment on view pager Available offer detail fragment - i want to navigate优惠片段 - 有查看寻呼机 可用优惠片段 - 查看寻呼机上的片段之一 可用优惠详情片段 - 我想导航

On button click I am calling this在按钮点击我打电话给这个

AvailableOffersFragmentDirections.actionAvailableOffersFragmentToAvailableOfferDetailFragment(offerId)

The exception例外

Navigation action/destination com.octave.staging:id/action_availableOffersFragment_to_availableOfferDetailFragment cannot be found from the current destination Destination(com.octave.staging:id/homeFragment) label=HomeFragment class=com.octave.home.HomeFragment

What is wrong over here?这里有什么问题?

Check using:检查使用:

btn.setOnClickListener {
       Navigation.findNavController(it).navigate(R.id.availableOfferDetailFragment)
}

button exists on availableOffersFragment.按钮存在于 availableOffersFragment 上。 I want to navigate from availableOffersFragment to availableOfferDetailFragment.我想从 availableOffersFragment 导航到 availableOfferDetailFragment。 offersFragment has the viewpager with 2 tabs having their own fragment availableOffersFragment and secondFragemnt offerFragment 的 viewpager 有 2 个选项卡,它们有自己的片段 availableOffersFragment 和 secondFragemnt

So, now you need to navigate from availableOffersFragment which is a tab (page fragment) in the ViewPager;因此,现在您需要从availableOffersFragment中的选项卡(页面片段)availableOffersFragment 导航; but this is not possible because those tabs don't affect the back stack.但这是不可能的,因为这些选项卡不会影响后台堆栈。 You can check out the questions discussed in here and also this one.您可以查看此处讨论的问题以及此问题。

But what you can do instead is to add an action from offersFragment (which holds the ViewPager) to availableOfferDetailFragment as following:但是您可以做的是将 offerFragment(包含 ViewPager)中的操作添加到 availableOfferDetailFragment,如下所示:

  • Remove the tab fragments from the navGraph ( availableOffersFragment and secondFragemnt ).从 navGraph 中删除选项卡片段( availableOffersFragmentsecondFragemnt )。
  • Create an action in the navGraph from offersFragment to availableOfferDetailFragment在 navGraph 中创建从 offerFragment 到 availableOfferDetailFragment 的操作
    <fragment
        android:id="@+id/offersFragment"
        android:name="com.octave.offers.OffersFragment"
        android:label="offers_fragment"
        tools:layout="@layout/offers_fragment">
        
        <action
            android:id="@+id/action_offersFragment_to_availableOfferDetailFragment"
            app:destination="@id/availableOfferDetailFragment" >
            <argument
                android:name="offerId"
                app:argType="integer"
                android:defaultValue="-1" />
        </action>
    </fragment>
  • When the button is clicked use parentFragment to access the offersFragment from the availableOffersFragment tab when the button is clicked.单击按钮时,使用parentFragmentavailableOffersFragment选项卡中访问offersFragment For instance:例如:

Create a method in offersFragment :offersFragment中创建一个方法:

goToDetailsFragment(offerId: Int) {
    // perfrom the navaigation
    OffersFragmentDirections.actionOffersFragmentToAvailableOfferDetailFragment(offerId)
}

And in availableOffersFragment并且在availableOffersFragment

btn.setOnClickListener {
       (parentFragment as offersFragment).goToDetailsFragment(offerId)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM