¿Cómo puedo agregar un ImageView como un encabezado ListView?

¿Puede alguien ayudarme a averiguar cómo agregar un ImageView como un encabezado a un ListView?

Mi código está aquí:

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View snapshot = inflater.inflate(R.layout.map_snapshot, null); ListView channelsList = (ListView) findViewById(R.id.channelsList); channelsList.addHeaderView(snapshot); 

Por ahora no muestra nada.

Intente inflar su vista de encabezado pasando el parámetro padre (ListView) como parámetro:

 ListView channelsList = (ListView) findViewById(R.id.channelsList); View snapshot = inflater.inflate(R.layout.map_snapshot, channelList, false); 

A continuación, sólo tiene que añadirlo a ListView:

 channelsList.addHeaderView(snapshot); 

Tratar:

 ListView channelsList = (ListView) findViewById(R.id.channelsList); LayoutInflater inflater = getLayoutInflater(); ViewGroup header = (ViewGroup)inflater.inflate(R.layout.map_snapshot, channelsList , false); channelsList .addHeaderView(header, null, false); 

En el diseño map_snapshot, debes tener el diseño de los padres como aquí he tomado Linear Layout también tomar eso.

  ListView channelsList = (ListView) findViewById(R.id.channelsList); LinearLayout headerView = (LinearLayout) getLayoutInflater().inflate(R.layout.map_snapshot, null); listView.addHeaderView(headerView); 

Si desea agregar vistas en el diseño:

 TextView mUsername=(TextView) headerView.findViewById(R.id.user_name); 

Puede utilizar CommonsWare MergeAdapter o SectionedListAdapter . Con esta biblioteca puede utilizar cualquier View como encabezado en su Listview . Vea esta pregunta y respuesta aceptada para más información.

  • Cómo implementar Android listview apertura gesto
  • Cómo obtener una Java.Lang.NullPointerException en Android Fragment
  • ¿Cómo implementar correctamente un listview personalizado con imágenes usando la biblioteca de Picasso?
  • Cómo quitar el borde en un ListView?
  • Mejor manera de mostrar datos de flujo en la vista de lista de Android
  • Reiniciar el contador después de hacer clic en el botón
  • Cómo mostrar el tamaño del archivo en vista de lista con nombre de archivo
  • Android: FastScrolling SectionIndexer getSections () se llama sólo una vez
  • Android: notifyDataSetChanged () no actualizar listview después de cambiar la orientación
  • Cómo configurar los datos de ArrayList en ListView?
  • ¿Cómo puedo probar ListView con Robolectric 2.2? Dice que android.widget.ListView todavía no está implementado
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.