Cómo hacer que android listview desplazable?

Tengo dos listas, pero no se desplazan. ¿Cómo puedo corregir esto?

Aquí está mi layout.xml

  <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/backgrund" > <!-- Header Starts --> <LinearLayout android:id="@+id/header" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_alignParentTop="true" android:background="@layout/header" > </LinearLayout> <!-- Header Ends --> <!-- Footer Start --> <TextView android:id="@+id/textAD" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/header" android:layout_alignParentRight="true" android:layout_marginBottom="14dp" android:layout_marginRight="26dp" android:text="Small Text" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#FFFFFF" /> <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/header" android:layout_gravity="center_horizontal" android:focusable="false" android:paddingBottom="5px" android:paddingTop="10px" android:src="@android:drawable/divider_horizontal_bright" /> <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="#000000" android:focusable="false" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/header" android:orientation="vertical" > <TextView android:id="@+id/textm" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Malzemeler" android:textSize="20dp" android:textColor="#000000"/> <EditText android:id="@+id/editaramalzeme" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" /> <Button android:id="@+id/btnmalzlist" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="false" android:text="Ara" /> <ListView android:id="@+id/mylist" android:layout_width="match_parent" android:layout_height="420dp" android:layout_weight="1" android:background="#FFFFFF" > </ListView> <ListView android:id="@+id/listsecili" android:layout_width="wrap_content" android:layout_height="210dp" android:layout_weight="1" android:background="#FFFFFF" > </ListView> <EditText android:id="@+id/txtNot" android:layout_width="match_parent" android:layout_height="88dp" android:ems="10" android:gravity="top" android:inputType="textMultiLine" android:lines="6" android:singleLine="false" > <requestFocus /> </EditText> </LinearLayout> <Button android:id="@+id/btnkaydet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/linearLayout1" android:text="malzeme ekle" /> <Button android:id="@+id/btntoplugonder" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/textAD" android:layout_below="@+id/btnkaydet" android:text="toplu gonder" /> </RelativeLayout> </ScrollView>** 

Nunca coloque listview en scroll view.Listview en sí es desplazable.

Prácticamente no es bueno hacerlo. Pero si quieres hacer esto, haz que la altura de listview fije a wrap_content.

 android:layout_height="wrap_content" 

Por defecto ListView es desplazable. No coloque ScrollView en ListView

No debe poner un ListView dentro de ScrollView porque la clase ListView implementa su propio desplazamiento y simplemente no recibe gestos porque todos ellos son manejados por el ScrollView padre

Listview por lo que tienen capacidades de desplazamiento inbuild. Así que no puedes usar listview dentro de scrollview. Encapsularlo en cualquier otro diseño como LinearLayout o RelativeLayout.

Este es mi código de trabajo. Usted puede intentar con esto.

Row.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listEmployeeDetails" android:layout_height="match_parent" android:layout_width="match_parent" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_gravity="center" android:background="#ffffff"> <TextView android:id="@+id/tvEmpId" android:layout_height="wrap_content" android:textSize="12sp" android:padding="2dp" android:layout_width="0dp" android:layout_weight="0.3"/> <TextView android:id="@+id/tvNameEmp" android:layout_height="wrap_content" android:textSize="12sp" android:padding="2dp" android:layout_width="0dp" android:layout_weight="0.5"/> <TextView android:layout_height="wrap_content" android:id="@+id/tvStatusEmp" android:textSize="12sp" android:padding="2dp" android:layout_width="0dp" android:layout_weight="0.2"/> </LinearLayout> 

Details.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listEmployeeDetails" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/page_bg" android:orientation="vertical" > <LinearLayout android:id="@+id/lLayoutGrid" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/page_bg" android:orientation="vertical" > ................... others components here............................ <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:alwaysDrawnWithCache="true" android:dividerHeight="1dp" android:horizontalSpacing="3dp" android:scrollingCache="true" android:smoothScrollbar="true" android:stretchMode="columnWidth" android:verticalSpacing="3dp" android:layout_marginBottom="30dp"> </ListView> </LinearLayout> </RelativeLayout> 

