简体   繁体   中英

How to open a "Details" Fragment onclick of Recyclerview in Kotlin

I have home fragment and I want it to go to another fragment which will show its "details", which passes the data of the clicked recyclerview item to that "details" fragment.

在此处输入图像描述

When I click the article it will move to detail article which passes the data.

As for the code, here's the adapter:

class ArticleAdapter(private val articleList: ArrayList<Article>) :
    RecyclerView.Adapter<ArticleAdapter.MyViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val binding = ItemArticleBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return MyViewHolder(binding)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val data = articleList[position]
        holder.bind(data)
    }

    class MyViewHolder(private val binding: ItemArticleBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(data: Article) {
            Glide.with(binding.root.context)
                .load(data.img)
                .into(binding.articleImage)
            binding.articleTitle.text = data.title

            binding.root.setOnClickListener {
                val article = Article(
                    data.title,
                    data.img,
                    data.content
                )
            }
        }
    }

    override fun getItemCount(): Int {
        return articleList.size
    }
}

Here's my detailFragment

class DetailArticleFragment : Fragment() {
    private var _binding: FragmentDetailArticleBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentDetailArticleBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val intent = Intent(binding.root.context, DetailArticleFragment::class.java)
        val article = intent.getParcelableExtra<Article>(DETAIL_ARTICLE) as Article
        Glide.with(this)
            .load(article.img)
            .into(_binding!!.articleImage)
        _binding!!.articleTitle.text = article.title
        _binding!!.articleDescription.text = article.content
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
    companion object {
        const val DETAIL_ARTICLE = "detail_article"
    }
}

You need to pass click listener interface to adapter, for example:

typealias OnArticleClick = (article: Article) -> Unit

class ArticleAdapter(
    private val articleList: ArrayList<Article>
) :
    RecyclerView.Adapter<ArticleAdapter.MyViewHolder>() {
    var onArticleClick: OnArticleClick? = null
    ...
            binding.root.setOnClickListener {
                val article = Article(
                    data.title,
                    data.img,
                    data.content
                )
                onArticleClick?.invoke(article)
            }
    ...
}

And init onArticleClick in your home fragment with RecyclerView:

adapter.apply {
    onArticleClick = {
        // show details fragment
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM