В фрагменте NotesFragment должен отображаться список, использую RecycleView. Но почему-то при создании LinearLayoutManager() параметр this либо this@NotesFragment подчеркивается красным. Что я делаю не так?
class NotesFragment : Fragment() {
private var binding: FragmentListBinding? = null
private val adapter = NoteAdapter()
companion object {
private const val PARAMS_KEY = "listFragmentKey"
fun newInstance(params: ListFragmentParams) = NotesFragment().apply {
arguments = Bundle().apply { putParcelable(PARAMS_KEY, params) }
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentListBinding.inflate(layoutInflater)
return binding?.root
binding!!.rcList.layoutManager = LinearLayoutManager(this)
binding!!.rcList.adapter = adapter
}
override fun onDestroy() {
super.onDestroy()
binding = null
}
}
Класс-адаптер:
class NoteAdapter : RecyclerView.Adapter<NoteAdapter.NoteHolder>() {
private val noteList = ArrayList<String>()
class NoteHolder(item: View) : RecyclerView.ViewHolder(item) {
private val binding = MyListItemBinding.bind(item)
fun bind(authorText: String) = with(binding) {
tvMessage.text = authorText
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.my_list_item, parent, false)
return NoteHolder(view)
}
override fun onBindViewHolder(holder: NoteHolder, position: Int) {
holder.bind(noteList[position])
}
override fun getItemCount(): Int {
return noteList.size
}
fun addNote(note: String) {
noteList.add(note)
notifyDataSetChanged()
}
}