Skip to content

Commit c82283a

Browse files
authored
Fixes #3506: Image height remains zero for some time on bad network (#3513)
* optimise width loading * amending as per the reviews * aspect ratio updates after layout completion * MediaDetailFragment: update aspect ratio using onGlobalLayoutListener * remove unnecessary imports
1 parent c53fcb5 commit c82283a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import android.os.Bundle;
1010
import android.text.Editable;
1111
import android.text.TextWatcher;
12-
import android.util.TypedValue;
1312
import android.view.LayoutInflater;
1413
import android.view.View;
1514
import android.view.ViewGroup;
1615
import android.view.ViewTreeObserver;
16+
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
1717
import android.widget.ArrayAdapter;
1818
import android.widget.Button;
1919
import android.widget.EditText;
@@ -33,7 +33,6 @@
3333

3434
import org.apache.commons.lang3.StringUtils;
3535
import org.wikipedia.util.DateUtil;
36-
import org.wikipedia.util.StringUtil;
3736

3837
import java.util.ArrayList;
3938
import java.util.Date;
@@ -224,7 +223,15 @@ public void onResume() {
224223
.setVisibility(View.GONE);
225224
}
226225
media = detailProvider.getMediaAtPosition(index);
227-
displayMediaDetails();
226+
scrollView.getViewTreeObserver().addOnGlobalLayoutListener(
227+
new OnGlobalLayoutListener() {
228+
@Override
229+
public void onGlobalLayout() {
230+
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
231+
displayMediaDetails();
232+
}
233+
}
234+
);
228235
}
229236

230237
private void displayMediaDetails() {
@@ -243,7 +250,8 @@ private void displayMediaDetails() {
243250

244251
private void updateAspectRatio(ImageInfo imageInfo) {
245252
if (imageInfo != null) {
246-
int finalHeight = (scrollView.getWidth()*imageInfo.getHeight()) / imageInfo.getWidth();
253+
int screenWidth = scrollView.getWidth();
254+
int finalHeight = (screenWidth*imageInfo.getHeight()) / imageInfo.getWidth();
247255
ViewGroup.LayoutParams params = image.getLayoutParams();
248256
params.height = finalHeight;
249257
image.setLayoutParams(params);

0 commit comments

Comments
 (0)