Clase del adaptador:

 import java.util.List; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class ListViewAdapter extends BaseAdapter { private Context context; private List<EmployeeBean> employeeList; publicListViewAdapter(Context context, List<EmployeeBean> employeeList) { this.context = context; this.employeeList = employeeList; } public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; EmployeeBeanHolder holder = null; LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(R.layout.row, parent, false); holder = new EmployeeBeanHolder(); holder.employeeBean = employeeList.get(position); holder.tvEmpId = (TextView) row.findViewById(R.id.tvEmpId); holder.tvName = (TextView) row.findViewById(R.id.tvNameEmp); holder.tvStatus = (TextView) row.findViewById(R.id.tvStatusEmp); row.setTag(holder); holder.tvEmpId.setText(holder.employeeBean.getEmpId()); holder.tvName.setText(holder.employeeBean.getName()); holder.tvStatus.setText(holder.employeeBean.getStatus()); if (position % 2 == 0) { row.setBackgroundColor(Color.rgb(213, 229, 241)); } else { row.setBackgroundColor(Color.rgb(255, 255, 255)); } return row; } public static class EmployeeBeanHolder { EmployeeBean employeeBean; TextView tvEmpId; TextView tvName; TextView tvStatus; } @Override public int getCount() { return employeeList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } } 

Clase de frijol empleado:

 public class EmployeeBean { private String empId; private String name; private String status; public EmployeeBean(){ } public EmployeeBean(String empId, String name, String status) { this.empId= empId; this.name = name; this.status = status; } public String getEmpId() { return empId; } public void setEmpId(String empId) { this.empId= empId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStatus() { return status; } public void setStatus(String status) { this.status =status; } } 

En Clase de actividad:

OnCreate método:

 public static List<EmployeeBean> EMPLOYEE_LIST = new ArrayList<EmployeeBean>(); //create emplyee data for(int i=0;i<=10;i++) { EmployeeBean emplyee = new EmployeeBean("EmpId"+i,"Name "+i, "Active"); EMPLOYEE_LIST .add(emplyee ); } ListView listView; listView = (ListView) findViewById(R.id.listView); listView.setAdapter(new ListViewAdapter(this, EMPLOYEE_LIST)); 

Poner ListView dentro de un ScrollView nunca se inspira. Pero si quieres tu comportamiento XML-like publicado, hay 3 opciones para mí:

  1. Eliminar ScrollView : Si elimina ScrollView , puede dar a ListView s un tamaño específico con respecto al diseño total (ya sea dp específico o layout_weight ).

  2. Reemplazar ListView con LinearLayout s: Puede agregar los elementos de lista iterando a través de la lista de elementos y agregar cada vista de elemento al LinearLayout correspondiente inflando la vista y ajustando los datos respectivos (cadena, imagen, etc.)

  3. Si realmente necesita poner su ListView en el ScrollView , debe hacer que su ListView no sea desplazable (lo cual es prácticamente igual que la solución 2 anterior, pero con los códigos ListView ), de lo contrario el diseño no funcionará como usted espera .
    Para hacer un ListView no desplazable, puede leer este post SO , donde la solución exacta para mí es como la siguiente:

 listView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return (event.getAction() == MotionEvent.ACTION_MOVE); } }); 

Sé que esta pregunta es 4-5 años de edad, pero aún así, esto podría ser útil:

A veces, si sólo tiene unos pocos elementos que "salen de la pantalla", es posible que la lista no se desplace. Esto se debe a que el sistema operativo no lo ve como realmente superando la pantalla.

Estoy diciendo esto porque me encontré con este problema hoy – sólo tenía 2 o 3 elementos que estaban excediendo los límites de la pantalla, y mi lista no era desplazable. Y fue un verdadero misterio. Tan pronto como agregué unos cuantos más, empezó a desplazarse.

Así que tienes que asegurarse de que no es un problema de diseño al principio, como la lista que parece ir más allá de los bordes de la pantalla, pero en realidad, "no lo hace", y ajustar sus dimensiones y valores de margen y ver si está empezando "Se puede desplazar". Lo hizo, para mí.

Encontré una solución complicada … que sólo funciona en RelativeLayout. Sólo necesitamos poner una vista encima de un ListView y establecer "true" clicable en Ver y false para ListView

  <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listview android:clickable="false" /> <View android:layout_width="match_parent" android:background="@drawable/gradient_white" android:layout_height="match_parent" android:clickable="true" android:layout_centerHorizontal="true" android:layout_alignTop="@+id/listview" /> 
  • ListView setOnItemClickListener no funciona en la vista de lista personalizada
  • Android: deshabilitar resaltado en listView haz clic
  • Android - ¿cómo puedo saber cuando gridview ha llegado al fondo?
  • ListView con alfabetos a la derecha, como el iPhone. ¿Es posible?
  • Mostrar el estado del último elemento de la lista en un ListView con ViewHolder
  • El primer elemento de un ListView no funciona correctamente en Android
  • Sólo en algunos teléfonos, ListView no se actualiza después de notifyDataSetChanged ()
  • Eliminar elementos ListView en Android
  • ListView artículo fondo hell
  • Android: clics retardados en ListView
  • Centrarse en EditText en ListView cuando bloquea descendientes (Android)
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.