【问题标题】:setOnClickListener RecycleView (in a Fragment) navigate to another FragmentsetOnClickListener RecycleView(在一个片段中)导航到另一个片段
【发布时间】:2022-01-10 01:14:44
【问题描述】:

我在Fragment 中有RecycleView。当我单击RecyclerView 中的一项时,我想打开新的Fragment。第二个Fragment 没有替换前一个但Fragment 被覆盖 Image。我该如何解决这个问题?

我的适配器:

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // Get element from your dataset at this position and replace the
        // contents of the view with that element

            val currentProduct = productList[position]

            holder.image.setImageBitmap(currentProduct.image)
            holder.title.text = currentProduct.title
            holder.price.text = currentProduct.price.toString()+"€"



            //Go to product details
            holder.itemView.setOnClickListener {
                val activity = it.context as AppCompatActivity
                val detailsFragment = ProductDetailsFragment()
                activity.supportFragmentManager.beginTransaction().replace(R.id.fragment,detailsFragment).addToBackStack(null).commit()
            }



    }

【问题讨论】:

    标签: android kotlin android-fragments android-recyclerview


    【解决方案1】:

    使用导航组件,流畅地导航到(action_from1fragment_to2fragment)。

    如果你想打开特定的对话框,带有特定数据的片段,你可以在适配器中使用 lambda 来为视图模型、片段提供 id。

    【讨论】:

    • 我可以使用导航组件,但问题是我在不同的片段中使用了两倍的recycleview。例如:profilepage->details and product ->details
    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 也许这只是解决方法,但试试这个。 binding.recyclerView.visibility = View.GONE 以及所有需要在该可见性上消失的东西。
    【解决方案2】:

    简单的答案是将elevationbackground color添加到第二个片段的父布局中,并将clickablefocusable等参数设置为true。 你的onClickrecyclerView 工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-12
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多