2012-02-12 61 views
0

我通过ArrayAdapter与复选框使用ListActivity。
我想要做的是添加ListView项目动态不同的布局(仅限TextView)当checkBox被检查。 也应该在再次取消选中后消失。

在复选框添加ListView项目检查与不同的格式

CheckBox unchecked: 

+----------------------+ 
| cb -- some text -- | 
+----------------------+ 
| - new Heading - | 
+----------------------+ 
|   etc.   | 
+----------------------+ 

CheckBox checked: 

+----------------------+ 
| cb -- some text -- | 
+----------------------+ 
| --- TextView --- | 
+----------------------+ 
| --- TextView --- | 
+----------------------+ 
| - new Heading - | 
+----------------------+ 
|   etc.   | 
+----------------------+ 


什么是实现这一目标的最佳方式是什么?

回答

0

你可以做到这一点,如下所示:

final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox); 
checkbox.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     // Perform action on clicks, depending on whether it's now checked 
     if (((CheckBox) v).isChecked()) { 
      ListView lv=v.getParent(); 
      TextView tv1=new TextView(this); 
      lv.addView(tv1,1); 
      TextView tv2=new TextView(this); 
      lv.addView(tv2,2); 
     } else { 
      ListView lv=v.getParent(); 
      TextView tv1=lv.getChildAt(1); 
      TextView tv2=lv.getChildAt(2); 

      if(tv1!=null && tv2!=null) lv.removeViews(1, 2); 
     } 
    } 
}); 

另一个simplar方法是定义从一开始的TextViews,让复选框设置自己的知名度吧。

+0

谢谢您的快速回答!我已经找到了我一直在寻找的偏好:依赖。 – user1163881 2012-04-07 14:56:32