因此,我正在为我的项目遵循kotlin(https://github.com/android10/Android-CleanArchitecture-Kotlin)中的这种干净的体系结构,我想在回收者视图加载数据之前添加进度条,并在数据加载后使其消失。在此示例中,进度条位于单独的工具栏中,但就我而言,我希望位于回收者视图的中心。
这是我的布局:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/list_wrapper_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:background="@drawable/layout_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/movieList_recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_bar"
app:layout_constraintEnd_toEndOf="@+id/list_horizontal_guideline_end"
app:layout_constraintStart_toStartOf="@+id/list_horizontal_guideline_start"
app:layout_constraintTop_toBottomOf="@+id/searchView" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="@color/black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
在我的课上:
class MoviesFragment : BaseFragment() {
@Inject lateinit var navigator: Navigator
@Inject lateinit var moviesAdapter: MoviesAdapter
private lateinit var moviesViewModel: MoviesViewModel
override fun layoutId() = R.layout.fragment_movies
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
appComponent.inject(this)
moviesViewModel = viewModel(viewModelFactory) {
observe(movies, ::renderMoviesList)
failure(failure, ::handleFailure)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initializeView()
loadMoviesList()
}
private fun initializeView() {
movieList_recyclerView.layoutManager = StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
movieList_recyclerView.adapter = moviesAdapter
}
private fun loadMoviesList() {
progressBar.visible()
movieList_recyclerView.invisibile()
moviesViewModel.loadMovies()
}
private fun renderMoviesList(movies: List<MovieView>?) {
moviesAdapter.collection = movies.orEmpty()
progressBar.invisible()
movieList_recyclerView.visible()
}
private fun handleFailure(failure: Failure?) {
when (failure) {
is NetworkConnection -> renderFailure(R.string.failure_network_connection)
is ServerError -> renderFailure(R.string.failure_server_error)
is ListNotAvailable -> renderFailure(R.string.failure_movies_list_unavailable)
}
}
private fun renderFailure(@StringRes message: Int) {
movieList.invisible()
emptyView.visible()
hideProgress()
notifyWithAction(message, R.string.action_refresh, ::loadMoviesList)
}
}
但是进度条根本不显示。我在做什么错了?