2

I have a basic exam in my app which is user should give some answer. After user answered the question than I want to allow swipe to right of horizontal pager. My pager is like down below.

  pagerState = rememberPagerState()
   HorizontalPager(
        modifier = Modifier.fillMaxSize(),
        count = questionList.size,
        state = pagerState) { 
          QuestionComponent(questionList[it], onUserAnswered = onUserAnswered) //Full Size Question
        }

at the top, user can swipe both direction. But if user didn't answered the question user shouldn't swipe page to right. How can I prevent this?

I.Yilmaz
  • 49
  • 8
  • Does this answer your question? [How to disable and enable scrolling in LazyColumn/LazyRow in Jetpack Compose?](https://stackoverflow.com/questions/66502096/how-to-disable-and-enable-scrolling-in-lazycolumn-lazyrow-in-jetpack-compose) – nglauber Apr 21 '22 at 03:36

1 Answers1

2

You can pass only the available questions count to the pager, and increase this value when next question is answered.

val questions = List(10) { it.toString() }
var lastAvailableQuestion by remember { mutableStateOf(1) }

HorizontalPager(
    lastAvailableQuestion
) { page ->
    Text(questions[page])
    Button({
        // check if correct
        lastAvailableQuestion += 1
    }) {
        Text("Check my answer")
    }
    if (page + 1 < lastAvailableQuestion) {
        Button({
        }) {
            Text("Go to next question")
        }
    }
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220