Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
Jetpack Compose LazyColumn desplácese programáticamente hasta el elemento

¿Hay alguna forma de desplazar programáticamente LazyColumn a algún elemento de la lista? Pensé que se puede hacer elevando el estado del argumento LazyColumn state: LazyListState = rememberLazyListState() pero no tengo idea de cómo puedo cambiar este estado, por ejemplo, al hacer clic en el botón.

over 4 years ago · Hanz Gallego
5 answers
Answer question

0

En Compose1.0.0-alpha07 , no hay una API pública, pero hay una API interna para LazyListState#snapToItemIndex .

 /** * Instantly brings the item at [index] to the top of the viewport, offset by [scrollOffset] * pixels. * * Cancels the currently running scroll, if any, and suspends until the cancellation is * complete. * * @param index the data index to snap to * @param scrollOffset the number of pixels past the start of the item to snap to */ @OptIn(ExperimentalFoundationApi::class) suspend fun snapToItemIndex( @IntRange(from = 0) index: Int, @IntRange(from = 0) scrollOffset: Int = 0 ) = scrollableController.scroll { scrollPosition.update( index = DataIndex(index), scrollOffset = scrollOffset, // `true` will be replaced with the real value during the forceRemeasure() execution canScrollForward = true ) remeasurement.forceRemeasure() }

Tal vez en el próximo lanzamiento, podamos ver las actualizaciones.

over 4 years ago · Hanz Gallego Report

0

he resuelto de 2 maneras

de una manera: elegí esta mejor manera

 LaunchedEffect(true) { repeat(Int.MAX_VALUE) { delay(TIME_DELAY_BANNER) pagerState.animateScrollToPage(page = it % pagerState.pageCount) } }

de dos vías:

 var index = pagerState.currentPage LaunchedEffect(true) { while (true) { delay(TIME_DELAY_BANNER) if (index == pagerState.pageCount) { index = 0 } pagerState.animateScrollToPage(page = index++) } }
over 4 years ago · Hanz Gallego Report

0

Con 1.0.x LazyListState admite la posición de desplazamiento a través de

  • la función scrollToItem() , que 'inmediatamente' ajusta la posición de desplazamiento,
  • animateScrollToItem() que se desplaza usando una animación

Algo como:

 val listState = rememberLazyListState() // Remember a CoroutineScope to be able to launch val coroutineScope = rememberCoroutineScope() LazyColumn(state = listState) { // ... } Button ( onClick = { coroutineScope.launch { // Animate scroll to the 10th item listState.animateScrollToItem(index = 10) } } ){ Text("Click") }
over 4 years ago · Hanz Gallego Report

0

Aquí está mi código que hace que los encabezados fijos, la lista y el desplazamiento

 @ExperimentalFoundationApi @Composable private fun ListView(data: YourClass) { //this is to remember state, internal API also use the same val state = rememberLazyListState() LazyColumn(Modifier.fillMaxSize(), state) { itemsIndexed(items = data.list, itemContent = { index, dataItem -> ItemView(dataItem)// your row }) // header after some data, according to your condition stickyHeader { ItemDecoration()// compose fun for sticky header } // More items after header itemsIndexed(items = data.list2, itemContent = { index, dataItem -> ItemView(dataItem)// your row }) } // scroll to top // I am scrolling to top every time data changes, use accordingly CoroutineScope(Dispatchers.Main).launch { state.snapToItemIndex(0, 0) } } }
over 4 years ago · Hanz Gallego Report

0

prueba con

 lazyListState.animateScrollToItem(lazyListState.firstVisibleItemIndex) @Composable fun CircularScrollList( value: Long, onValueChange: () -> Unit = {} ) { val lazyListState = rememberLazyListState() val scope = rememberCoroutineScope() val items = CircularAdapter(listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) scope.launch { lazyListState.scrollToItem(items.midIndex()) } LazyColumn( modifier = Modifier .requiredHeight(height = 120.dp) .border(width = 1.dp, color = Color.Black), state = lazyListState, ) { items(items) { if (!lazyListState.isScrollInProgress) { scope.launch { lazyListState.animateScrollToItem(lazyListState.firstVisibleItemIndex) } } Text( text = "$it", modifier = Modifier.requiredHeight(40.dp), style = TextStyle(fontSize = 30.sp) ) } } } class CircularAdapter( private val content: List<Int> ) : List<Int> { fun midIndex(): Int = Int.MAX_VALUE / 2 + 6 override val size: Int = Int.MAX_VALUE override fun contains(element: Int): Boolean = content.contains(element = element) override fun containsAll(elements: Collection<Int>): Boolean = content.containsAll(elements) override fun get(index: Int): Int = content[index % content.size] override fun indexOf(element: Int): Int = content.indexOf(element) override fun isEmpty(): Boolean = content.isEmpty() override fun iterator(): Iterator<Int> = content.iterator() override fun lastIndexOf(element: Int): Int = content.lastIndexOf(element) override fun listIterator(): ListIterator<Int> = content.listIterator() override fun listIterator(index: Int): ListIterator<Int> = content.listIterator(index) override fun subList(fromIndex: Int, toIndex: Int): List<Int> = content.subList(fromIndex, toIndex) }
over 4 years ago · Hanz Gallego Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!