RecyclerView留为空白,不绑定任何数据

时间:2019-05-09 18:21:29

标签: android android-fragments recycler-adapter recyclerview-layout

我在fragment(viewContractorsFragment)中使用了recyclerview(id recyclerView),我放大了fragment布局的根视图并初始化了recycler视图。相应的适配器和布局管理器也已正确设置。 但是在加载时,包含recyclerview的片段为空白。

ViewContractorsFragment.kt

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".fragments.ViewContractorsFragment">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
</FrameLayout>

contract_list_row.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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:clickable="true"
        android:padding="16dp"
        android:background="?android:attr/selectableItemBackground"
        android:orientation="vertical">

    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorWhite"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">

        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="4dp">

            <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="16dp"
                    android:orientation="vertical">

                <TextView android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:id="@+id/tv_contractor_name"
                          android:text="Rana Prathap"
                          android:textStyle="bold"
                          android:textSize="24sp"
                          android:textColor="@android:color/black"
                          android:paddingTop="16dp"
                          android:paddingBottom="8dp"/>

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Event Photographer"
                        android:id="@+id/tv_contractor_profession"
                        android:textAllCaps="true"
                        android:paddingBottom="8dp"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:textColor="@android:color/holo_green_dark"/>
                <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                    <RatingBar
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/rb_contractor_rating"
                            android:rating="4"
                            android:layout_marginTop="2dp"
                            android:paddingTop="8dp"
                            style="?android:attr/ratingBarStyleSmall"
                            android:numStars="5"
                            android:stepSize="0.5"
                            android:clickable="false"
                            android:focusable="false"
                            android:isIndicator="true"/>

                    <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/tv_contractor_rating"
                            android:textSize="24sp"
                            android:layout_marginLeft="8dp"
                            android:textColor="@android:color/black"
                            android:textStyle="bold"
                            android:text="4.5"/>


                </LinearLayout>


            </LinearLayout>

            <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:orientation="vertical">
                <ImageView
                        android:layout_width="125dp"
                        android:layout_height="100dp"
                        android:src="@mipmap/ic_health_fitness"
                        android:scaleType="fitXY"/>

                <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:layout_marginTop="4dp">
                    <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_phone_blue_24dp"
                            android:padding="8dp"/>
                    <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_message_blue_24dp"
                            android:padding="8dp"/>

                    <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_share_blue_24dp"
                            android:padding="8dp"/>

                </LinearLayout>

            </LinearLayout>


        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

ContractorAdapter.kt

class ContractorAdapter(val context: Context?, val contractorsList: List<Contractor>) : RecyclerView.Adapter<ContractorAdapter.ContractViewHolder>()
{
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContractViewHolder {
        val itemView = LayoutInflater.from(context).inflate(R.layout.contract_list_row,parent,false)
        return ContractViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return contractorsList.size
    }

    override fun onBindViewHolder(holder: ContractViewHolder, position: Int) {

       val contractor = contractorsList[position]
        holder.setData(contractor,position)
    }

    inner class ContractViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){

        fun setData(contractor: Contractor?, position: Int) {
            itemView.tv_contractor_name.text = contractor!!.name
            itemView.tv_contractor_profession.text = contractor!!.profession
            itemView.tv_contractor_rating.text = contractor!!.rating.toString()
            itemView.rb_contractor_rating.rating = contractor!!.rating
        }
    }
}

ViewContractorsFragment.kt

class ViewContractorsFragment : Fragment() {

    private lateinit var recyclerViewContractors: RecyclerView
    private lateinit var rootView: View

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        rootView = inflater.inflate(R.layout.fragment_view_contractors, container, false)
        recyclerViewContractors = rootView.findViewById(R.id.recyclerView) as RecyclerView
        initAdapter()
        return view
    }

    override fun onResume() {
        super.onResume()

        // Showing and setting the title for the dashboard activity when the fragment is loaded
        (activity as AppCompatActivity).supportActionBar!!.show()
        (activity as AppCompatActivity).supportActionBar!!.title = resources.getString(R.string.view_contracts)
    }

    override fun onStop() {
        super.onStop()

        // Hiding the support action bar in the dashboard activity when fragment exits
        (activity as AppCompatActivity).supportActionBar!!.hide()
    }

    private fun initAdapter() {
        val layoutManager = LinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        recyclerViewContractors.layoutManager = layoutManager

        recyclerViewContractors.adapter = ContractorAdapter(activity, Supplier.contractorsList)
    }
}

1 个答案:

答案 0 :(得分:0)

您正在将片段布局放大为rootView并使用其他变量视图返回

如下修改

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        rootView = inflater.inflate(R.layout.fragment_view_contractors, container, false)
        recyclerViewContractors = rootView.findViewById(R.id.recyclerView) as RecyclerView
        initAdapter()
        return rootview
    }

如果问题仍然存在,请检查Supplier.contractorsList是否包含数据