ПОСТАНОВКА ЗАДАЧИ
С серверной части можно увидеть большой текст, который по сути представляет собой историю. Показ этого текста внутри Text. И я не помещаю этот текст ни в какой LazyColumn, так как мне нужно поведение HorizontalPager. Таким образом, пользователь может провести пальцем справа налево, чтобы увидеть оставшийся контент.
Чтобы решить эту проблему, я написал такое решение
Первоначально значение количества страниц по умолчанию становится равным 1.
var pageCount by remember { mutableIntStateOf(1) }
val pagerState = rememberPagerState(pageCount = { pageCount })
Создание такого HorizontalPager
val linesPerPage = remember { mutableStateListOf<Int>() }
val remainingText = remember { mutableStateOf("") }
HorizontalPager(state = pagerState) {
Column(
modifier = modifier
.fillMaxSize()
.padding(bottom = innerPadding.calculateBottomPadding())
) {
showLog("PAGE COUNT is $pageCount")
if (pagerState.currentPage == 0) {
Spacer(modifier = modifier.padding(top = innerPadding.calculateTopPadding()))
TitleView(title = data.title, modifier = modifier)
AuthorNameTextView(name = data.author.name, modifier = modifier)
}
StoryContentView(
story = dummyStory,
textSize = textSize,
textStyle = selectedTextStyle,
onTextLayout = { result ->
val lastVisibleLineIndex =
result.getLineForVerticalPosition(result.layoutInput.constraints.maxHeight.toFloat())
val lastVisibleLineStart = result.getLineStart(lastVisibleLineIndex)
val lastVisibleLineEnd = result.getLineEnd(lastVisibleLineIndex)
val lastVisibleLineText =
dummyStory.substring(lastVisibleLineStart, lastVisibleLineEnd)
showLog("REMAINING TEXT: $lastVisibleLineText")
if (result.didOverflowHeight) {
// Check if this is a new page or the current page is overflowing
if (linesPerPage.isEmpty() || linesPerPage.last() != lastVisibleLineIndex) {
// New page or current page is overflowing
if (!linesPerPage.contains(lastVisibleLineIndex)) {
pageCount++
linesPerPage.add(lastVisibleLineIndex)
remainingText.value = lastVisibleLineText
}
}
}
}
)
}
}
В этом StoryContentView нет ничего, кроме простого текста, составленного Jetpack.
Здесь требование — когда result.didOverflowHeight вернется true, в этом случае я добавлю еще одну страницу. Теперь, когда я перехожу на страницу 2, я хочу показать там оставшийся текст, и если контент снова переполняется на странице 2, он добавляет pageCount, а затем переходит на страницу 3 и так далее, если я не достиг завершения содержимого.
В этом коде я могу получить ОСТАТОЧНЫЙ ВЕСЬ ТЕКСТ, но как показать его на другой странице, я там затрудняюсь.
Для этого я взял в качестве примера dummyStory, который val dummyStory = """ На пути к созданию такой яркой мечты, Я гоняюсь за звездами в бесконечной ночи. С каждым моим шагом, с каждым шагом, Я покорю мир, не позволю мечтам скрыться.
The path may be winding, the journey long,
But I'll keep marching, staying strong. Through trials and tests, I'll find my way, To the land of dreams where I'll someday stay.
With bricks of ambition, and mortar of hope,
I'll build my dream, I'll never elope.
I'll shape it with passion, and carve it with care,
A vision so grand, beyond compare.
On my way to building the dream in my heart,
I'll face every challenge, I'll do my part. With unwavering faith and a determined soul,
I'll reach my destination, I'll achieve my goal.
So, watch me as I rise, like a radiant sunbeam,
On my way to building the dream.
As I walk this path, lined with stars aglow,
I feel the universe whisper, a comforting flow.
Each star a story, each twinkle a sign,
Guiding me forward, igniting a shine.
Through valleys of doubt and mountains of fear,
I'll press on, knowing my purpose is near.
With courage as my compass, and hope as my guide,
I'll navigate challenges, with grace as my stride.
Every setback a lesson, every obstacle a test,
I'll embrace them all, for they help me manifest.
The dream that's within, the vision so clear,
I'll nurture it daily, banishing every fear.
With patience as my ally, and persistence my friend,
I'll endure the hardships, until the very end.
For dreams are not fleeting, nor illusions in air,
They're the seeds of our future, the essence we bear.
So, as I continue on this journey of mine,
I'll cherish each moment, each mountain I climb.
For the dream that I seek, the dream that I chase,
Is not just a destination, but a journey of grace.
On my way to building the dream so bright,
I'll paint the sky with my colors, in the darkest of night.
For dreams are the fuel, the fire in our soul,
Guiding us forward, making us whole.
""".trimIndent()
Вы можете сохранить список String в качестве своего состояния, добавляя оставшийся текст на каждом этапе, а затем просматривая список с указателем текущей страницы, чтобы узнать содержимое текущей страницы. Что-то вроде следующего:
var storyPages by remember { mutableStateOf(listOf(story)) }
val pageCount by remember { derivedStateOf { storyPages.count() } }
val pagerState = rememberPagerState(pageCount = { pageCount })
HorizontalPager(state = pagerState) {
Column(
modifier = modifier
.fillMaxSize()
.padding(bottom = innerPadding.calculateBottomPadding())
) {
val currentPageIndex = it
if (currentPageIndex == 0) {
Spacer(modifier = modifier.padding(top = innerPadding.calculateTopPadding()))
TitleView(title = data.title, modifier = modifier)
AuthorNameTextView(name = data.author.name, modifier = modifier)
}
val pageContent = storyPages[currentPageIndex]
StoryContentView(
story = pageContent,
textSize = textSize,
textStyle = selectedTextStyle,
onTextLayout = { result ->
val lastVisibleLineIndex =
result.getLineForVerticalPosition(result.layoutInput.constraints.maxHeight.toFloat())
val lastVisibleLineStart = result.getLineStart(lastVisibleLineIndex)
val lastVisibleLineText = pageContent.substring(lastVisibleLineStart, pageContent.lastIndex)
val hasNextPage = storyPages.count() > currentPageIndex + 1
if (result.didOverflowHeight && !hasNextPage) {
// Add remaining text to be the new story page
val newList = storyPages.toMutableList()
newList.add(lastVisibleLineText)
storyPages = newList
}
}
)
}
}
Краткое описание того, что я изменил по сравнению с вашим:
pagerState.currentPage просто используйте it, доступный в области действия HorizontalPager. (т. е. val currentPageIndex = it)result.didOverflowHeight и следующей страницы нет (см. hasNextPage).Конечно, так и будет, я намеренно оставил эти части за рамками. Тогда позвольте мне отредактировать мой ответ с полным кодом :)
При этом логика LastVisibleLineText теперь ломается. Первая страница - отображается правильно, вторая страница - также отображается правильно, но на третьей странице отображаются неправильные строки.