@Koshkasobaka

Почему LinearLayoutManager не принимает параметр this?

В фрагменте 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()
    }
}
  • Вопрос задан
  • 48 просмотров
Решения вопроса 1
iLLuzor
@iLLuzor
Java, Kotlin, Android Developer
Но почему-то при создании LinearLayoutManager() параметр this либо this@NotesFragment подчеркивается красным

Не почему-то, а по определённой причине, которая будет отображена при наведении курсора.
Параметр не this. У параметра есть тип, в данном случае это Context. Собственно, контекст и надо передавать.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы