我目前正在使用新的(对我而言)导航组件编写应用程序。我已经通过一个导航图来了解我的应用程序的基础知识,我有一个带有 BottomNavigationView 的片段,其中有 3 个单独的片段,我已经设法更新它以使用导航组件(就我的问题)使用带有与导航项匹配的 ID 的菜单。我以前使用 newInstance 方法将包传递给 onCreate 的片段显然现在没有使用,但我仍然需要将包传递给我的片段。
我无法找到任何完成此操作的示例,因为这些片段是隐式创建的。
我的代码结构为 ClientFragment,它是导航抽屉等的宿主片段;
class ClientFragment : Fragment() {
private val viewModel: ClientViewModel by viewModel()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_client, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.client = arguments?.getParcelable(ARG_CLIENT)!!
toolbar_client.title = viewModel.client.name
toolbar_client.setNavigationOnClickListener { Navigation.findNavController(view).navigateUp() }
}
}
Run Code Online (Sandbox Code Playgroud)
这个类以前持有 onclick 侦听器到我的片段,使用工具 viewModel.client 的 newInstance 方法。
我在 nav_graph 中的片段都很相似。第一个片段;
class ClientDetailsFragment : Fragment() {
private val viewModel: ClientViewModel …Run Code Online (Sandbox Code Playgroud) 我有一个用于工具栏的布局;
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/expanded_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="120dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/expanded_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
我在布局中将其用作:
<com.dan.finance.ui.ExpandedToolbar
android:id="@+id/expandable_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:toolbarNavColor="?attr/NavigationIconColor"
app:toolbarNavIcon="?attr/NavigationUpArrow"
app:toolbarTitle="My Finances"
app:toolbarTitleColor="?attr/NavigationTitleColor"/>
Run Code Online (Sandbox Code Playgroud)
And below this in most of my layouts I have a nested scrollview, my problem is on layouts where the content shouldn't scroll by default, opening the soft keyboard allows the content to scroll using adjustResize, but my toolbar doesn't react to this and …
我有一个基本的工作经理
class BackgroundSyncWorker (
appContext: Context,
workerParams: WorkerParameters
): Worker(appContext, workerParams) {
override fun doWork(): Result {
return Result.success()
}
}
Run Code Online (Sandbox Code Playgroud)
我想将我的存储库注入其中以在我的数据库中做一些工作。我已经正确设置了 Koin,但似乎找不到如何将我的依赖项注入到 Worker 中的方法。我试过继承 KoinComponent 并尝试使用它来做它,但by inject()不存在,但有两种by inject方法我找不到如何使用。似乎没有任何关于如何注入管理器的信息,尽管有一些使用 dagger。