小编Had*_*adi的帖子

Android Paging 3 库:如何使用新参数更改列表?

我有一个显示搜索项目列表的搜索片段。如果用户输入某些内容,我会将该字符串作为新的查询参数传递给 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)

android android-paging kotlin-flow android-paging-3

10
推荐指数
1
解决办法
4191
查看次数

android BottomAppBar 有额外的左填充

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)

结果是:

在此处输入图片说明

layout android android-bottomappbar

9
推荐指数
1
解决办法
1615
查看次数

如何在“在工具窗口中启动”在“设置”中处于活动状态时更改 Android Studio 模拟器设置?

如果模拟器工具窗口被标记,我就找不到模拟器设置。我想要这个设置:

在此处输入图片说明

而“在工具窗口中启动”在设置中处于活动状态:

在此处输入图片说明

android-emulator android-studio

4
推荐指数
1
解决办法
478
查看次数

Android Studio 错误:原因:为 cmake /path/to/project/app/tools/CMakeLists.txt 执行外部本机构建

我在 android studio 运行应用程序后从 Github(链接)下载了 WireGuard,但出现此错误:

\n\n
ERROR: Cause: executing external native build for cmake  /path/to/project/app/tools/CMakeLists.txt\n
Run Code Online (Sandbox Code Playgroud)\n\n

android_gradle_generate_cmake_ninja_json_x86.stderr.txt显示这个:

\n\n
    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\n
Run Code Online (Sandbox Code Playgroud)\n\n

wireguard-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)

android cmake android-ndk android-studio ndk-build

3
推荐指数
1
解决办法
3128
查看次数

如何以编程方式更改 AppBarLayout 偏移量?

我们可以看到偏移量的变化,appBarLayout.addOnOffsetChangedListener但是如何以编程方式更改偏移量?像 appBarLayout.setOffset(y: Float) 这样的东西

android material-design android-coordinatorlayout android-appbarlayout

3
推荐指数
1
解决办法
1270
查看次数