此 Activity 中的 findViewWithTag 返回 null,但 CustomExpandableListAdapter 中的日志输出正确。我在这里做错了什么?
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
//normal stuff
public View getGroupView(int groupPosition, boolean isLastChild,
View view, ViewGroup parent) {
CustomObject group = (CustomObject) getGroup(groupPosition);
//normal stuff
LinearLayout toggle = (LinearLayout) view.findViewById(R.id.toggle);
//group.getId() returns an int
toggle.setTag("toggle" + group.getId());
Log.i("BBBBBtoggleadapterBBBBBB", toggle.getTag().toString());
return view;
}
}
public class MainActivity extends BaseListActivity {
//normal stuff
//this is an xml onClick
public void toggleView(View view) {
String groupId = view.getTag().toString();
//LinearLayout toggle = (LinearLayout) view.findViewWithTag("toggle " + groupId);
Log.i("BBBBBBBBBtoggleactivityBBBBBBBBBB", "toggle" + groupId);
/*if (toggle == null) {
Toast.makeText(getBaseContext(), "null", Toast.LENGTH_LONG).show();
}*/
}
}
编辑:更新代码,在 Activity 和适配器中记录“toggle”+ id,它们是相同的
最佳答案
我明白了。 findViewWithTag 查找匹配的子标签。我试图识别的 LinearLayout 不是 onClick 触发的toggleView 按钮的子级。因此,我需要向上(在本例中为 2 层)才能找到它。
View parent = (View) view.getParent().getParent();
LinearLayout toggle = (LinearLayout) parent.findViewWithTag("toggle" + groupId);
关于java - Android - findViewWtihTag 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20550928/