显示来自服务器的不同种类的产品

2022-01-29 09:07:08 标签 androidkotlinmvvmretrofitrx-java2

我工作在一个电子商务Android应用程序使用MVVM & RXJAVA & KOIN芬兰湾的科特林和产品请求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>
        }

###你在两个上设置相同的适配器。

        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>
        }
阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码