DialogFragment не обновляет свой макет - PullRequest
0 голосов
/ 18 марта 2020

У меня был фрагмент диалога, содержащий listView, созданный непосредственно из действия со списком элементов для listView, что-то вроде этого: UsersDialogFragment (usersList: List). Затем список обрабатывался простым ArrayAdapter, и все работало просто отлично. Теперь требования изменены, и мне нужно показать фрагмент моего диалогового окна, запустить выборку списка пользователей и показать индикатор выполнения, пока не будет готов RecyclerView, поэтому я пришел к следующему:

MainActivity

class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener {
private val usersDialogFragment = UsersDialogFragment()
override fun onCreate(savedInstanceState: Bundle?) {
...
//code for the activity
viewModel.usersList .observe(this, Observer { usersList ->
        usersList ?.let {
if (usersDialogFragment.isVisible) {
 usersDialogFragment.prepareAndShowUsersList(usersList,currentUser)
   }
}
....
//other code for the activity 
}

UsersDialogFragment

class UsersDialogFragment : DialogFragment() {

private var usersList = ArrayList<User>()

private var currentUser: String? = null

private var userRecyclerViewAdapter = UserRecyclerViewAdapter(usersList,currentUser)

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.fragment_users_dialog, container, true)
    usersRecyclerView  = view.findViewById(R.id.users_dialog_recyclerView)
    usersRecyclerView.adapter = userRecyclerViewAdapter
    return view
}

fun prepareAndShowUsersList(usersList: List<SapUser>, currentUser: String?) {
    usersList.clear()
    usersList.addAll(usersList)
    userRecyclerViewAdapter.notifyDataSetChanged()
 //Now I would like to hide the progress bar and show the recyclerView
 //but even if these two methods are called nothing happens 
getDialog().findViewById<ProgressBar>(R.id.users_progresBar).visibility = View.Invisible
getDialog().findViewById<RecyclerView>(R.id.users_dialog_recyclerView).visibility = View.Visible
  }
}

FragmentLayout

<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/custom_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragments.UsersDialogFragment">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/users_dialog_recyclerView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:scrollbars="none"
    android:visibility="invisible"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/parent"
    app:layout_constraintTop_toBottomOf="@+id/parent"/>

<ProgressBar
    android:id="@+id/users_progresBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    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_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Итак, фрагмент показывает, список получен, действие вызывает метод «prepareAndShowUsersList» фрагмента, список обновляется адаптер уведомляется, но представление рециркулятора остается пустым, а видимость не изменяется. Я пытался всеми видами методов для получения представления, как: requireDialog (). RequireViewById - this.view? .FindViewById - requireView (). FindViewById - this.dialog? .FindViewById - requireDialog (). RequireViewById, даже с inflater.inflate, даже с inflater.inflate (R.layout.fragment_users_dialog, container, false) или inflater.inflate (R.layout.fragment_users_dialog, null), но индикатор выполнения продолжает вращаться, а список не обновляется / не отображается.

Где может быть проблема?

...