我正在实现一个应用程序,您可以在其中选择左侧的不同选项并将它们添加到右侧的 main_fragment。情况如下:
使用这段代码一切正常:
public View getGroupView(int i, boolean b, View view){
TextView textview = new TextView(MyFragment.this.getActivity());
textview.setText("smth");
return textview;}
但我的 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;
或以编程方式添加 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;
请注意,我已经缩短了代码以使其更易于阅读。 谢谢你的建议!
最佳答案
默认情况下,复选框会捕获其父布局的所有焦点事件。因此,要使布局处理点击事件(包括展开),您需要将复选框的可聚焦性设置为 false:
XML 布局:
android:focusable="false"
来自 JAVA 代码:
checkBox.setFocuseble(false);
关于Android ExpandableListView 不会在返回 LinearLayout 或 View 时展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19517189/