1

我正在实现一个应用程序,您可以在其中选择左侧的不同选项并将它们添加到右侧的 main_fragment。情况如下:

使用此代码一切正常:

  public View getGroupView(int i, boolean b, View view){

    TextView textview = new TextView(MyFragment.this.getActivity());
    textview.setText("smth");

  return textview;}

但我需要在我的文本视图旁边有一个复选框。现在,有几种方法可以做到这一点,但不幸的是,没有一种方法可以在点击时提供相同的结果:展开!

这是在 LinearLayout 中带有 TextView 和 CheckBox 的简单 XML 元素的第一种可能性:

LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
    view = inflater.inflate(R.layout.element, null);
    TextView textview = ((TextView) view.findViewById(R.id.textview01));
    textview.setText("smth");

    CheckBox cb = ((CheckBox) view.findViewById(R.id.checkbox01));
return view;

或以编程方式添加视图:

LinearLayout ln = new LinearLayout(MyFragment.this.getActivity());
TextView textview = new Textview(MyFragment.this.getActivity());
textview.setText("smth");
CheckBox cb = new Checkbox(MyFragment.this.getActivity());
ln.addView(textview);
ln.addView(cb);

return ln;

请注意,我已缩短代码以使其更易于阅读。谢谢你的建议!

4

1 回答 1

1

默认情况下,复选框会捕获其父布局的所有焦点事件。因此,要使布局处理单击事件(包括展开),您需要将复选框可聚焦性设置为 false:
XML 布局:

android:focusable="false"

来自 JAVA 代码:

checkBox.setFocuseble(false);
于 2013-10-22T12:41:07.233 回答