callstack / callstack/react-native-pager-view

ViewPager layout breaks when detaching and attaching it again

Aperta
#122 4 commenti 7 reazioni 0 assegnatari Vedi su GitHub
android bug
Lingua principale
TypeScript
Stelle
3.4k
Fork
476
Merge medio
10g 21h
PR unite (30g)
2

Descrizione

## Environment info

```sh
react-native: 0.61.5
```

Library version: `3.2.0`

## Steps To Reproduce

See the issue:

![pager-detach-broken](https://user-images.githubusercontent.com/774577/70314278-72597080-1817-11ea-85d8-49f9945809e4.gif)

## Reproducible sample code

Repro: https://github.com/ferrannp/react-native-viewpager-detach-example

When navigating to another screen with `react-navigation` (e.g to another tab), `onDetachedFromWindow` is called. When we come back to the view that has the `ViewPager`, this code is called:

https://github.com/react-native-community/react-native-viewpager/blob/master/android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPager.java#L212-L219

However, it seems to be called too late and the Pager gets broken. I tried the following solution:

```java
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
// The viewpager reset an internal flag on this method so we need to run another layout pass
// after attaching to window.
this.requestLayout();
// CODE CHANGES BELOW
measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
layout(getLeft(), getTop(), getRight(), getBottom());
}
```

And it works. However, I am not sure this is the best solution. I know @kmagiera did something similar before with Pager so I am just CCing you here. Maybe @satya164 @NoemiRozpara you found this before when working with react-navigation and ViewPager? I'll also CC @ruiaraujo which implemented this piece of code 2 years ago 😄 .

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia da android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPager.java alle righe collegate e riproduci il flusso di detach/attach usando il react-native-viewpager-detach-example fornito. Confronta il comportamento esistente di onAttachedToWindow con la sequenza di measure e layout segnalata; il lavoro è completato quando ViewPager rimane correttamente disposto dopo aver navigato altrove e poi essere tornati indietro.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
android, java, react-native
Ambito
mobile-dev
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.