Is there a way to disable right horizontal scrolling on jetpack-compose accompanist horizontal pager?

2.5k views Asked by At

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?

1

There are 1 answers

0
Phil Dukhov On BEST ANSWER

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")
        }
    }
}