Basic use of RecycleView and Adapter

1. Create a layout file. Word "fragment" is used to put Recycle and word "fragment" grad "cell is used to put small cells

Word? Fragment layout:

Word? Fragment? Grad? Cell layout: (card layout)

2. Create the adapter wordFragmentAdapter class

package com.example.ngsl.wordFragment

import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.ngsl.R
import com.example.ngsl.room.Word
import kotlinx.android.synthetic.main.word_fragment_grad_cell.view.*

//Adapter: tell the view what it has and how to fill it
class WordFragmentAdapter : ListAdapter<Word, MyViewHolder>(DiffCallBack) {
    //Comparator, comparing objects in each row
    object DiffCallBack : DiffUtil.ItemCallback<Word>() {
        override fun areItemsTheSame(oldItem: Word, newItem: Word): Boolean {
            //Is comparison the same object
            return oldItem.wordId == newItem.wordId
        }
        override fun areContentsTheSame(oldItem: Word, newItem: Word): Boolean {
            //Whether the comparison contents are the same
            return oldItem == newItem
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        //Load the view, that is, every small unit word? Fragment? Grad? Cell
        val view = LayoutInflater.from(parent.context).inflate(R.layout.word_fragment_grad_cell, parent, false)
        val holder = MyViewHolder(view)
        holder.itemView.apply {
            //Put the listening event here, and the new listening event will not be created every time an item is loaded
            val onClickListener = View.OnClickListener {
                val uri = Uri.parse("https://m.youdao.com/dict?le=eng&q=${this.textViewEnglish.text}")
                Intent(Intent.ACTION_VIEW).apply {
                    this.data = uri
                    holder.itemView.context.startActivity(this)
                }
            }
            this.textViewEnglish.setOnClickListener(onClickListener)
            this.imageView.setOnClickListener(onClickListener)
        }
        return holder
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        //load resources
        holder.itemView.apply {
            val item = getItem(position)
            this.textViewWordId.text = item.wordId.toString()
            this.textViewEnglish.text = item.english
        }
    }
}

//This is very important
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

3. Load adapter in wordFragment

fragment is best operated in onActivityCreated

package com.example.ngsl.wordFragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.example.ngsl.R
import kotlinx.android.synthetic.main.word_fragment.*

class WordFragment : Fragment() {

	//ViewModel manages the
    private lateinit var viewModel: WordViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.word_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        //Create adapter
        val wordFragmentAdapter = WordFragmentAdapter()
        recycleView.apply {
            adapter = wordFragmentAdapter
            //Page layout, 1 column
            layoutManager = GridLayoutManager(requireContext(), 1)
        }

        //ViewModel
        viewModel = ViewModelProvider(this).get(WordViewModel::class.java)
        viewModel.allWord.observe(viewLifecycleOwner, Observer {
            //Provide data for the adapter
            wordFragmentAdapter.submitList(it)
        })
    }
}

4.GitHub source connection

There are many components in the whole project, just look at the parts you need: https://github.com/YDDUONG/NGSL-English
If it's useful, like it

81 original articles published, 40 praised, 30000 visitors+
Private letter follow

Tags: Android Fragment github Java

Posted on Wed, 12 Feb 2020 10:36:44 -0800 by dlester