android - 在复选框事件上重绘 expandableListView

标签 android expandablelistview

我有一个可扩展 ListView ,其中包含一个按组显示的复选框和一个按子项显示的复选框。 当一个组被选中/取消选中时,所有子项都被选中/取消选中。我没有问题保持此复选框的状态,但我想强制 expandableListView 重绘以立即显示所有子项复选框已选中或未选中。 我搜索了几个小时,但没有找到解决方案。 有谁能向我解释我们如何解决这个问题,我会很高兴。

这是我在 expandableListAdapter 中管理复选框更改的代码:

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    GroupViewHolder gholder;
    final TypeVehicule type=(TypeVehicule) getGroup(groupPosition);
    if (convertView==null)
    {
        gholder=new GroupViewHolder();
        convertView = inflater.inflate(R.layout.listevehicule_type,null);
        gholder.textViewGroup=(TextView)convertView.findViewById(R.id.LV_textType);
        gholder.checkType=(CheckBox)convertView.findViewById(R.id.LV_CheckType);
        gholder.checkType.setTag(groupPosition);        
        gholder.imgType=(ImageView)convertView.findViewById(R.id.LV_ImageType);
        convertView.setTag(gholder);
    }
    else
    {
        gholder = (GroupViewHolder)convertView.getTag();
    }
    gholder.textViewGroup.setText(type.getType());
        if (type.getColor()==-1)
        { 
            int color;
            Random rnd = new Random(); 
            color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
            type.setColor(color);       
        }   
        gholder.imgType.setBackgroundColor(type.getColor());
        gholder.checkType.setChecked(type.isIschecked());

        gholder.checkType.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) 
            {                   
                type.setIschecked(((CheckBox) v).isChecked());

                    for (Vehicule _v : type.getListeVehicules())
                    {
                        _v.setIschecked(type.isIschecked());

                    }
            }       

        });

        gholder.checkType.setChecked(type.isIschecked());
      //
    return convertView;
}

和 onCreate 的代码:

protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(net.ornicar.ornimobile.R.layout.listevehicule);
    listView_Vehic = (ExpandableListView)findViewById(R.id.ListeVehicules);     
    // ici pour l'instant on rempli en dur en attendant l'implementation de la couche metier..      
    for (int i=1; i<10;i++)
    {
        TypeVehicule type = new TypeVehicule("Type " + i);

        ArrayList<Vehicule> oListVeh = new ArrayList<Vehicule>();

        for (int j=0;j<10;j++)
        {
            oListVeh.add(new Vehicule(type, "Veh N°"+j+"-"+i));
        }
        type.setListVehicule(oListVeh);
        oListType .add(type);           
    }
    ListeVehiculesAdapter adapter = new ListeVehiculesAdapter(this, oListType,true);
    listView_Vehic.setAdapter(adapter);
    this.registerForContextMenu(listView_Vehic);
    listView_Vehic.setOnChildClickListener(new  OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
            Toast.makeText(getApplicationContext(), oListType.get(groupPosition).getListeVehicules().get(childPosition).getNom()+" Clicked!!", 4000).show();    
            return true;
        }
    });

}

最佳答案

要强制任何 (Expandable)ListView 重绘,您可以调用 notifyDataSetChanged在 View 使用的适配器上。这将强制重绘屏幕上可见的任何内容。这意味着您的适配器必须扩展 BaseAdapterBaseExpandableListAdapter .

关于android - 在复选框事件上重绘 expandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10830241/

相关文章:

java - 为什么 TextInputEditText 会产生此错误?

android - 实现 OnLongClickListener 时 ExpandableListView 不扩展

android - 需要 Android 2.3.3 上的 expandableListView 的示例代码

android - ExpandableListView 不起作用

android - Multichild可扩展列表android

android - 知道应用程序是否正在运行 alpha、beta 或 release

java - 重新启动 Chromecast

android - 更改 fragment 中的操作栏菜单

android - 无法将数据插入到 Android 中的 SQLite 数据库中

java - ExpandableListView parent 中的不同开关 Intent - 前一个不起作用