androidandroid-fragmentsandroid-recyclerviewandroid-edittextandroid-nested-fragment

Unable to NotifyDataSetChanged on a RecyclerView when EditText is in Focus


I have an Activity with a fragment called MainFragment which has a ViewPager2 to display a few Fragments. One of which is SearchFragment. SearchFragment has an EditText with a TextWatcher, upon text change, an API call is being made to fetch the suggestions. When I'm setting these suggestions to a RecyclerView which is shown right below the EditText, it won't get updated unless I touch the RecyclerView.

I've tried running a Handler to make the API call and update the RecyclerView without letting the EditText gain Focus, and it worked. RecyclerView updates the Data just fine when EditText is not Focused.

<androidx.constraintlayout.widget.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/search_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/almostBlack"
    tools:context=".presentation.ui.modules.search.view.SearchFragment">

    <com.vishal.presentation.ui.common.custom.ClearableAutoCompleteTextView
        android:id="@+id/etSearchText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent"
        android:ellipsize="end"
        android:focusable="true"
        android:fontFamily="@font/sfprotext_regular"
        android:gravity="center|start"
        android:hint="@string/search_hint"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:maxLength="50"
        android:maxLines="1"
        android:padding="12dp"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textColorHint="@color/veryLightPink"
        android:textSize="16sp"
        app:clearIconDrawWhenFocused="true"
        app:clearIconDrawable="@drawable/ic_close_white_24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/autoCompleteSuggestions"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/almostBlack"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etSearchText" />

</androidx.constraintlayout.widget.ConstraintLayout>


Solution

  • There was a problem with ViewPager2 Library.

    I was using "androidx.viewpager2:viewpager2:1.0.0-beta05" which had a Bug that would cause View Elements to lose focus if EditText is in Focus.

    Updated to "androidx.viewpager2:viewpager2:1.0.0-rc01" and Issue is resolved now.