Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Recycler view resizes when app is on background

I have this very weird bug on my app. Every time I open my app my view works perfectly, but when I try to put my app in background and open my app again, my recycler view resizes.

Here's an image to describe it properly:

This is the correct image:

enter image description here

This is the image that messes up my recyclerview when the app is on background, then I open it again.

enter image description here

There are also times that all images in my recyclerview are gone. Here's a screenshot:

enter image description here

This is my onResume() code:

public void onResume()
 {
   Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
   if (fragment instanceof MenuFragment)
   {
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
     transaction.remove(fragment);
     transaction.commit();
     transaction = getSupportFragmentManager().beginTransaction();
     transaction.replace(R.id.fragment_container, fragment);
     transaction.addToBackStack(null);
     transaction.commit();

   }
 }

This is my view pager layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_background">
    <android.support.constraint.ConstraintLayout
        android:id="@+id/action_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <include
            android:id="@+id/nav_bar"
            layout="@layout/home_screen_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

    <com.yotadevices.widget.RtlViewPager
        android:id="@+id/menuFragment_viewPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toTopOf="@+id/buttons"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/nav_bar"/>
    <include
        android:id="@+id/buttons"
        layout="@layout/navigation_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/menuFragment_viewPager" />
</android.support.constraint.ConstraintLayout>

Here's my list layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

What do you think is the problem here?

PS: The bug occurs only on selected phones. Not all. We've tested this on Samsung Galaxy s8+ and it works well.

like image 847
PinoyStackOverflower Avatar asked Apr 13 '18 09:04

PinoyStackOverflower


People also ask

How to set the random background for recyclerview in Android?

Using recycler view we can show grids and list of items. This example demonstrates How to set the random background for Recyclerview by creating a beautiful student records app that displays student name with age. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How to work with recycler view in Android app?

This example demonstrates about Working with Recycler View in Android App Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

What is nested recyclerview in Android?

Nested View means adding a Horizontal RecyclerView in a Vertical RecyclerView. This type of Nesting may reduce RecyclerView performance. Below is the image of Nested RecyclerView. 3. Use the setHasFixedsize method

What is recyclerview in Android UI element?

This post will focus on the UI Element for Android that is known as RecyclerView. It is an advanced scrolling list that has flexible configurations. It can be used to display contents that consist of a large dataset or datasets that are frequently changing.


2 Answers

If you are using only single child view, it's better to use FrameLayout. Then in RecyclerView use match_parent parameters in layout attribute, so your code should look like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

By the way my best method to debug scenarios like this is to write android:background="#0000FF" in every view, to see how it inflates.

like image 74
Equa Avatar answered Sep 27 '22 23:09

Equa


I have solved the problem by breaking part the included layouts. So instead of adding an included layout, I just transferred all the included in to the main XML layout.

Even though this fixed the problem, i still do not know why the bug occurred in the first place.

like image 24
PinoyStackOverflower Avatar answered Sep 27 '22 23:09

PinoyStackOverflower