1

我使用伴奏库中的 SwipeRefresh 组合,我检查了示例,但找不到符合我需要的示例。我想实现隐藏在主 UI 上方的元素,因此当用户开始滑动框时会慢慢显示出来。我通过设置隐藏框下方的框的填充来实现此逻辑。显而易见的原因是,通过更改填充,所有可组合项都被重新创建,这会导致从本报告中看到的滞后。有没有办法解决它?

在此处输入图像描述

@Composable
fun HomeScreen(viewModel: CityWeatherViewModel) {

    val scrollState = rememberScrollState()
    val maxTopOffset = 200f
     
    SwipeRefresh(
        state = rememberSwipeRefreshState(isRefreshing = isRefreshing),
        onRefresh = {
             
        },
        indicator = { state, triggerDp ->

            if (state.isRefreshing) {

            } else {
                val triggerPx = with(LocalDensity.current) { triggerDp.toPx() }
                val progress = (state.indicatorOffset / triggerPx).coerceIn(0f, 1f)
                viewModel.apply {
                    rotateSwipeRefreshArrow(progress >= 0.9)
                    setSwipeRefreshTopPadding(progress * maxTopOffset)
                }
            }
        }
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(state = scrollState, enabled = true)
                .padding(top = viewModel.swipeRefreshPaddingTop.value.dp)
        ) {
            HiddenSwipeRefreshBox(viewModel)
            MainBox(viewModel)
        }
    }
}

@Composable
fun HiddenSwipeRefreshBox(viewModel: CityWeatherViewModel) {
}

@Composable
fun MainBox(viewModel: CityWeatherViewModel) {
}
 
@HiltViewModel
class CityWeatherViewModel @Inject constructor(
    private val getCityWeather: GetCityWeather
) : ViewModel() {
 
    private val _swipeRefreshPaddingTop = mutableStateOf(0f)
    val swipeRefreshPaddingTop: State<Float> = _swipeRefreshPaddingTop
 
    fun setSwipeRefreshTopPadding(padding: Float) {
        _swipeRefreshPaddingTop.value = padding
    }
}
4

1 回答 1

0

我设法通过用offset替换填充来修复它,因此 Column 的代码更改为:

Column(
    modifier = Modifier
        .fillMaxSize()
        .verticalScroll(state = scrollState, enabled = true)
        .offset { IntOffset(0, viewModel.swipeRefreshPaddingTop.value.roundToInt()) }
) {
}

现在,即使在具有 2GB RAM 的旧设备上也不再有滞后!我不知道这与填充滞后有何关系,但它确实有效。我从这里找到了代码

于 2022-02-01T05:56:17.243 回答