Android studio Kotlin:将数据从片段传递到recyclerview

Android studio Kotlin:将数据从片段传递到recyclerview,android-studio,android-fragments,kotlin,Android Studio,Android Fragments,Kotlin,我有一个recyclerview,其中包含通过API加载的产品。当你点击一个产品时,你会被发送到信息页面,那里有一个按钮,可以将该产品添加到一个新的recyclerview中。但我不知道如何将该产品发送到我的“愿望列表”回收视图。 链接到项目: 从API加载产品列表的片段 ` class DashboardFragment : Fragment() { private lateinit var dashboardViewModel: DashboardViewModel priv

我有一个recyclerview,其中包含通过API加载的产品。当你点击一个产品时,你会被发送到信息页面,那里有一个按钮,可以将该产品添加到一个新的recyclerview中。但我不知道如何将该产品发送到我的“愿望列表”回收视图。 链接到项目:

从API加载产品列表的片段

`
class DashboardFragment : Fragment() {
    private lateinit var dashboardViewModel: DashboardViewModel
    private lateinit var notificationsViewModel: NotificationsViewModel

    private val popularProducts = arrayListOf<Producten>()
    private val popularProductsAdapter =
        MainProductAdapter(popularProducts, onClickListener = this::clickOnPopularProduct)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        dashboardViewModel =
            ViewModelProviders.of(requireActivity())[DashboardViewModel::class.java]
        val root = inflater.inflate(R.layout.fragment_dashboard, container, false)

        val rvPopularProducten: RecyclerView = root.findViewById(R.id.rvPopularProducts)
        //rv van category
        val rvCategory: RecyclerView = root.findViewById(R.id.rvCategories)

        // connect the adapters to the recyclerviews
        rvPopularProducten.layoutManager =
            LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)
        rvPopularProducten.adapter = popularProductsAdapter

        loadData()

        return root
    }


    fun loadData() {
        dashboardViewModel.getProducts()
        popularProducts.clear()

        dashboardViewModel.product.observe(
            viewLifecycleOwner,
            Observer {
                this.popularProducts.clear()
                popularProducts.addAll(it.products)
                popularProductsAdapter.notifyDataSetChanged()
            })
    }


    private fun clickOnPopularProduct(view: View, product: Producten) {
        val transaction = requireFragmentManager().beginTransaction()
        val productInfoFragment = ProductInfoFragment()

        productInfoFragment.geselecteerdeProduct = product

        transaction.replace(R.id.nav_host_fragment, productInfoFragment)
        transaction.addToBackStack(null)
        transaction.commit()

    }
}
如果您单击addToWishListbtn,我想要我的产品的回收视图

class NotificationsFragment : Fragment() {

    private lateinit var notificationsViewModel: NotificationsViewModel
    val product = arrayListOf<Producten>()

    private val wenslijstAdapter =
        WenslijstAdapter(product)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        notificationsViewModel =
            ViewModelProviders.of(requireActivity())[NotificationsViewModel::class.java]

        val root = inflater.inflate(R.layout.fragment_notifications, container, false)
        val rvWenslijst: RecyclerView = root.findViewById(R.id.rvWenslijst1)




        rvWenslijst.layoutManager =
            LinearLayoutManager(this.context, LinearLayoutManager.VERTICAL, false)
        rvWenslijst.adapter = wenslijstAdapter




        return root
    }


}
类通知片段:片段(){
私有lateinit变量notificationsViewModel:notificationsViewModel
val product=arrayListOf()
二等兵瓦尔·温斯利斯塔普特=
WenslijstAdapter(产品)
覆盖创建视图(
充气机,
容器:视图组?,
savedInstanceState:捆绑?
):查看{
通知视图模型=
ViewModelProviders.of(requireActivity())[NotificationsViewModel::class.java]
val root=充气机。充气(R.layout.fragment\u通知,容器,false)
val rvWenslijst:RecyclerView=root.findViewById(R.id.rvWenslijst1)
rvWenslijst.layoutManager=
LinearLayoutManager(this.context,LinearLayoutManager.VERTICAL,false)
rvWenslijst.adapter=wenslijstAdapter
返回根
}
}

您可以在参数中传递项,如下所示:

   private fun clickOnPopularProduct(view: View, product: Producten) {
    val transaction = requireFragmentManager().beginTransaction()
    val productInfoFragment = ProductInfoFragment()

    **//(psuedocode)
    val bundle = Bundle()
    bundle.putExtra(“product”, //pass your product item here)
    productInfoFragment.arguments = bundle**

    transaction.replace(R.id.nav_host_fragment, productInfoFragment)
    transaction.addToBackStack(null)
    transaction.commit()

}
在ProductInfoFragment的onCreateView中,您可以在展开布局后调用:

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    productInfoViewModel = ViewModelProviders.of(this).get(ProductInfoViewModel::class.java)

    getArgs()

    val root = inflater.inflate(R.layout.product_info, container, false)


    …
     return root
}

private fun getArgs() {

  //here you can extract your extra(psedocode)
  geselecteerdeProduct = arguments?.getExtra(“product") 
}
同样,在该方法中:

private fun click() {

}

您应该将参数中的产品传递给下一个片段。

transaction.replace(R.id.nav\u host\u fragment,productInfoFragment)
如果您使用的是NavHostFragment和Jetpack导航,则不应替换NavHostFragment。您应该使用
navigation.xml
在片段之间导航。我现在看到这个想法是从问题继承来的,但是问题的代码已经不正确了。但是,使用
setArguments()
代替字段变量的想法无疑是一种改进。您实际上如何在一个片段到另一个片段之间导航?有大约4种方法可以做到这一点,所以要回答你的问题,知道这一点是至关重要的。哦,你有
R.id.nav\u host\u fragment
。这意味着您正在使用Jetpack导航。这意味着您没有包含
navigation.xml
,因此无法回答您的问题。不应该用另一个片段替换NavHostFragment,也不应该通过公共可变字段变量设置片段的参数。阅读#5,您的应用程序将在生产中崩溃。这是否回答了您的问题?
private fun click() {

}