[英]How to evaluate a variable (changing value) in a Fragment from a RecyclerView Adapter (Kotlin) for conditional processing?
I'm new to all of this and was making good progress but I have hit a brick wall with working out how to do the next thing.我对这一切都很陌生,并且取得了良好的进展,但我在解决如何做接下来的事情时遇到了障碍。 I am using Kotlin and have a Fragment with an associated Recyclerview Adapter.我正在使用 Kotlin 并且有一个带有相关 Recyclerview 适配器的片段。 I would like to set OnClick (On or Off) against items in a row depending upon a value in the Fragment which could change at any time.我想根据 Fragment 中可能随时更改的值对一行中的项目设置 OnClick(开或关)。
My adapter works fine to show and update the array of data and also to implement OnClick.我的适配器可以很好地显示和更新数据数组,也可以实现 OnClick。
I have tried sending a data element via a constructor which changed in the fragment but always showed as the initial setting in the adapter.我尝试通过在片段中更改但始终显示为适配器中的初始设置的构造函数发送数据元素。 The same with trying to call a method.尝试调用方法也是如此。
Many other questions touch on the issue but only show snippets of code, and it seems that I'm not advanced enough to get them working successfully in my code.许多其他问题涉及该问题,但仅显示代码片段,而且似乎我不够先进,无法让它们在我的代码中成功运行。
Could anyone please provide a pointer to a working set of Kotlin code that includes parsing a variable from fragment to adapter - perhaps in Git or a tutorial.谁能提供一个指向 Kotlin 代码工作集的指针,其中包括将变量从片段解析到适配器 - 可能在 Git 或教程中。 I'm sure that if I can study a working program I can move forward.我敢肯定,如果我能学习一个工作计划,我就能继续前进。 Thank you.谢谢你。
It would have been better if you had included your Adapter and Fragment code in the question, that would have helped us in understanding how you have setup everything and what data model are you passing to adapter.如果您在问题中包含您的适配器和片段代码会更好,这将有助于我们了解您如何设置所有内容以及您将什么数据模型传递给适配器。
But looking at your question, one solution that comes to my mind is to add an enabled
boolean in your data model that is displayed in the ViewHolder.但是看看你的问题,我想到的一个解决方案是在你的数据模型中添加一个enabled
的布尔值,它显示在 ViewHolder 中。 Using this you can set view.clickable = model.enabled
.使用它你可以设置view.clickable = model.enabled
。 Now whenever your "value in the Fragment" changes you can update this list and let the adapter rebind items.现在,每当您的“片段中的值”更改时,您都可以更新此列表并让适配器重新绑定项目。
Note that the above solution is when you want to selectively enable/disable clicks on individual items.请注意,上述解决方案是当您想要选择性地启用/禁用对单个项目的点击时。 If you want to do this for all items at once, it's better to create a variable in adapter that you can change from the Fragment, and inside the clickListener you can check the value of that adapter variable.如果您想一次对所有项目执行此操作,最好在适配器中创建一个可以从 Fragment 更改的变量,然后在 clickListener 中检查该适配器变量的值。 If it's false, just return out of the click listener.如果它是假的,就从点击监听器中返回。 Something like,就像是,
view.setOnClickListener {
if(adapterValue) {
// handle Click
}
}
If this approach doesn't help, I would ask you to add more context in your question and show what you have done so far.如果这种方法没有帮助,我会要求您在问题中添加更多上下文并展示您到目前为止所做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.