试图通过EventBus影响RecyclerView项目 [英] Trying to impact RecyclerView items via EventBus

查看:130
本文介绍了试图通过EventBus影响RecyclerView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从回收项目视图类发布EventBus事件,并在同一类中订阅该事件,以便所有回收项目都可以捕获该事件.

I'm trying to post an EventBus event from an recycler item view class and subscribe to it in the same class so that the event is grabbed by ALL the recycler items.

现在更详细:

我有一个RecyclerView,其中每个项目(FriendListItem.kt)都有一个上下文菜单.一次只能显示一个上下文菜单.这意味着我需要关闭另一个项目的上下文菜单(如果可见).

I have a RecyclerView where each item (FriendListItem.kt) has a context menu. Only one context menu should be shown at a time. That means that I need to close another item's context menu if its visible.

我选择使用我们已经在我们的应用程序中广泛使用的org.greenrobot.eventbus.在项目类中,当要显示菜单时,我发布一个事件,并将其捕获到同一类中.我以为这样所有的项目都会响应并关闭它们的菜单(可能是可见的),但是事件Subscriber却什么都没有.

I chose to use the org.greenrobot.eventbuswhich we already widely used in our app. In the item class, when the menu is to be shown, I post an event, and the grab it in the same class. I thought that this way all the items will respond and dismiss their (possibly visible) menus, but the event Subscriber doesn't grab anything.

我只添加包含的片段按需要注册和注销EventBus,因为它可以在另一个事件中正常工作.

I'll just add that the containing fragment registers and unregisters the EventBus as it should because it works fine in another event.

这里的问题可能是该事件应该在回收项目视图中获取,该视图体现在多个​​项目中.可以吗?

The problem here might be that the event should be grabbed in a recycler item view, which manifests in multiple items. Could that be it?

以下是项目:

package com.myapp.android.common.social.friends

import android.content.Context
import android.text.Spanned
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.LinearLayout
import com.myapp.android.common.R
import com.myapp.android.common.database.room.entities.User
import com.myapp.android.common.generic.coachmark.BubbleCoachMark
import com.myapp.android.common.image.ImageSize
import com.myapp.android.common.social.friends.events.FriendsListResetOtherMenus
import kotlinx.android.synthetic.main.friends_list_item.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class FriendsListItem(context: Context) : LinearLayout(context) {

    private var user: User? = null
    private var menu: BubbleCoachMark? = null

    init {
        View.inflate(context, R.layout.friends_list_item, this)
    }

    fun updateView(user: User, sp: Spanned?) {
        this.user = user

        // Avatar
        if (user.pictureUrl != "") {
            setUserPicture(user.pictureUrl + "", user.isPremium)
        }

        // Premium
        when {
            user.isPremium -> friendPremium.visibility = View.VISIBLE
            else -> friendPremium.visibility = View.GONE
        }

        // Name
        when (sp) {
            null -> friendName!!.text = user.name
            else -> friendName!!.text = sp
        }

        // Friend Status
        invite.visibility = View.GONE
        action.visibility = View.VISIBLE

        when (user.status) {
            User.Status.friend -> {
                action.setImageResource(R.drawable.ic_friend_options)
                action.setOnClickListener(OnClickListener {
                    EventBus.getDefault().post(FriendsListResetOtherMenus())
                    when (menu) {
                        null -> {
                            menu = BubbleCoachMark(BubbleCoachMark.BubbleCoachMarkBuilder(context, action, FriendsListItemMenu(context)).setShowBelowAnchor(true))
                            menu!!.show()
                        }
                        else -> {
                            if (!menu!!.isShowing)
                                menu!!.show()
                        }
                    }
                })
            }
            User.Status.pending -> {
                action.setImageResource(R.drawable.ic_friend_requested)
            }
            User.Status.nofriend -> {
                action.setImageResource(R.drawable.ic_friend_add)
            }
            else -> {
                invite.visibility = View.VISIBLE
                action.visibility = View.GONE
            }
        }
    }

    fun setUserPicture(pictureUrl: String, isPremium: Boolean) {
        synchronized(this) {
            friendAvatar!!.scaleType = ImageView.ScaleType.CENTER_CROP
            friendAvatar!!.setUserPicture(pictureUrl, isPremium, R.drawable.profile_silhuette_new, ImageSize.thumbnail)
        }
    }

    // The method here is not grabbing the event
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: FriendsListResetOtherMenus) {
        if (menu != null && !menu!!.isShowing)
            menu!!.dismiss()
    }
}

更新的解决方案:

init子句中本地注册EventBus:

Registered the EventBus locally in the init clause:

...
init {
    View.inflate(context, R.layout.friends_list_item, this)

    EventBus.getDefault().register(this@FriendsListItem)
}
...

onDetachedFromWindow事件中取消注册EventBus:

Unregistered the EventBus in the onDetachedFromWindow event:

...
override fun onDetachedFromWindow() {
    super.onDetachedFromWindow()

    EventBus.getDefault().unregister(this@FriendsListItem)
}
...

推荐答案

尝试一下. Kotlin Int发生在我身上,这可能会解决您的问题

Try this. It happened with me for kotlin Int, it might resolve your issue

@Subscribe(threadMode = ThreadMode.MAIN)
fun onEventMainThread(event: FriendsListResetOtherMenus?) {
    if (menu != null && !menu!!.isShowing)
        menu!!.dismiss()
}

这篇关于试图通过EventBus影响RecyclerView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