我SwipeRefresh
在撰写中使用视图(来自accompanist)。我在LazyVerticalGrid
里面,网格填充来自网络的分页数据。如果分页数据中没有内容,我将显示空状态视图。当有一些数据时,滑动刷新功能就会起作用。LazyVerticalGrid
问题是,如果没有数据,我无法进行滑动刷新,但同样有效LazyColumn
(两种情况都已NoContentView
显示)。
@Composable
fun GridItems(
searchViewModel: SearchViewModel
) {
var isRefreshing by remember { mutableStateOf(false) }
val posts = remember { searchViewModel.posts }.collectAsLazyPagingItems()
Scaffold(
topBar = { MyTopBar() }
) { innerPadding ->
SwipeRefresh(
state = rememberSwipeRefreshState(isRefreshing = isRefreshing),
onRefresh = { searchViewModel.getPosts() }
) {
Column(modifier = Modifier.padding(innerPadding)) {
LazyVerticalGrid(
cells = GridCells.Fixed(3),
modifier = modifier.padding(horizontal = 3.dp)
) {
items() {
MySinglePostItem()
}
posts.apply {
when { …
Run Code Online (Sandbox Code Playgroud) 我有一个多行文本字段,如下所示
val scrollState = rememberScrollState(0)
TextField(
modifier = Modifier
.fillMaxWidth()
.height(75.dp)
.verticalScroll(scrollState),
value = caption,
onValueChange = { onCaptionChanged(it) }
)
Run Code Online (Sandbox Code Playgroud)
目前,它支持手动滚动,但我希望它在用户输入新行(\n)时自动向下滚动。我怎样才能做到这一点?
将内部存储中的所有图像放入LazyVerticalGrid
. 到目前为止,它运行良好,没有任何问题,但最近出现了这个问题,没有任何明显的提示。
这是我的用户界面和实用程序代码
fun getAllImagesFromStorage(context: Context): List<ImageMetaData> {
val imageList = mutableListOf<ImageMetaData>()
val collection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
} else {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
}
val projection = arrayOf(
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.SIZE
)
val sortOrder = MediaStore.MediaColumns.DATE_ADDED + " DESC"
val cursor = context.contentResolver
.query(collection, projection, null, null, sortOrder)
cursor?.use { cursor ->
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)
while (cursor.moveToNext()) {
val id = cursor.getLong(idColumn)
val name = cursor.getString(nameColumn)
val contentUri: Uri = ContentUris.withAppendedId( …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为类创建 TypeConverter Color(androidx.compose.ui.graphics)
,但 room 在编译时失败并出现错误
error: Class is referenced as a converter but it does not have any converter methods.
public final class ColorConverter {
^
Run Code Online (Sandbox Code Playgroud)
颜色类别(组合中)
value class Color(val value: ULong) {
...
}
Run Code Online (Sandbox Code Playgroud)
颜色转换器的实现
object ColorConverter {
@JvmStatic
@TypeConverter
fun toLong(color: Color): Long = color.value.toLong()
@JvmStatic
@TypeConverter
fun toColor(value: Long): Color = Color(value)
}
Run Code Online (Sandbox Code Playgroud)
我也在数据库中声明了
@TypeConverters(ColorConverter::class)
Run Code Online (Sandbox Code Playgroud) 我正在使用分页撰写库使用远程中介(由本地房间数据库支持)从服务器加载分页数据。有没有办法在滑动刷新的情况下手动刷新中介数据?
android android-paging android-jetpack-compose android-paging-3
我使用下面的分页源实现从网络获取数据并通过可组合方式观察它collectAsLazyPagingItems()
。但是,当调用refresh()
this 方法时LazyPagingItems
,它不会从中获取数据page 0
,而是从上次获取的页码中获取数据,这导致没有数据可显示。这是怎么回事?难道是因为val page = params.key ?: 0
?
class CommentDataSource(
private val postId: Long,
private val commentApi: CommentApi
) : PagingSource<Int, Comment>() {
override fun getRefreshKey(state: PagingState<Int, Comment>): Int? {
return state.anchorPosition
}
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Comment> {
return try {
val page = params.key ?: 0
val responseData = commentApi.getPostComments(
postId = postId,
page = page
).data
LoadResult.Page(
data = responseData.comments,
prevKey = if (page == …
Run Code Online (Sandbox Code Playgroud) navigation-compose
我在 jetpack compose 中与底部栏一起使用。我想为不同类型的用户显示不同的底部栏。为此,我需要startDestination
在 中设置条件NavHost
。我怎么做?
我在下面尝试过,但它发生了变化startDestination
,但没有反映在用户界面中。
val user by remember { mutableStateOf(viewModel.user) }.collectAsState()
var startDestination = Screens.UserType1.route
LaunchedEffect(user) {
startDestination = if (loggedInUser?.userType == UserType.Seller) Screens.SellerHome.route else Screens.BuyerHome.route
}
Run Code Online (Sandbox Code Playgroud)
虽然下面的代码抛出 java.util.NoSuchElementException: List contains no element matching the predicate.
when (user?.userType) {
UserType.Seller -> {
startDestination = Screens.SellerHome.route
}
UserType.Buyer -> {
startDestination = Screens.BuyerHome.route
}
else -> {}
}
Run Code Online (Sandbox Code Playgroud)
我的导航主机
NavHost(
modifier = modifier,
navController = navController,
startDestination = startDestination
) {
... …
Run Code Online (Sandbox Code Playgroud) 我使用下面的代码从远程 url 检索视频帧MediaMetadataRetriever
,但它滞后并导致 UI 性能非常低。我怎样才能让它快速高效?
@Composable
private fun ContentItem(
modifier: Modifier = Modifier,
content: Content,
onClick: (Content) -> Unit
) {
when (content.type) {
ContentType.Image -> {
// handle image
}
ContentType.Video -> {
val bitmap = remember { mutableStateOf<Bitmap?>(null) }
LaunchedEffect(content) {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(content.url)
// Retrieve frame at 1 second
bitmap.value = retriever.getFrameAtTime(
1000000,
MediaMetadataRetriever.OPTION_CLOSEST_SYNC
)
retriever.release()
}
bitmap.value?.let {
Image(
modifier = modifier,
bitmap = it.asImageBitmap(),
contentDescription = null
)
}
}
} …
Run Code Online (Sandbox Code Playgroud)