问题
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