我想在我的android应用程序中创建一个可扩展的无线电组列表。列表的每个元素应该包含(当打开时)几个单独的无线电组。到目前为止,我只对每个孩子进行了一个放射组的测试,但我已经遇到了一个我似乎无法独自解决的问题。
当我展开我的可扩展列表的父级时,它似乎工作正常。radiogroup(目前是父元素的唯一子元素)的单选按钮按其应有的方式显示。但是,当展开可展开列表的另一个父组时,单选按钮将被添加到另一个子组的现有按钮中。这意味着:一旦我展开了几个子组,每一个被展开的子组都包含了大量的单选按钮,占据了整个屏幕。简单地说,展开parent 1时,得到选项
- 对 没有
- 或者
展开第二个父元素时,得到单选项
- 对 没有
- 或者
- 对 没有也许可能
…它们也被添加到父1内部的单选按钮组。现在我有两个大的扩展列表元素。当我展开另一个组时,这些选项也会被添加到所有子视图中,等等,等等…
因此,遵循简单的逻辑,似乎所有的单选按钮都没有放入一个新组(每次我展开子列表时新创建),而是将它们添加到相同的,已经存在的组。
我已经能够将问题隔离到我的ExpandableListAdapter类中的getChildView函数。这是导致上述输出的代码:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
ViewGroup parent) {
ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.child_layout, null);
}
ArrayList<HashMap<String,String>> inputDetailsList = child.getInputfields();
//RadioGroup rg = new RadioGroup(view.getContext());
RadioGroup rg = (RadioGroup) view.findViewById(R.id.radiogroup);
int has_radiogroup = 0;
for (int i = 0; i <inputDetailsList.size(); i++){
if (inputDetailsList.get(i).get("type").equals("radio")){
has_radiogroup = 1;
RadioButton rb = new RadioButton(rg.getContext());
rb.setText(inputDetailsList.get(i).get("textcontent"));
rg.addView(rb);
}
}
return view;
}
下面是用作子布局的xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:background="@color/ExpandChildBackground"
android:orientation="vertical" >
<TextView
android:id="@+id/tvChild"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/Black"
android:textSize="17dip" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</RadioGroup>
</LinearLayout>
我怀疑我的错误可能是这一行:
RadioGroup rg = (RadioGroup) view.findViewById(R.id.radiogroup);
它查找我在膨胀的子布局(R.layout.child_layout)中创建的无线电组。但是它不会为每个展开的子视图使用一个新的视图,而是使用一个现有的视图来应用按钮。换句话说,似乎无线电组永远不会得到它"完成"的标志,并且/或者子视图永远不会得到必须使用新无线电组代替旧无线电组的标志。
我所尝试的是使用以下行为每个子视图创建一个新的RadioGroup(您可以看到它是上面代码中的注释行),而不是使用我在布局中定义的RadioGroup。创建一个新的radiogroup,看起来像这样:
RadioGroup rg = new RadioGroup(view.getContext());
然而,这不起作用,它不显示任何无线电组,更不用说按钮了。我还尝试过从布局xml文件中删除radiogroup元素(因为它没有被使用)。还是没有运气。对每个元素使用简单的无组织的textview似乎可以正常工作,因此这也指出了我的无线电组实现的问题。
老实说,我现在已经没有主意了。我可能会得到错误的上下文,但我不知道该使用什么上下文。如果有人能给我指出正确的方向,我会非常感激。如果有人感兴趣,我已经能够自己解决这个问题。我只是删除了if条件[if (view == null)
],所以新代码看起来像这样:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
ViewGroup parent) {
ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition);
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.child_layout, null);
ArrayList<HashMap<String,String>> inputDetailsList = child.getInputfields();
RadioGroup rg = (RadioGroup) view.findViewById(R.id.radiogroup);
int has_radiogroup = 0;
for (int i = 0; i <inputDetailsList.size(); i++){
if (inputDetailsList.get(i).get("type").equals("radio")){
System.out.println("type: "+inputDetailsList.get(i).get("type"));
has_radiogroup = 1;
RadioButton rb = new RadioButton(rg.getContext());
rb.setText(inputDetailsList.get(i).get("textcontent"));
System.out.println("textcontent: "+inputDetailsList.get(i).get("textcontent"));
rb.setId(Integer.parseInt(inputDetailsList.get(i).get("value")));
if (has_radiogroup == 1){
rg.addView(rb);
}
}
}
return view;
}
现在说得通了。每次生成新的子视图(即展开组)时,都会调用LayoutInflater
。在此之前,它只是使用用radiobuttons
填充的现有视图(视图不是null),并将新的radiobuttons
添加到现有视图,导致我的列表淹没屏幕。
无论如何,还是要感谢所有努力解决这个问题的人。
这对我来说很有效。
android:focusable="false"