2

I'm using accompanist library for swipe to refresh. And I adopt it sample code for testing, however, it didn't work. I search for adopt it, but I couldn't find. Is there anything wrong in my code?

I want to swipe when user needs to refresh

class MyViewModel : ViewModel() {
    private val _isRefreshing = MutableStateFlow(false)

    val isRefreshing: StateFlow<Boolean>
        get() = _isRefreshing.asStateFlow()

    fun refresh() {
        // This doesn't handle multiple 'refreshing' tasks, don't use this
        viewModelScope.launch {
            // A fake 2 second 'refresh'
            _isRefreshing.emit(true)
            delay(2000)
            _isRefreshing.emit(false)
        }
    }
}

@Composable
fun SwipeRefreshSample() {
    val viewModel: MyViewModel = viewModel()
    val isRefreshing by viewModel.isRefreshing.collectAsState()

    SwipeRefresh(
        state = rememberSwipeRefreshState(isRefreshing),
        onRefresh = { viewModel.refresh() },
    ) {
        LazyColumn {
            items(30) { index ->
                // TODO: list items
            }
        }
    }
}


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        setContent {
            TestTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {



                }
            }
        }
    }
}
Kyu-Ho Hwang
  • 143
  • 11

1 Answers1

2

Your list doesn't take up the full screen width and you should include the state parameter:

SwipeRefresh(
    state = rememberSwipeRefreshState(isRefreshing),
    onRefresh = { viewModel.refresh() },
) {
    LazyColumn(state = rememberLazyListState(), modifier = Modifier.fillMaxSize()) {
        items(100) { index ->
            Text(index.toString())
        }
    }
}

or with Column:

Column(modifier = Modifier
    .fillMaxSize()
    .verticalScroll(rememberScrollState())) {
    repeat(100) { index ->
        Text(index.toString())
    }
}
Johann
  • 27,536
  • 39
  • 165
  • 279
  • can I use in Column? I use column in different pages. It didn't work in column. Column( modifier = Modifier .fillMaxSize() ) – Kyu-Ho Hwang Jan 16 '22 at 12:31
  • You can use Column. I updated my code to show how to use Column. – Johann Jan 16 '22 at 14:48
  • I think in my code, nested vertical scroll problem. I use vertical scroll in top level composable function. How can I solve this problem? am I have to insert vertical scroll seperately? I'm always thanks for your answer. – Kyu-Ho Hwang Jan 16 '22 at 15:39
  • You need to post more of your code to show the problem. I already provided you two solutions that work. If neither of these work, you need to show your actual code. – Johann Jan 17 '22 at 07:59
  • Column( modifier = Modifier .fillMaxWidth(0.9f) .verticalScroll(rememberScrollState()) ){ ContentChooser() } – Kyu-Ho Hwang Jan 25 '22 at 14:44
  • and I make contentchooser swipeRefresh – Kyu-Ho Hwang Jan 25 '22 at 14:45