【问题标题】:recyclerview not rendering to screenrecyclerview 不渲染到屏幕
【发布时间】:2020-06-02 07:37:28
【问题描述】:

我有以下 XML 文件 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E0E0E0 "
    android:orientation="vertical"
    tools:context=".fragments.CountriesListFragment">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/white"
        app:title="Countries" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/fragment_countries_list_countries_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:listitem="@layout/country_viewholder" />


</LinearLayout>

和下面的片段 -

class CountriesListFragment : Fragment(R.layout.fragment_countries_list) {


    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)
        setHasOptionsMenu(true)
        initViewModel()
        init()
    }


    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.country_list_menu, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }

    private fun initViewModel(){
        countriesViewModel = ViewModelProvider(this).get(CountriesListViewModel::class.java)
        countriesViewModel.fetchAllCountries(object : CountriesRepository.CountryFetchCallback {
            override fun onFetchError(reason: String) {
                Toast.makeText(context, reason, Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun init() {
        countriesAdapter = CountriesListAdapter(countriesList)
        countriesRecyclerview.adapter = countriesAdapter
        countriesRecyclerview.setHasFixedSize(true)
        countriesRecyclerview.layoutManager = LinearLayoutManager(context)
        countriesViewModel.getAllCountries().observe(requireActivity(), Observer { countryList ->
            countriesList.addAll(countryList)
            countriesAdapter.notifyDataSetChanged()
        })
    }
}

编辑 -

我的适配器 -

class CountriesListAdapter(private val countriesList: List<CountryEntity>) : RecyclerView.Adapter<CountryViewHolder>() {
    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 getItemCount(): Int  = countriesList.size

    override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {
        holder.bind(countriesList[position])
    }

}

我的viewholder xml -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tool="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@color/white"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/country_viewholder_native_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textSize="16sp"
                tools:text="Country native name" />

            <TextView
                android:id="@+id/country_viewholder_country_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textSize="16sp"
                tools:text="Country name" />

            <TextView
                android:id="@+id/country_viewholder_area"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textSize="16sp"
                tools:text="Area" />

        </LinearLayout>


        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/country_viewholder_country_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:src="@mipmap/ic_launcher" />

    </LinearLayout>


</LinearLayout>

持有片段的活动 -

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CountriesListActivity">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/app_navigation" />


</androidx.constraintlayout.widget.ConstraintLayout>

thw 导航图 -

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/app_navigation"
    app:startDestination="@id/countriesListFragment">

    <fragment
        android:id="@+id/countriesListFragment"
        android:name="com.countriesborders.fragments.CountriesListFragment"
        android:label="fragment_countries_list"
        tools:layout="@layout/fragment_countries_list">
        <action
            android:id="@+id/action_countriesListFragment_to_countryBordersFragment"
            app:destination="@id/countryBordersFragment" />
    </fragment>
    <fragment
        android:id="@+id/countryBordersFragment"
        android:name="com.countriesborders.fragments.CountryBordersFragment"
        android:label="fragment_country_borders"
        tools:layout="@layout/fragment_country_borders" />
</navigation>

由于某种原因,数据没有呈现到屏幕上。我真的不知道是什么原因造成的,但我对正在发生的事情一无所知。

我尝试将手动硬编码的值添加到列表中,但它根本没有帮助。

有什么想法吗?

【问题讨论】:

  • 这里只是一个注释,但您可能希望使用 match_parent 作为回收器视图高度。此外,如果在使用实时数据时出现问题,最简单的检查方法是查看数据是否实际更改。如果调用 countriesViewModel.getAllCountries() 的观察者,则问题可能出在回收站视图上,但如果不是,则问题很可能出在实时数据机制上。可以加个日志来检查一下这个case吗?
  • 我已经检查了实时数据 - 数据确实到达并且正在填写列表。 OnBindViewHolder 正在被调用,bind() 函数正在被调用。所以一切看起来都是正确的,但列表没有被渲染到屏幕上
  • 你能发布适配器,也许是回收站视图项目布局?也许您在适配器中为 getItemCount 返回 0?
  • countriesRecyclerview.layoutManager = LinearLayoutManager(context, LinearLayout.VERTICAL, false)
  • 第一次调试。检查列表大小

标签: android android-recyclerview android-adapter


【解决方案1】:

您的布局中有两个相互冲突的维度。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

如您所见,最内部的一个是指示系统绘制与父高度匹配的项目,但父布局说要包装其内容(最终导致高度为 0)。如果将内部的更改为 match_parent,它将起作用并显示项目。

【讨论】:

  • 感谢上帝。我不会在一年内解决这个问题。
  • 不客气。此外,您可能会删除最外面的 LinearLayout,因为它只有另一个 LinearLayout 作为子级。没看好,不过应该是可以的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-02
相关资源
最近更新 更多