¿Es posible desactivar scroll en un listView?

Agrego elementos dinámicamente a un ListView y quiero hacer que todos los elementos siempre sean visibles, sin desplazamiento.

Código de diseño:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/recipe_inside" android:orientation="vertical" > <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="16dp" android:src="@drawable/head_cella" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="41dp" android:background="@drawable/body_cella" android:gravity="center_horizontal" android:paddingTop="5dp" android:text="Remember to save before leaving this page or inserted/modified data will be lost." android:textColor="#000000" android:textSize="13dp" /> <ImageView android:id="@+id/ImageView01" android:layout_width="match_parent" android:layout_height="16dp" android:layout_marginTop="30dp" android:src="@drawable/head_cella" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="35dp" android:background="@drawable/body_cella" android:gravity="center_horizontal" android:paddingTop="10dp" android:text="Category" android:textColor="#000000" android:textSize="15dp" /> <Spinner android:id="@+id/category_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/ImageView02" android:layout_width="match_parent" android:layout_height="16dp" android:layout_marginTop="30dp" android:src="@drawable/head_cella" /> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="35dp" android:background="@drawable/body_cella" android:gravity="center_horizontal" android:paddingTop="10dp" android:text="Name" android:textColor="#000000" android:textSize="15dp" /> <EditText android:id="@+id/recipe_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="Recipe title" android:lines="1"/> <ImageView android:id="@+id/ImageView03" android:layout_width="match_parent" android:layout_height="16dp" android:layout_marginTop="30dp" android:src="@drawable/head_cella" /> <TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="35dp" android:background="@drawable/body_cella" android:gravity="center_horizontal" android:paddingTop="10dp" android:text="Ingredients" android:textColor="#000000" android:textSize="15dp" /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:cacheColorHint="#00000000" android:divider="#00000000" android:isScrollContainer="false" > </ListView> <Button android:id="@+id/button_ing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:background="@drawable/add_recipe_button" android:onClick="addItems"/> </LinearLayout> </ScrollView></LinearLayout> 

Antiguo código de función OnCreate ():

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_recipe_form); spinner = (Spinner)myhead.findViewById(R.id.category_spinner); Cursor cursor = mydb.getCategoriesList(); ArrayList<Category> list = new ArrayList<Category>(); list.add(new Category(0,"Choose a category")); while (cursor.moveToNext()) { list.add(new Category(cursor.getInt(0),cursor.getString(1))); } // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list); // Step 3: Tell the spinner about our adapter spinner.setAdapter(spinnerArrayAdapter); spinner.setOnItemSelectedListener(this); setListAdapter(adapter); ingredients.add((View)findViewById(R.layout.row_add_recipe)); adapter.notifyDataSetChanged(); } 

Si muevo el código relacionado para llenar mi hilandero con datos de la base de datos, siempre me devuelve una NullPointerException ya que no se encuentra en mi diseño. Así que tengo que mantenerlo aquí y la solución añadiendo addHeaderView () a mi ListView parece estar bien, pero tengo otra NullPointerException con respecto a mi diseño de la cabeza:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_recipe_form); ListView mylistview = (ListView) findViewById(android.R.id.list); LinearLayout myhead = (LinearLayout)findViewById(R.id.linear_form); mylistview.addHeaderView(myhead); spinner = (Spinner)myhead.findViewById(R.id.category_spinner); Cursor cursor = mydb.getCategoriesList(); ArrayList<Category> list = new ArrayList<Category>(); list.add(new Category(0,"Choose a category")); while (cursor.moveToNext()) { list.add(new Category(cursor.getInt(0),cursor.getString(1))); } // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list); // Step 3: Tell the spinner about our adapter spinner.setAdapter(spinnerArrayAdapter); spinner.setOnItemSelectedListener(this); setListAdapter(adapter); ingredients.add((View)findViewById(R.layout.row_add_recipe)); adapter.notifyDataSetChanged(); } 

Si te entiendo bien, sólo necesitas algunas vistas más que se pueden desplazar junto con tu listview. La mejor manera de implementarlo es llamar addHeaderView() y addFooterView() en su ListView() .

EDITAR:

Debería ir así, creo:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_main_layout); LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View header = inflater.inflate(R.layout.your_header_layout); //R.layout, not R.id! (ListView) list = findViewById(android.R.list); list.addHeaderView(header); list.setAdapter(adapter); //according to docs you should call setAdapter() after addHeaderView() //to get the spinner (Spinner) spinner = (Spinner) header.findViewById(R.id.spinner); } 

Estoy escribiendo desde memoria y no lo probé, pero creo que debería funcionar. Pruébelo cuando tenga un momento.

Una solución es hacer que la altura de ListView sea tan grande como el contenido. Puede lograr este efecto utilizando la siguiente clase en lugar de ListView:

 public class ExpandedListView extends ListView { private int old_count = 0; public ExpandedListView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { if (getCount() != old_count) { old_count = getCount(); int totalHeight = 0; int desiredWidth = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST); for (int i = 0; i < getCount(); i++) { View listItem = getAdapter().getView(i, null, this); listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = getLayoutParams(); params.height = totalHeight + (getDividerHeight() * (getCount() - 1)); setLayoutParams(params); requestLayout(); } super.onDraw(canvas); } } 

De todos modos, es una mala idea tener un ListView dentro de un ScrollView, por lo que añadiría los otros elementos en su Layout como una vista de encabezado / pie de página con los métodos addHeaderView () y addFooterView () como MichałK señaló.

Aquí tienes un ejemplo sobre cómo usar estos métodos:

https://github.com/commonsguy/cw-advandroid/tree/master/ListView/HeaderFooter

Haga que su altura de ListView mayor que su tamaño de contenido .

Diga si usted tiene 5 listitems cada uno que tiene 20sp altura fijó la altura de la opinión de lista a 100 o más grande esta manera usted puede alcanzarla

  • Obtener el elemento seleccionado de ListView enlazado con SimpleCursorAdapter
  • Android: Múltiples temporizadores en un ListView con manejador y ejecutables. 2 Problemas
  • ListView item link haga clic en el estado de la fila con el botón dentro del elemento de lista
  • Android ListView resaltar varios elementos
  • Cómo implementar anuncios nativos express en Listview con CursorAdapter
  • Iterar a través de ListView y obtener valores de EditText-Field
  • Pase el evento de movimiento a la Scrollview primaria cuando Listview en la parte superior / inferior
  • El objetivo no debe ser nulo utilizando la Biblioteca Picasso
  • ListView Creator llamado, pero no otros métodos
  • "Separadores" en ListView
  • Cómo actualizar el ListView en lista de arrastre de ListView?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.