K
K
Koshkasobaka2021-06-07 11:03:46
Android
Koshkasobaka, 2021-06-07 11:03:46

Why doesn't the LinearLayoutManager take a this parameter?

The NotesFragment should display a list using RecycleView. But for some reason, when creating a LinearLayoutManager(), the this or [email protected] parameter is underlined in red. What am I doing wrong?

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
    }
}


Adapter class:
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()
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2021-06-07
@Koshkasobaka

But for some reason, when creating a LinearLayoutManager(), the this or [email protected] parameter is underlined in red

Not for some reason, but for a certain reason, which will be displayed on hover.
The parameter is not this. The parameter has a type, in this case it is Context. Actually, the context must be passed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question