【发布时间】:2019-11-11 10:26:00
【问题描述】:
我有一个名为 MainFragment 的片段的 Activity,它有一个 ViewPager2 来显示一些片段。其中之一是 SearchFragment。 SearchFragment 有一个带有 TextWatcher 的 EditText,在文本更改时,将调用 API 来获取建议。当我将这些建议设置为显示在 EditText 正下方的 RecyclerView 时,除非我触摸 RecyclerView,否则它不会更新.
我已经尝试运行一个处理程序来进行 API 调用并更新 RecyclerView 而不让 EditText 获得焦点,并且它起作用了。当 EditText 没有 Focused 时,RecyclerView 更新 Data 就好了。
<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>
【问题讨论】:
-
你能分享更多细节吗? (Fragment 和 Adapter 类)你是在 API 结果之后调用 notifyDataSetChanged 吗?
-
恐怕我做不到。我会制作一个虚拟项目并分享它。
-
感谢@HamzaKhan 的回复。现在问题已经解决了。 ViewPager2 库中存在错误,已对其进行了更新,现在可以正常工作了。
标签: android android-fragments android-recyclerview android-edittext android-nested-fragment