我有一个显示搜索项目列表的搜索片段。如果用户输入某些内容,我会将该字符串作为新的查询参数传递给 url,并使用 paging 3 库获取新列表。
第一个解决方案是:
//viewModel
lateinit var postListUrl: String
val postList: Flow<PagingData<Post>> = Pager(PagingConfig(pageSize = 20)) {
PostPagingSource(postRepository, postListUrl)
}.flow.cachedIn(viewModelScope)
//fragment
fun showPostList(url: String) {
postListAdapter.submitData(lifecycle, PagingData.empty())
viewModel.postListUrl = url
viewLifecycleOwner.lifecycleScope.launch {
viewModel.postList.collectLatest {
postListAdapter.submitData(it)
}
}
}
Run Code Online (Sandbox Code Playgroud)
通过此解决方案,通过更改 url ( showPostList(newUrl),列表保持不变。也许在 viewModel 中使用缓存列表。
另一种解决方案是:使用showPostList(initUrl)in onViewCreatedof 片段,然后通过更改参数使用 blow 方法:
//fragment
fun changePostList(url: String) {
viewModel.postListUrl = url
postListAdapter.refresh()
}
Run Code Online (Sandbox Code Playgroud)
这项工作有效,但如果旧列表和新列表有共同的项目,新列表将显示在最后一个共同的可见项目上。例如,如果旧列表的第 5 个位置项与新列表的第 7 个位置项相同,则在列表更改以显示新列表后,它将从第 7 个位置开始,而不是第一个项目。
我在这里找到了另一个解决方案:
//viewModel
val postListUrlFlow = MutableStateFlow("") …Run Code Online (Sandbox Code Playgroud) 我BottomAppBar在我的应用程序中使用,但它有一个额外的左填充。如何删除这个填充?我的 xml 代码是:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="@color/spark_secondary_color"
>
<LinearLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/white"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_search"
android:background="?attr/selectableItemBackground"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_send"
android:background="?attr/selectableItemBackground"
/>
</LinearLayout>
</LinearLayout>
</com.google.android.material.bottomappbar.BottomAppBar>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
结果是:
我在 android studio 运行应用程序后从 Github(链接)下载了 WireGuard,但出现此错误:
\n\nERROR: Cause: executing external native build for cmake /path/to/project/app/tools/CMakeLists.txt\nRun Code Online (Sandbox Code Playgroud)\n\n并android_gradle_generate_cmake_ninja_json_x86.stderr.txt显示这个:
CMake Error at CMakeLists.txt:11 (add_executable):\n Cannot find source file:\n\n wireguard-tools/src/wg-quick/android.c\n\n Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp\n .hxx .in .txx\n\n\n CMake Error: CMake can not determine linker language for target: libwg-quick.so\nRun Code Online (Sandbox Code Playgroud)\n\nwireguard-tools 文件夹为空。也许它的内容必须由 cmake 从 CMakeLists.txt 构建。\n我不明白 cmake。\nCMakeLists.txt 内容是:
\n\n# SPDX-License-Identifier: Apache-2.0\n#\n# Copyright \xc2\xa9 2018-2019 WireGuard LLC. …Run Code Online (Sandbox Code Playgroud) 我们可以看到偏移量的变化,appBarLayout.addOnOffsetChangedListener但是如何以编程方式更改偏移量?像 appBarLayout.setOffset(y: Float) 这样的东西
android material-design android-coordinatorlayout android-appbarlayout