在 ConstraintLayout 中使用 group 来监听多个视图上的点击事件 [英] Use group in ConstraintLayout to listen for click events on multiple views
问题描述
基本上我想将一个 OnClickListener 附加到 ConstraintLayout 内的多个视图.
Basically I'd like to attach a single OnClickListener to multiple views inside a ConstraintLayout.
在迁移到 ConstraintLayout 之前,我可以在一个布局中添加一个监听器的视图.现在它们与 ConstraintLayout 正下方的其他视图位于同一层.
Before migrating to the ConstraintLayout the views where inside one layout onto which I could add a listener. Now they are on the same layer with other views right under the ConstraintLayout.
我尝试将视图添加到 android.support.constraint.Group
并以编程方式向其添加 OnClickListener.
I tried adding the views to a android.support.constraint.Group
and added a OnClickListener to it programmatically.
group.setOnClickListener {
Log.d("OnClick", "groupClickListener triggered")
}
然而,从 ConstraintLayout 版本 1.1.0-beta2
However this does not seem to work as of the ConstraintLayout version 1.1.0-beta2
我做错了什么,有没有办法实现这种行为,或者我是否需要将侦听器附加到每个单个视图?
Have I done something wrong, is there a way to achieve this behaviour or do I need to attach the listener to each of the single views?
推荐答案
ConstraintLayout
中的 Group
只是 AFAIK 视图的松散关联.它不是 ViewGroup
,因此您将无法像在视图位于 ViewGroup
中时那样使用单击侦听器.
The Group
in ConstraintLayout
is just a loose association of views AFAIK. It is not a ViewGroup
, so you will not be able to use a single click listener like you did when the views were in a ViewGroup
.
作为替代方法,您可以在代码中获取属于您的 Group
成员的 id 列表,并显式设置点击侦听器.(我还没有找到关于这个功能的官方文档,但我相信它只是滞后于代码发布.) 查看关于 getReferencedIds
的文档此处.
As an alternative, you can get a list of ids that are members of your Group
in your code and explicitly set the click listener. (I have not found official documentation on this feature, but I believe that it is just lagging the code release.) See documentation on getReferencedIds
here.
Java:
Group group = findViewById(R.id.group);
int refIds[] = group.getReferencedIds();
for (int id : refIds) {
findViewById(id).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// your code here.
}
});
}
在 Kotlin 中,您可以为此构建一个扩展函数.
In Kotlin you can build an extension function for that.
科特林:
fun Group.setAllOnClickListener(listener: View.OnClickListener?) {
referencedIds.forEach { id ->
rootView.findViewById<View>(id).setOnClickListener(listener)
}
}
然后调用组上的函数:
group.setAllOnClickListener(View.OnClickListener {
// code to perform on click event
})
更新
引用的 id 在 2.0.0-beta2 中并不立即可用,尽管它们在 2.0.0-beta1 和更早版本中可用.发布"上面的代码以在布局后获取参考 ID.像这样的东西会起作用.
The referenced ids are not immediately available in 2.0.0-beta2 although they are in 2.0.0-beta1 and before. "Post" the code above to grab the reference ids after layout. Something like this will work.
class MainActivity : AppCompatActivity() {
fun Group.setAllOnClickListener(listener: View.OnClickListener?) {
referencedIds.forEach { id ->
rootView.findViewById<View>(id).setOnClickListener(listener)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Referenced ids are not available here but become available post-layout.
layout.post {
group.setAllOnClickListener(object : View.OnClickListener {
override fun onClick(v: View) {
val text = (v as Button).text
Toast.makeText(this@MainActivity, text, Toast.LENGTH_SHORT).show()
}
})
}
}
}
这应该适用于 2.0.0-beta2 之前的版本,因此您只需执行此操作而无需进行任何版本检查.
This should work for releases prior to 2.0.0-beta2, so you can just do this and not have to do any version checks.
这篇关于在 ConstraintLayout 中使用 group 来监听多个视图上的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!