[英]Find view in dynamically added layouts
So I have a layout with an Add button, which when clicked adds a custom layout to the LinearLayout. 因此,我有一个带有“添加”按钮的布局,单击该按钮可将自定义布局添加到LinearLayout。
In this custom layout, I have an image, that when clicked should increase a counter (displayed in a textView next to the image). 在此自定义布局中,我有一个图像,单击该图像应增加一个计数器(显示在图像旁边的textView中)。
The problem I am currently having is, that when I have more than one custom layout added, and I click the image it always increases the count of the textView right on top and not the textView next to the image clicked. 我当前遇到的问题是,当我添加了多个自定义布局时,单击图像总是会增加textView的计数,该计数直接位于顶部,而不是单击的图像旁边的textView。
How would I find the correct textView to change? 我如何找到要更改的正确textView?
I am fairly new to the android dev world, so if I am doing anything wrong please add that too. 我对android dev世界还很陌生,所以如果我做错了什么,也请添加。
Regards 问候
Set OnClickListener
to each ImageView
when you're inflating your custom layout. 在
OnClickListener
自定义布局时,将OnClickListener
设置为每个ImageView
。 In this listener refer to a final TextView
variable which repesents a TextView
exactly from the currently created layout. 在此侦听器中,请参考最终的
TextView
变量,该变量完全从当前创建的布局中表示TextView
。 Something like this: 像这样:
private void addView() {
View customView = getLayoutInflater.inflate(resId, null);
final TextView textView = (TextView) customView.findViewById(R.id.textview);
customView.findViewById(R.id.imageview).setOnClickListener(new OnClickLitener(View v) {
textView.setText("value");
});
container.addView(customView);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.