studio llenar example custom como arrayadapter agregar activity android android-listview android-view

llenar - listview android example



La vista setVisibility(GONE) se vuelve invisible pero aún ocupa espacio (8)

Esta es mi solución. Cree un nuevo archivo de diseño con el nombre "special.xml", copie el código en el archivo.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> </LinearLayout>

Use la condición para inflar el diseño vacío dentro de la nueva vista. No use view.setVisibility (View.GONE); .

if(true){ view=mInflater.inflate ( R.layout.special,parent, false ); }else{ view=mInflater.inflate(R.layout.normal,parent,false); // The view that you want to be visible }

Tengo una vista que efectivamente es un botón. Aquí está su diseño XML (add_new.xml)

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/buttonNew" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bText" android:onClick="addNew"/> </LinearLayout>

Cuando configuro su visibilidad como GONE

v = getActivity().getLayoutInflater().inflate(R.layout.add_new, null); v.setVisibility(View.GONE);

desaparece, pero aún ocupa espacio. Me gusta esto:

Este botón es un encabezado en ListView , que está definido por este xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/porno" > <ImageView android:id="@+id/icon" android:layout_width="30dp" android:layout_height="40dp" android:layout_marginLeft="4dp" android:layout_marginRight="10dp" android:layout_marginTop="4dp" android:src="@drawable/ic_launcher"> </ImageView> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:textSize="20dp" > </TextView> </LinearLayout>

Y no quiero que ocupe un elemento de lista adicional cuando su visibilidad está configurada en GONE. Como se indica en la documentación.

GONE: esta vista es invisible y no requiere espacio para el diseño.

¿Alguna idea sobre cómo hacerlo NO ocupa espacio?

Gracias, Dennis xx

PD: Mi vista de lista está dentro de un FoldedFragment ListFragment y aquí está el xml de MainActivity donde se presenta FoldersFragment

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment android:id="@+id/foldersFragment" android:layout_width="200dip" android:layout_height="match_parent" class="com.example.fragments.FoldersFragment" > </fragment> <fragment android:id="@+id/detailFragment" android:layout_width="match_parent" android:layout_height="match_parent" class="com.example.fragments.DetailFragment" > </fragment> </LinearLayout>


Este es un error de Android en mi opinión, solo solucionamos este problema al hacer esto:

<FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/layout_to_hide" android:layout_width="match_parent" android:layout_height="wrap_content"> //Put here your views </LinearLayout> </FrameLayout>

Simplemente oculte LinearLayout con la ID LAYOUT_TO_HIDE con Visible.GONE y luego la raíz FrameLayout colapsará su altura proporcionándole un encabezado "oculto" con espacios en blanco.


Lo que puede hacer es establecer una declaración if, siempre que la condición para configurar la visibilidad sea "GONE", establecer la vista como contenido wrap y su espacio será libre, lo hice y funcionó para una barra de búsqueda


No creo que esto sea un error. Solo use GONE para hacer desaparecer el espacio vacío. CSS hace lo mismo. Intenta display: block frente a visibility: hidden .


Si esto todavía se necesita, hay una gran manera de manejarlo:

parentView.removeView(chileView);

aquí un ejemplo:

final WhatEverLayout parentView, childView; parentView = (WhatEverLayout)findViewById(R.id.parentView_xml); childView =(WhatEverLayout)findViewById(R.id.childView_xml); parentView.removeView(childView);


Todas las respuestas en este hilo sugieren una nueva vista de contenedor, que tiene un costo. La forma correcta de ocultar una vista por completo es establecer los márgenes en 0 mientras configura la visibilidad en GONE . En este ejemplo de código, cardView es la vista que trato de ocultar. El padre directo de cardView es RecyclerView, por eso estamos usando RecyclerView.LayoutParams: recuerde reemplazar con los parámetros de diseño correctos.

if (cardView.getVisibility() != GONE) { cardView.setVisibility(GONE); RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) cardView.getLayoutParams(); layoutParams.setMargins(0, 0, 0, 0); cardView.setLayoutParams(layoutParams); }


establezca layout_width y layout_height en 0 donde desee ocultar el elemento, haciendo esto

//if item''s parent layout is LinearLayout, change according to your item xml holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(0,0));


header_layout.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/mHeaderView" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/mNoHelpLayout" android:layout_width="match_parent" android:layout_height="176dip" android:background="#ffffffff" android:gravity="center" /> </LinearLayout> </LinearLayout>

código java:

LayoutInflater mInflater =(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View outerView = mInflater.inflate(R.layout.header_layout, null); mHeaderView = (LinearLayout) outerView.findViewById(R.id.mHeaderView);

utilice mHeaderView.setVisiable () para controlar visiable no outerView.setVisiable (). esto funciona para mi.