列表适配器不会填充应用程序的第二个片段上的适配器



我有一个包含 2 个片段的应用程序,这两个片段都有从同一适配器填充的列表。

第一个工作正常,但第二个 -

class CountryBordersFragment : Fragment(R.layout.fragment_country_borders) {
private lateinit var selectedCountry: String
private lateinit var countriesViewModel: CountriesListViewModel
private lateinit var countriesAdapter: CountriesListAdapter
private var countriesList = mutableListOf<CountryEntity>()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
countriesViewModel = ViewModelProvider(this).get(CountriesListViewModel::class.java)
initData()
initClickListener()
}
private fun initClickListener() {
backButton.setOnClickListener {
requireActivity().onBackPressed()
}
}
private fun initData() {
countriesAdapter = CountriesListAdapter(null)
countriesAdapter.submitList(countriesList)
countriesRecyclerView.setHasFixedSize(true)
countriesRecyclerView.layoutManager = LinearLayoutManager(context)
countriesRecyclerView.adapter = countriesAdapter
arguments?.let {
selectedCountry = it.getString(getString(R.string.countries_list_fragment_selected_country))!!
}
countryName.text = selectedCountry
countriesViewModel.getCountryBorders(selectedCountry).observeOnce(requireActivity(), Observer { countryBorder ->
if (countryBorder.neighborCountries.isEmpty()) {
bordersWith.text = getString(R.string.country_border_fragment_country_does_not_have_borders)
return@Observer
}
countriesViewModel.getCountryByCioc(countryBorder.neighborCountries).observe(requireActivity(), Observer { countryEntityList ->
countriesAdapter.submitList(countryEntityList)
})
})
}
}

根本不填充适配器。它只是不显示任何列表。

肯定缺少一些东西,因为我能够在第一个片段正确填充我的适配器,但来到这个片段时,列表没有弹出。

这是我的列表适配器实现 -

class CountriesListAdapter(private val callback: CountryViewHolder.OnCountryClickListener?)
: ListAdapter<CountryEntity, CountryViewHolder>(CountriesDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountryViewHolder {
val view = LayoutInflater.from(App.context!!).inflate(R.layout.country_viewholder, parent, false)
return CountryViewHolder(view)
}
override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {
holder.bind(getItem(position), callback)
}
class CountriesDiffCallback : DiffUtil.ItemCallback<CountryEntity>() {
override fun areItemsTheSame(oldItem: CountryEntity, newItem: CountryEntity): Boolean {
return oldItem.countryName == newItem.countryName
}
override fun areContentsTheSame(oldItem: CountryEntity, newItem: CountryEntity): Boolean {
return oldItem == newItem
}
}
}

和我的视图持有人和模型 -

class CountryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val rootLayout: LinearLayout = view.country_viewholder_root_layout
private val nativeName: TextView = view.country_viewholder_native_name
private val countryName: TextView = view.country_viewholder_country_name
private val area: TextView = view.country_viewholder_area
private val countryImage: ImageView = view.country_viewholder_country_image
fun bind(model: CountryEntity, callback: OnCountryClickListener?) {
nativeName.text = App.context!!.getString(R.string.country_view_holder_native_name).plus(" ${model.countryName}")
countryName.text = App.context!!.getString(R.string.country_view_holder_country_name).plus(" ${model.nativeName}")
area.text = App.context!!.getString(R.string.country_view_holder_country_area).plus(" ${model.area}")
//        Glide.with(App.context!!).load("https://www.talkwalker.com/images/2020/blog-headers/image-analysis.png").into(countryImage)
Picasso.get().load(model.imageUri).into(countryImage)
rootLayout.setOnClickListener {
callback?.onCountryClicked(model.countryName)
}
}
interface OnCountryClickListener {
fun onCountryClicked(countryName: String)
}
}
@Entity(tableName = countriesTable, primaryKeys = ["countryName"])
class CountryEntity(
val countryName: String,
val nativeName: String,
val area: Double,
val cioc: String? = null,
val imageUri : String? = null
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as CountryEntity
if (countryName != other.countryName) return false
if (nativeName != other.nativeName) return false
if (area != other.area) return false
if (cioc != other.cioc) return false
if (imageUri != other.imageUri) return false
return true
}
override fun hashCode(): Int {
var result = countryName.hashCode()
result = 31 * result + nativeName.hashCode()
result = 31 * result + area.hashCode()
result = 31 * result + (cioc?.hashCode() ?: 0)
result = 31 * result + (imageUri?.hashCode() ?: 0)
return result
}
}
class CountryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val rootLayout: LinearLayout = view.country_viewholder_root_layout
private val nativeName: TextView = view.country_viewholder_native_name
private val countryName: TextView = view.country_viewholder_country_name
private val area: TextView = view.country_viewholder_area
private val countryImage: ImageView = view.country_viewholder_country_image
fun bind(model: CountryEntity, callback: OnCountryClickListener?) {
nativeName.text = App.context!!.getString(R.string.country_view_holder_native_name).plus(" ${model.countryName}")
countryName.text = App.context!!.getString(R.string.country_view_holder_country_name).plus(" ${model.nativeName}")
area.text = App.context!!.getString(R.string.country_view_holder_country_area).plus(" ${model.area}")
//        Glide.with(App.context!!).load("https://www.talkwalker.com/images/2020/blog-headers/image-analysis.png").into(countryImage)
Picasso.get().load(model.imageUri).into(countryImage)
rootLayout.setOnClickListener {
callback?.onCountryClicked(model.countryName)
}
}
interface OnCountryClickListener {
fun onCountryClicked(countryName: String)
}
}

我错过了什么?刚开始从普通的RecyclerView.Adapter使用ListAdapter。

看起来你失踪了 -notifyDataSetChanged()

就在之后——

countriesAdapter.submitList(countryEntityList)

加-

countriesAdapter.notifyDataSetChanged()

最新更新