Cómo limitar la altura de la vista desplegable de Spinner en Android

Por favor, sugerir cualquier enfoque que utilizo para crearlo.

Consulta : Estoy creando 2-Spinner vista, donde tengo que agregar la lista de País / Ciudades, Así como si estoy seleccionando la India, entonces estoy recibiendo 50 elementos dentro de la vista desplegable, el problema con esto es que está tomando toda la página En Altura .

Lo que quiero : Quiero crear una vista desplegable, donde el usuario puede ver sólo 10 elementos en la vista desplegable, se mostrarán otros elementos cada vez que el usuario desplace la vista desplegable.


Mi problema

Puede utilizar Reflection.

Spinner spinner = (Spinner) findViewById(R.id.spinner); try { Field popup = Spinner.class.getDeclaredField("mPopup"); popup.setAccessible(true); // Get private mPopup member variable and try cast to ListPopupWindow android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner); // Set popupWindow height to 500px popupWindow.setHeight(500); } catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) { // silently fail... } 

También puede afectar la ubicación y el tamaño de la vista desplegable subclasificando Spinner y reemplazando su getWindowVisibleDisplayFrame(Rect outRect) que es utilizado por android.widget.PopupWindow para los cálculos. Simplemente establezca la outRect para limitar el área en la que se puede mostrar la vista desplegable.

Por supuesto, este enfoque no es adecuado para todos los escenarios ya que a veces se desea colocar una vista desplegable para no obscurecer otra vista o por alguna otra condición conocida sólo "fuera de la instancia".

En mi caso, necesitaba aplicar FLAG_LAYOUT_NO_LIMITS bandera a mi ventana de actividad que causó el outRect a ser enorme y por lo tanto parte de la vista desplegable se escondía a veces detrás de la barra de navegación. Con el fin de restaurar el comportamiento original que utiliza la siguiente anulación:

 @Override public void getWindowVisibleDisplayFrame(Rect outRect) { WindowManager wm = (WindowManager) getContext.getSystemService(Context.WINDOW_SERVICE); Display d = wm.getDefaultDisplay(); d.getRectSize(outRect); outRect.set(outRect.left, <STATUS BAR HEIGHT>, outRect.right, outRect.bottom); } 

Para eso he creado mi propio, PopUpWindow como lo sugiere @theLittleNaruto, en la sección de comentarios.

Main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:layout_marginTop="80dp" android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Country" android:layout_gravity="center_vertical|center_horizontal"/> </LinearLayout> 

Popup_example.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dip" > <ListView android:id="@+id/lstview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

Showpopup_1.java

 package com.example.spinnerworking; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageButton; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.PopupWindow.OnDismissListener; import android.widget.Toast; public class showpopup_1 extends Activity { boolean click = true ; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); final LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final Button b = (Button) findViewById(R.id.btn); final View pview = inflater.inflate(R.layout.popup_example, (ViewGroup) findViewById(R.layout.main)); final PopupWindow pw = new PopupWindow(pview); Log.i("hello", "hello") ; b.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (click) { // if onclick written here, it gives null pointer exception. // if onclick is written here it gives runtime exception. pw.showAtLocation(v, Gravity.CENTER_HORIZONTAL, 0, 0); pw.update(8, 0, 150, 200); String[] array = new String[] { "tushar", "pandey", "almora" }; ListView lst = (ListView) pview.findViewById(R.id.lstview); adapterHello adapter = new adapterHello(showpopup_1.this); lst.setAdapter(adapter); lst.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(showpopup_1.this, "pandey", Toast.LENGTH_SHORT).show(); } }); click = false ; } else { pw.dismiss(); click = true; } } }); } } class adapterHello extends BaseAdapter { String array[] = new String[] { "tushar", "pandey", "almora", "hello", "tushar", "pandey", "almora", "hello", "tushar", "pandey", "almora", "hello" }; showpopup_1 context; public adapterHello(showpopup_1 context) { this.context = context; } public int getCount() { // TODO Auto-generated method stub return array.length; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub TextView text = new TextView(context); text.setHeight(30); text.setPadding(10, 8, 0, 0); text.setTextColor(Color.BLACK); text.setText(array[position]); text.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("clicked", "tushar"); } }); return text; } } 
  1. Agrega android:popupBackground="#00000000" al Spinner
  2. En el adaptador

 getDropDownView(); parentParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, (int) Utils.convertDpToPx(350)); parentParams.gravity = Gravity.BOTTOM; parent.setLayoutParams(parentParams); 
  1. Usted puede mover el popup agregando android:dropDownVerticalOffset="60dp"
  • Obtener la posición de un spinner en Android
  • Evitar que el menú desplegable de hilanderos muestre
  • Spinner de Selección Múltiple
  • Barra de acciones setnavigationmode deprecated
  • La mejor práctica para implementar el par de valores clave en Android Spinner
  • Android spinner prompt
  • Spinner onItemSelected no se llama usando Marsmallow
  • ¿Cómo puedo extender el spinner para implementar una nueva función?
  • Android Spinner: centralizar el texto verticalmente dentro de la hiladora
  • Cómo mantener onItemSelected de disparar en un Spinner recién instanciado?
  • Android.R.simple_spinner_adapter no se puede resolver
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.