Адаптер копирует элемент в другие элементы при нажатии на них в программе recyclerView. - PullRequest
0 голосов
/ 07 марта 2020

В моем приложении Android я использую recyclerView с группой ie адаптером . Я отправляю запрос на сервер, а затем связываю ответ с recyclerView и обновляю его группой ie.

. У меня есть 3 различных раздела: Заголовок, Пины, Основной . Таким образом, у каждого элемента есть значок булавки справа, при нажатии на который булавка будет включаться / выключаться (в главном разделе), и этот элемент будет добавляться / удаляться в pinSection наверху.

Например, , если мой основной раздел выглядит следующим образом:

A                               A                                C

B --after clicking once on C--> C --after clicking twice on C--> C

C                               C                                C

Каким-то образом notifyItemChange копирует элемент в вышеуказанные элементы в mainSection.

Код для фрагмента

            notifyMainItemChange.observeNullSafe(owner) {
                println("index " + it)
                println("this index " + (it + pinnedSection.itemCount + headerSection.itemCount).toString())
                widgetSection.notifyItemChanged(
                    it + pinnedSection.itemCount + headerSection.itemCount - 1
                )
            }

            notifyPinItemRemoved.observeNullSafe(owner) {
                pinnedSection.remove(it.first)
                pinnedSection.notifyItemRemoved(headerSection.itemCount + it.second)
                /*it.second is index of that item in list<Pins>*/
            }

            notifyPinItemInserted.observeNullSafe(owner) {
                pinnedSection.add(it)
                pinnedSection.notifyItemInserted(pinnedSection.itemCount - 1)
            }

Код для viewModel

   private fun onPinClicked(slug:String) {
                println(slug)
                mainWidgetItems.filterIsInstance<PriceRowItem>().find {
                    it.priceRowEntity.slug == slug
                }?.also {
                    val isPinned = it.priceRowEntity.isPinned
                    it.priceRowEntity.isPinned = !isPinned
                    _notifyMainItemChange.value = mainWidgetItems.indexOf(it)

                    if (!isPinned) {
                        addPinToPinnedSection(it.priceRowEntity)
                    } else {
                        removePinFromPinnedSection(it.priceRowEntity)
                    }
                }
    }

    private fun addPinToPinnedSection(entity: PriceRowEntity) {
        repo.insertPinnedItem(entity.slug)
            .subscribeOn(threads.backgroundThread.getScheduler())
            .observeOn(threads.mainThread.getScheduler())
            .subscribe({
                val newItem = PriceRowItem(entity, pinEventPublisher).apply {
                    priceRowEntity.isPinned = true
                }
                pinnedWidgetItems.add(newItem)
                _pinnedWidgetsObservable.value = pinnedWidgetItems
                //                _notifyPinItemInserted.value = newItem
            }, {
                println(it)
            })
            .addTo(compositeDisposable)
    }

    private fun removePinFromPinnedSection(entity: PriceRowEntity) {
        var item: PriceRowItem? = null
        repo.removePinnedItem(entity.slug)
            .subscribeOn(threads.backgroundThread.getScheduler())
            .doOnSubscribe {
                item = pinnedWidgetItems.filterIsInstance<PriceRowItem>().find { toRemove ->
                    toRemove.priceRowEntity.slug == entity.slug
                }
            }
            .observeOn(threads.mainThread.getScheduler())
            .subscribe({
                item?.also {
                    pinnedWidgetItems.remove(it)
                    _notifyPinItemRemoved.value = Pair(it, pinnedWidgetItems.indexOf(it))
                }
            }, {
                println(it)
            })
            .addTo(compositeDisposable)
    }

Я не уверен, что не так.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...