I have a ListView
with two child elements:
-----------------
| left | right |
-----------------
Now I have a method, that swaps these two childs:
-----------------
| right | left |
-----------------
So the right content is on the left and the left on the right. The function looks like this:
fun swapViews(A: View, B: View) {
val viewAx = A.x
val viewAy = A.y
val viewBx = B.x
val viewBy = B.y
A.x = viewBx
A.y = viewBy
B.x = viewAx
B.y = viewAy
}
The function itself works fine, but I'm having a hard time, to swap them, when the user closes the app or just goes to app overview screen.
Currently I'm doing a check on a boolean, that is stored in the sharedprefs: Something like (pseudecode):
override fun onCreate() {
Handler().postDelayed({
if (swapped) {
swapViews(viewA, viewB)
}
, 100)
}
(BTW I needed to add the delay, because otherwise nothing would happen at all. Does anybody know why?)
While this works, when the user closes the entire app and even removes it from recent view, it does not work, when the user switches to recent app screen and switches back to the app.
It works with two LinearViews, but not with the views inside the ListView
.
I'm guessing, that android reloads the listdata onResume
and therefore it overrides my swapping. But I'm not sure tough.
Now my question: Has anybody any idea how I can solve that?
For me easiest would be, to have a function like:
listview.onReload(dostuff)
so I can run my function, when android loads the data for the ListView
.
(For testing I also did a text override in one of the ListView
s and when switching back to the app, it was gone, so android indeed reloads the ListView
)