kotlin 导航组件添加透明片段

e0bqpujr  于 2022-12-27  发布在  Kotlin
关注(0)|答案(1)|浏览(136)

我正在将应用导航迁移到导航组件。一切都很好,非常直观,但我有这个问题。在我的主活动中,我首先添加homeFragment,它工作得很好。但当我尝试在homeFragment上添加一些新片段时,我的第二个片段是透明的。我的元素是可见的。但是背景不是。2所以我看到HomeScreen片段,在它上面是我的第二个片段的元素。3这是我的代码:

<fragment
            android:id="@+id/mainContainer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:elevation="5dp"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/toolbar" />

<!--        <FrameLayout-->
<!--            android:id="@+id/mainContainer"-->
<!--            android:layout_width="0dp"-->
<!--            android:layout_height="0dp"-->
<!--            app:layout_constraintBottom_toBottomOf="parent"-->
<!--            app:layout_constraintLeft_toLeftOf="parent"-->
<!--            app:layout_constraintRight_toRightOf="parent"-->
<!--            app:layout_constraintTop_toBottomOf="@id/toolbar" />-->

(之前使用frameLayout,一切都很好,现在使用fragment就不行了)

class NavigationMainComponent : CoroutineScope{

    var job = Job()

    override val coroutineContext: CoroutineContext
        get() = job + Dispatchers.IO

    private val _navigation = MutableSharedFlow<NavigationState>()
    val navigationState: Flow<NavigationState> = _navigation

    fun navigate(navigate: NavigationState){
        launch(Dispatchers.IO) {
            when(navigate) {
                is NavigationState.Deeplink -> resolveDeeplink(navigate.url)
                else -> _navigation.emit(navigate)
            }
        }
    }

    private fun resolveDeeplink(url: String) {
        val uri = Uri.parse(url)

        // missing navigation to fragment
    }
}

open class NavigationState {
    object PopBackStack : NavigationState()
    data class Navigate(val resId: Int, val bundle: Bundle?) : NavigationState()
    data class Deeplink(val url: String) : NavigationState()
    data class GameDetail(val gameId: String) : NavigationState()



 private fun setupNavigation() {
        navController = findNavController(R.id.mainContainer)

        val topLevelDestinations = hashSetOf(
            R.id.home_fragment,
            R.id.database_saved_countries
        )
        val appBarConfiguration = AppBarConfiguration
            .Builder(topLevelDestinations)
            .build()

        setupWithNavController(binding.toolbar.toolbarMain, navController, appBarConfiguration)
    }

我称之为打开新的片段:

NavigationMainComponent().navigate(NavigationState.Navigate(R.id.database_saved_countries, null))

我很清楚一些尴尬的事情会解决这个问题,但我到现在都没找到。提前感谢帮助:D

6gpjuf90

6gpjuf901#

这是由新版本中导航组件的行为导致的。您可以使用以下方法防止透明背景:
首先创建一个基本fragment类,如下所示:

public class BaseFragment extends Fragment {

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // we don't need to change background when method calls again!
        if (savedInstanceState != null) return;

        // we set the background color of root view to white
        // because navigation animations run on a transparent background by default
        view.setBackgroundColor(Color.WHITE);
    }

}

然后扩展这个类的所有片段:

public class HomeFragment extends BaseFragment {

}

相关问题