如何在自定义布局中使用导航组件?

时间:2019-07-10 14:29:46

标签: android android-fragments android-architecture-navigation

我正在使应用看起来像这样。

v

圆形的东西是纽扣。绿色部分是片段。

我的host_navigation.xml由A,B,C片段组成。

那些片段属于MainActivity(当然,它具有fragment视图),并且没有<action>(设计中片段之间没有行),因为我想打开每个随机碎片。例如,A-> B,B-> A C-> A,...无论您身在何处,都可以移动到特定片段。

三个圆形按钮也属于MainActivity

我找到了BottomNavigationViewNavigationView个样本。但是,它们似乎无法在此布局中使用。但是行为是非常相似的。只是布局不同。

如何实现此目的,以及如何自然地管理堆栈行为?例如,返回按钮,我想保留最后3个片段,并删除堆栈中所有先前的片段。

+

这是我的host_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/host_navigation"
            app:startDestination="@id/aFragment">
    <fragment android:id="@+id/aFragment"
              android:name="com.example.AFragment"
              android:label="fragment_a"
              tools:layout="@layout/fragment_a"/>

    <fragment android:id="@+id/bFragment"
              android:name="com.example.BFragment"
              android:label="fragment_b"
              tools:layout="@layout/fragment_b"/>

    <fragment android:id="@+id/cFragment"
              android:name="com.example.CFragment"
              android:label="fragment_c"
              tools:layout="@layout/fragment_c">
    </fragment>
</navigation>

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <RelativeLayout
            android:id="@+id/rl_tab_area"
            android:layout_width="match_parent"
            android:layout_height="105dp">

            <Button
                    android:id="@+id/btn_a"
                    android:layout_width="50dp"
                    android:layout_height="50sp"
                    android:layout_alignParentTop="true"
                    android:src="@drawable/ic_a"
                    android:margin="5dp"
                    android:background="@drawable/btn_a"
            />
            <Button
                    android:id="@+id/btn_b"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_marginTop="30dp"
                    android:layout_marginLeft="15dp"
                    android:layout_toRightOf="@id/btn_a"
                    android:background="@drawable/btn_b"/>

            <Button
                    android:id="@+id/btn_c"
                    android:layout_width="200dp"
                    android:layout_height="100dp"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/btn_c"/>
    </RelativeLayout>
    <fragment
            android:id="@+id/fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_below="@+id/rl_tab_area"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/host_navigation">
    </fragment>
</RelativeLayout>

我想我需要在MainActivity

中使用它
val navController = Navigation.findNavController(this@MainActivity, R.id.host_navigation)

如果使用NavigaitonBottomView,则可以将此值传递给它,但由于不使用它,我现在不知道。

我试图以此称呼其他片段

Navigation.findNavController(this, R.id.fragment).navigate(R.id.aFragment)

尝试

Navigation.findNavController(this, R.id.host_navigation).navigate(R.id.aFragment)

Navigation.findNavController(R.id.host_navigation).navigate(R.id.aFragment)

findNavController(R.id.host_navigation).navigate(R.id.aFragment)

以及其他方式。但是到目前为止,它们都没有起作用。

1 个答案:

答案 0 :(得分:0)

以这种方式为所有片段设置操作:

from selenium import webdriver
import time

driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.get('https://example.com')

page_height_pre_scroll = driver.execute_script('return document.body.scrollHeight;')
run_time, max_run_time = 0, 1
while True:
    iteration_start = time.time()
    # Scroll webpage, the 100 allows for a more 'aggressive' scroll
    driver.execute_script('window.scrollTo(0, 100*document.body.scrollHeight);')
    # Collect new height of webpage
    page_height_post_scroll = driver.execute_script('return document.body.scrollHeight;')

    if page_height_pre_scroll != page_height_post_scroll: # Success at scrolling
        run_time = 0
        page_height_pre_scroll = page_height_post_scroll
    elif page_height_pre_scroll == page_height_post_scroll and run_time < max_run_time: # Loading page
        run_time += time.time() - iteration_start
    elif page_height_pre_scroll == page_height_post_scroll and run_time >= max_run_time: # Scrolled to the bottom, or timed out
        break

或者也可以使用<navigation ... <fragment android:id="@+id/fragment_a" android:label="fragment_a" android:name="com.example.AFragment" tools:layout="@layout/fragment_a"> <action android:id="@+id/a_to_b" app:destination="@id/fragment_b" app:launchSingleTop="true"/> <action android:id="@+id/a_to_c" app:destination="@id/fragment_c" app:launchSingleTop="true"/> </fragment> </navigation>

然后通过navController启动它们:

app:popUpTo
相关问题