Kotlin:从片段中传递数据到回收视图

问题描述 投票:0回答:1

我有一个回收视图,里面的产品是通过我的API加载的。当你点击一个产品时,你会被发送到信息页面,在那里有一个按钮,将该产品添加到一个新的回收视图。但我不知道如何将该产品发送到我的 "愿望清单 "回收视图。https:/github.comarfeen14arfeenShopApp。

从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()

    }
}

当你点击产品时,产品信息会在这里显示。

 class ProductInfoFragment : Fragment() {

    private lateinit var productInfoViewModel: ProductInfoViewModel
    var geselecteerdeProduct: Producten? = null

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

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

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


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

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            root.tvProducInfo_info.text =
                Html.fromHtml(geselecteerdeProduct!!.bodyHtml, Html.FROM_HTML_MODE_LEGACY)
        } else {
            root.tvProducInfo_info.text = Html.fromHtml(geselecteerdeProduct!!.bodyHtml)
        }

        Glide.with(root.context).load(geselecteerdeProduct!!.imagePath.productImgPath)
            .into(root.imgProduct_info)
        root.tvProduct_info_name.text = "€ " + geselecteerdeProduct!!.variants[0].productPrice

        root.btnAddToWishList.setOnClickListener() {
            click()

        }
        return root
    }


    private fun click() {

    }
}

当你点击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
    }


}
android-studio android-fragments kotlin
1个回答
-1
投票

你可以通过下面的参数来传递项目。

   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() {

}

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

© www.soinside.com 2019 - 2024. All rights reserved.