Recyclerview not showing kotlin [duplicate]

情到浓时终转凉″ 提交于 2020-05-17 06:45:13

问题


Recyclerview is not showing on screen just show white background.

Is it there any mistake? Please tell me, thanks.

Adapter
class NeedsAdapter(var context: Context) : RecyclerView.Adapter<NeedsAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NeedsAdapter.ViewHolder {
        var view = View.inflate(parent.context, R.layout.needs_item, null)
        var parms = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        view.layoutParams = parms
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return 10;
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    }


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

    }

}

Fragment

class NeedsFragment : Fragment() {

    private lateinit var adapter: NeedsAdapter
    private lateinit var layout: View
    private lateinit var recyclerView: RecyclerView
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        layout = inflater.inflate(R.layout.fragment_needs, container, false)
        adapter = NeedsAdapter(this!!.activity!!)
        recyclerView = layout.findViewById(R.id.recyclerview)
        recyclerView.addItemDecoration(DividerItemDecoration(context!!, LinearLayoutManager.VERTICAL))
        recyclerView.adapter = adapter
        adapter.notifyDataSetChanged()

        return view
    }

}

I want to show just items on recyclerview.


回答1:


You have missed setting layout manager

recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL ,false)


来源:https://stackoverflow.com/questions/58060279/recyclerview-not-showing-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!