如何在Recyclerview中触发其他卡片视图元素,而不是单击?

发布于 2025-01-28 12:51:28 字数 837 浏览 6 评论 0原文

我使用RecyClerview在适配器中有一种方法,我基本上显示了cardViews的一些元素。

如何在其他元素上触发方法,

cardView.setOnClickListener {
// MY action on specific} cardView element

例如,当我单击第三个元素时,我通常会在其中使用,<我的操作>发生在第二个元素中? 在CardView内部,我可以使用:

statusButtons.toggleVisibility(!statusButtons.isVisible)

但是只有在使用方法触发实际元素时,它才能

cardView.setOnClickListener { }

找到类似的东西 访问Recyclerview中的所有卡片视图,单击特定的卡片浏览量

但它与我的案例并不直接相关,我需要在特定ID上采取措施

statusButtons.toggleVisibility(!statusButtons.isVisible) where I not have argument to change

I have a method in adapter using recyclerview I basically display some elements of cardViews.

How to trigger method on other element where usually I use

cardView.setOnClickListener {
// MY action on specific} cardView element

for example when I click third element, <MY ACTION> happens in second element?
Inside cardView I can i.e use:

statusButtons.toggleVisibility(!statusButtons.isVisible)

but it will only work when I trigger actual element using method

cardView.setOnClickListener { }

I have found something similiar
How to access all cardviews in a recyclerview, on item click of a particular cardview

but it is not directly related to my case, I need to make action on specific ID like

statusButtons.toggleVisibility(!statusButtons.isVisible) where I not have argument to change

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开浅夏 2025-02-04 12:51:28

您可以使用recyclerview.findviewholderforlayoutposition(position)函数在给定位置获取view> view> view holder,并从活动/fragment上调用该视图持有人。
该函数返回null如果没有view> view holder在给定位置

class YourAdapter(
    onCardClick: (Int) -> Unit
) {

    ...

    inner class YourViewHolder {
        fun bind() {
            cardView.setOnClickListener {
                onCardClick(adapterPosition)
            }
        }
      
        fun operateOnCard() {
            statusButtons.toggleVisibility(!statusButtons.isVisible)
        }
    }
}

class YourActivity {
    
    ...

    val adapter = YourAdapter { position ->
        val viewHolder = recyclerView.findViewHolderForLayoutPosition(position - 1) // Or whatever position you want
        (viewHolder as? YourViewHolder)?.operateOnCard()
    }
}

编辑:如果您需要item.id.id:在选择视图持有人的活动中,您也可以从lambda传递该项目。

class YourAdapter(
    onCardClick: (Int, YourItem) -> Unit
) {
  ...
  onCardClick(adapterPosition, item)
  ...
}
class YourActivity {
    ...
    val adapter = YourAdapter { position, item ->
        // Here you have access to item as well
    }

您可以根据需要从适配器发送到活动的任何信息来更新oncardClick lambda。另外,如果要将ID发送到操作函数,也可以做到这一点。

You can use the RecyclerView.findViewHolderForLayoutPosition(position) function to get the ViewHolder at given position and call functions on that ViewHolder from the activity/fragment.
The function returns null if no ViewHolder exists at given position

class YourAdapter(
    onCardClick: (Int) -> Unit
) {

    ...

    inner class YourViewHolder {
        fun bind() {
            cardView.setOnClickListener {
                onCardClick(adapterPosition)
            }
        }
      
        fun operateOnCard() {
            statusButtons.toggleVisibility(!statusButtons.isVisible)
        }
    }
}

class YourActivity {
    
    ...

    val adapter = YourAdapter { position ->
        val viewHolder = recyclerView.findViewHolderForLayoutPosition(position - 1) // Or whatever position you want
        (viewHolder as? YourViewHolder)?.operateOnCard()
    }
}

Edit: If you need item.id in the activity for choosing the ViewHolder, you can pass the item as well from the lambda.

class YourAdapter(
    onCardClick: (Int, YourItem) -> Unit
) {
  ...
  onCardClick(adapterPosition, item)
  ...
}
class YourActivity {
    ...
    val adapter = YourAdapter { position, item ->
        // Here you have access to item as well
    }

You can update the onCardClick lambda depending upon whatever info you need to send from adapter to activity. Also, if you want to send the id to the operateOnCard function, you can also do that.

挽你眉间 2025-02-04 12:51:28

解决方案:
在适配器中

// define flowState
(...)
 val _stateFlow = MutableStateFlow(-1)
 val stateFlow = _stateFlow.asStateFlow()

// after every change of element
(...)
                CoroutineScope(Dispatchers.Main).launch {
                    stateFlow.collect { id ->
                        statusButtons.toggleVisibility(item.id == id)
                    }
                }

Solution:
in Adapter

// define flowState
(...)
 val _stateFlow = MutableStateFlow(-1)
 val stateFlow = _stateFlow.asStateFlow()

// after every change of element
(...)
                CoroutineScope(Dispatchers.Main).launch {
                    stateFlow.collect { id ->
                        statusButtons.toggleVisibility(item.id == id)
                    }
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文