我工作在一个电子商务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>
}