【问题标题】:Show products from server with diffrent sorts显示来自不同类型服务器的产品
【发布时间】:2021-11-10 20:19:40
【问题描述】:

我正在开发一个使用 MVVM & RXJAVA & KOIN 和 kotlin 的电子商务 Android 应用程序,并且产品请求 URL 具有查询类型参数,这些参数将显示最新产品、最受欢迎的产品、最贵到最便宜和最便宜到最贵的产品,我想用 RecyclerView 在主页中显示排序,但我所有的 RecyclerViews 都显示相同的东西。我该如何解决这个问题?

interface ApiService{ 

    @GET("product/list")
    fun getLatestProducts(@Query("sort")sort:String):Single<List<Product>>
}

    class MainViewModel(productRepository: ProductRepository,bannerRepository: BannerRepository):NikeViewModel() {
    val productLiveData = MutableLiveData<List<Product>>()
    val productLiveData1 = MutableLiveData<List<Product>>()
    val progressBarLiveData = MutableLiveData<Boolean>()
    val bannerLiveData=MutableLiveData<List<Banner>>()
    init {
        progressBarLiveData.value = true
        productRepository.getLatestProducts(SORT_LATEST)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally { progressBarLiveData.value = false }
            .subscribe(object : SingleObserver<List<Product>> {
                override fun onSubscribe(d: Disposable) {
                    compositeDisposable.add(d)
                }

                override fun onSuccess(t: List<Product>) {
                    productLiveData.value = t
                }

                override fun onError(e: Throwable) {
                    TODO("Not yet implemented")
                }

            })


        productRepository.getLatestProducts(SORT_POPULAR)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doFinally { progressBarLiveData.value = false }
            .subscribe(object : SingleObserver<List<Product>> {
                override fun onSubscribe(d: Disposable) {
                    compositeDisposable.add(d)
                }

                override fun onSuccess(t: List<Product>) {
                    productLiveData1.value = t
                }

                override fun onError(e: Throwable) {
                    TODO("Not yet implemented")
                }

            })

class MainFragment:NikeFragment(),ProductListAdapter.ProductOnClickedListener {

    val mainViewModel:MainViewModel by viewModel()
    val productListAdapter:ProductListAdapter by inject()
    val productListAdapter2:ProductListAdapter by inject()
    private val TAG = "MainFragment"
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.home_fragment,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        latest_product_rv.layoutManager=
           LinearLayoutManager(requireContext(),RecyclerView.HORIZONTAL,false)

        productListAdapter.productOnClickedListener=this

        popular_product_rv.layoutManager=
            LinearLayoutManager(requireContext(),RecyclerView.HORIZONTAL,false)
        popular_product_rv.adapter=productListAdapter
        val bannerSlider=view.findViewById<ViewPager2>(R.id.bannerSliderMain)
        mainViewModel.productLiveData.observe(viewLifecycleOwner){
            latest_product_rv.adapter=productListAdapter
            productListAdapter.products=it as ArrayList<Product>

        }

        mainViewModel.productLiveData1.observe(viewLifecycleOwner){
            popular_product_rv.adapter=productListAdapter
            productListAdapter2.products=it as ArrayList<Product>

        }

【问题讨论】:

    标签: android kotlin mvvm retrofit rx-java2


    【解决方案1】:

    您在两者上都设置了相同的适配器。

    这个

            mainViewModel.productLiveData1.observe(viewLifecycleOwner){
                popular_product_rv.adapter=productListAdapter
                productListAdapter2.products=it as ArrayList<Product>
    
            }
    

    应该是

            mainViewModel.productLiveData1.observe(viewLifecycleOwner){
                popular_product_rv.adapter=productListAdapter2
                productListAdapter2.products=it as ArrayList<Product>
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2015-09-09
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 2016-03-20
      相关资源
      最近更新 更多