Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – LazyColumn callback for items that become visible but only once

Posted on November 11, 2022 By

Solution 1 :

Just change your code to :

 snapshotFlow { listState.layoutInfo.visibleItemsInfo}
    .map { it.first() }
    .distinctUntilChanged()
    .collect {
        MyAnalyticsService.someVisibleItemCallback()
    }

Distinct until changed will prevent your flow from being called until your value changes

Solution 2 :

When a LazyColumn item is “recycled“, the item will be re-initialized including side-effects it has.

I had a similar requirement and attempted to utilize rememberUpdatedState, sadly to no avail, it didn’t satisfy what I wanted because what ever I do, LazyColumn's item keeps being recycled, so I just ended up adding an additional attribute to my data class, something that would “persist” outside of the recycling like your isSeen boolean property.

isInitialized: Boolean

Making sure this flag wraps my callback.

@Composable
fun ListItemComposable(
    item: Item,
    doneInitCallback : (Item) -> Unit
) {
    LaunchedEffect(Unit) {
        if (!item.isInitialized) {
            doneInitCallback(item)
        }
    }
    ....
}

If there are other ways, I’m not sure, though the closest solution we can find is using either rememberUpdatedState, your attempt to use snapShotFlow or rememberSaveable, but again every item is being recycled as you scroll. I haven’t tried using rememberSaveable yet for this situation though.

Also have a look at Phil Dukhov’s answer.

Problem :

I am looking for an efficient way to trigger a callback for each item of a LazyColumn as they become visible, but only once.

  • The callback should happen only once as items become visible. It should not trigger if the user scrolls past the same item several times.
  • The callback should only happen once per each item.

Is there a way Compose-y way of handling this?

I tried to use snapshotFlow as below, but no matter which side effect I use, it gets triggered over and over as a user scrolls.

val listState = rememberLazyListState()
LaunchedEffect(listState) {
    snapshotFlow { listState.layoutInfo.visibleItemsInfo}
        .map { it.first() }
        .collect {
            MyAnalyticsService.someVisibleItemCallback()
        }
}

Another way I can image is baking this into the model state as follows.

data class SomeObject(
  val someStuff: SomeStuff,
  val isSeen: Boolean = false
)

How can I handle this in an efficient way?

READ  [FIXED] How can I make a button open a specific page in PDF file which is on assets in android studio?
Powered by Inline Related Posts

Comments

Comment posted by clamentjohn

But for a large list being scrolled the component will get mounted and unmounted, won’t it. In such a case won’t

Comment posted by Arsh

Yes it will be called again but you change your visibility value will be changed only once. Store visibility for all the items in your VM and simply change it for the first and last time when the item comes in view. The distinctUntilChanged will not execute again and again

Comment posted by Sudhir Singh Khanger

If I store visibility as a Boolean in the data class then what role does snapshotFlow plays as you mentioned in the answer. If I have it saved in the model then I would just check for it and if it is false then fire the LaunchedEffect.

Comment posted by Sudhir Singh Khanger

I am trying to think if

Comment posted by Sudhir Singh Khanger

Why is item.isInitialized check inside the side effect? Should it not be outside? Or maybe I am wrong at least when you are in the view you want to avoid the check on recomposition. Any other problems have you experienced with this approach? Any slowdowns?

Comment posted by z.y

I consider this kind of scenario as a “Side-Effect” that’s why I wrap it inside

Comment posted by z.y

Also thank you if you voted it up, I was waiting for some correction or recommendation from the one who voted it down as I find this something hard to work around with. If I may ask, is the size of your list fixed? or undetermined?, because I have another situation where I just ended up using a

READ  [FIXED] android - How to draw a rectangle around multiple views
Powered by Inline Related Posts

Comment posted by Sudhir Singh Khanger

1. I am not seeing any benefit of snapshotFlow because the LaunchedEffect will execute anyway. So will snapshotFlow.

Comment posted by Sudhir Singh Khanger

Size depends on API response but except more or less 100 items. But downside of Column is that 100 columns will be created and there is recycling of them.

Android Tags:android, android-jetpack-compose, android-jetpack-compose-list, lazycolumn

Post navigation

Previous Post: [FIXED] java – App crash after uploaded to play store , working correctly when installed from android studio
Next Post: What is the 21 day fitness challenge?

Related Posts

[FIXED] android – findViewById(R.id.editTextDialog) must not be null with custom dialog and ROOM database Android
[FIXED] java – How to get specific data from Firebase Firestore and Create a Line Graph in Android? Android
[FIXED] android – Get the name of image used in ImageButton Android
[FIXED] android – Moving an ImageView in the screen Android
[FIXED] android – Sign Out activity Android
[FIXED] android – How to get bitmap from URL using Coil? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme