同时 inflate 两个 Layout

Inflate two Layouts at the same time

我想在一个 ListView 中同时膨胀两个布局。我实际上做到了,但这并不是我想要的。

这是我的适配器中的 getView 代码:

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
//Create InstalacionEnLista object
Instalacion inst = items.get(position);
int id = inst.getIdCategoria();
if(id == 1){
     if (convertView == null || convertView.findViewById(R.id.custom_todaslasinstalaciones_categoria_header_titulo)==null)
        {
        LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inf.inflate(R.layout.custom_todaslasinstalaciones_header_categoria, null);
        TextView nombreCategoria = (TextView)                                               v.findViewById(R.id.custom_todaslasinstalaciones_categoria_header_titulo);
        nombreCategoria.setText("Esto es una categoria");
        }

}else{
    LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inf.inflate(R.layout.custom_todaslasinstalaciones_conflecha, null);
    TextView nombre = (TextView)v.findViewById(R.id.custom_todaslasinstalaciones_conflecha_tvnombreinstalacion);
    nombre.setText(inst.getNombre());
}
return v;

}

如果 id 为 1,我希望两个布局都膨胀,并包含项目信息。 如果id不是=1,我只希望一个Layout膨胀。

此致

看来你的适配器没问题。您要添加列表中的所有行吗? 我的意思是,你应该怎么做才能拥有它 运行:

0- 创建 xml 文件(listView 和行)

1- 创建行 class(在您的情况下:Instalacion)

2- 创建适配器(使用 getView 等)

3- 在某处创建 'Instalacion' 对象列表(需要适配器的对象)

4- 将元素添加到此列表 (InstalacionList)

5-初始化列表(设置de适配器等)

希望对您有所帮助。如果需要,我可以尝试 post 一些代码。

我这样解决了我的问题:

        //Category layout inflate            
    if (convertView == null || convertView.findViewById(R.id.category)==null)
            {
            LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inf.inflate(R.layout.custom_todaslasinstalaciones_header_categoria, null);
            //disable click over category
            v.setOnClickListener(null);
            v.setOnLongClickListener(null);
            v.setLongClickable(false);

            }
        TextView nombreCategoria = (TextView) v.findViewById(R.id.custom_todaslasinstalaciones_categoria_header_titulo);
        nombreCategoria.setText(nombre);




    //simple item layout inflate
    else{

            LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inf.inflate(R.layout.custom_todaslasinstalaciones_conflecha, null);
            TextView tvNombre = (TextView) v.findViewById(R.id.custom_todaslasinstalaciones_conflecha_tvnombreinstalacion);
            tvNombre.setText(nombre);


    }
return v;

谢谢。