0

我试图在LazyRow可组合中加载多个图像,画家状态总是给我一个空状态,我不知道缺少什么。

可组合代码

fun NetworkImage(
    url: String,
    contentDescription: String = "",
    modifier: Modifier,
    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.Fit,
    alpha: Float = DefaultAlpha,
    colorFilter: ColorFilter? = null,
    placeholderDrawableRes: Int? = null,
    crossFade: Int? = null,
    transformations: List<Transformation>? = null,
    onLoading: @Composable () -> Unit,
    onError: @Composable () -> Unit
) {
    Box(
       modifier = modifier
    ){
        val painter = rememberImagePainter(
            data = url,
            builder = {
                placeholderDrawableRes?.let {
                    placeholder( drawableResId = it )
                }
                error(
                    R.drawable.ic_warning
                )
                crossFade?.let {
                    crossfade(durationMillis = it)
                }
                transformations?.let {
                    transformations(transformations = it)
                }
            }
        )
        val imageState = painter.state

        if(imageState is ImagePainter.State.Loading){
            onLoading()
        }
        if(imageState is ImagePainter.State.Error){
            onError()
        }
        Image(
            painter = painter,
            contentDescription = contentDescription,
            contentScale = contentScale,
            alignment = alignment,
            alpha = alpha,
            colorFilter = colorFilter
        )
    }
 }

LazyRow里面

LazyRow(modifier = modifier) {
    items(items = urls){ url ->
        NetworkImage(
            url = url,
            contentScale = ContentScale.Crop,
            modifier = Modifier.fillMaxSize(),
            onLoading = {
                ConstraintLayout(
                    modifier = Modifier.fillMaxSize()
                ) {
                    val indicator = createRef()
                    CircularProgressIndicator(
                        modifier = Modifier.constrainAs(indicator) {
                            top.linkTo(parent.top)
                            bottom.linkTo(parent.bottom)
                            start.linkTo(parent.start)
                            end.linkTo(parent.end)
                        }
                    )
                }
            },
            onError = {
                Icon(
                    painter = painterResource(id = R.drawable.ic_warning),
                    contentDescription = "",
                    tint = Color.Red
                )
            }
        )
    }
}

当我只使用NetworkImage Composable 一次它可以正常工作时,但是当我在循环LazyRow中使用它时,它会呈现一个空框。任何提示。

4

1 回答 1

0

您最好使用伴奏寻呼机https://google.github.io/accompanist/pager/)将寻呼机库添加到应用级模块

implementation "com.google.accompanist:accompanist-pager:0.18.0"

然后将此代码用作示例

Box(contentAlignment = Alignment.Center, modifier = Modifier.fillMaxSize()) {
    val pagerState = rememberPagerState(pageCount = urls.size)
    HorizontalPager(state = pagerState) { index ->
        NetworkImage(
            url = urls[index],
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .fillMaxWidth()
                .height(300.dp),
            onLoading = {
                ConstraintLayout(
                    modifier = Modifier.fillMaxSize()
                ) {
                    val indicator = createRef()
                    CircularProgressIndicator(
                        modifier = Modifier.constrainAs(indicator) {
                            top.linkTo(parent.top)
                            bottom.linkTo(parent.bottom)
                            start.linkTo(parent.start)
                            end.linkTo(parent.end)
                        }
                    )
                }
            },
            onError = {
                Icon(
                    painter = painterResource(id = R.drawable.ic_warning),
                    contentDescription = "",
                    tint = Color.Red
                )
            }
        )
    }
}

按原样使用可组合的NetworkImage 。

于 2021-09-07T12:20:48.410 回答