Spinner dropUP instead dropDOWN android

China☆狼群 提交于 2021-02-05 04:58:26

问题


I have an Adapter which extends from ArrayAdapter in order to fill one Spinner with one collections of objects (City). Both all objects and adapter work correctly, at least what it that it seems. The problem is that when I display all collections of cities, it is displayed to UP instead to DOWN.

Does anyone know what can be happen?

This is my Adapter

public class AdapterSpinnerListCities extends ArrayAdapter<City> {

// My context
private Context context;
// Values for the spinner (City)
private List<City> listLocationsSpinner = UpdatedStatusApplication.getListLocations();
Typeface tf = null;

public AdapterSpinnerListCities (Context context, int textViewResourceId, List<City> listLocationsAPP) {
    super(context, textViewResourceId, listLocationsAPP);
    this.context = context;
    this.listLocationsSpinner = listLocationsAPP;
    tf = CustomFontsLoader.getTypeface(this.context,CustomFontsLoader.FONT_CHALKBOARD_1);   
}

public int getCount(){
   return listLocationsSpinner.size();
}

public City getItem(int position){
   return listLocationsSpinner.get(position);
}

public long getItemId(int position){
   return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {         
    
    LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item = convertView;  
    
    item = inflater.inflate(R.layout.list_item_city, null);
    TextView tvCity = (TextView)item.findViewById(R.id.tv_item_city);
    tvCity.setText(listLocationsSpinner.get(position).getCityName().toUpperCase()); 
    tvCity.setTypeface(tf);   
        
    return(item);
}



// And here is when the "chooser" is popped up
// Normally is the same view, but you can customize it if you want
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {       
    
    LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item = convertView;  
    
    if (listLocationsSpinner.get(position).getCitySubcity() == TagManager.TAG_IS_NOT_SUBCITY){
        item = inflater.inflate(R.layout.list_item_city, null);
        TextView tvCity = (TextView)item.findViewById(R.id.tv_item_city);
        tvCity.setText(listLocationsSpinner.get(position).getCityName().toUpperCase());             
        tvCity.setTypeface(tf);             
    }
    else{
        item = inflater.inflate(R.layout.list_item_subcity, null);
        TextView tvSubCity = (TextView)item.findViewById(R.id.tv_item_subcity);
        tvSubCity.setText(listLocationsSpinner.get(position).getCityName());    
        tvSubCity.setTypeface(tf);              
    }
    return(item);        
  }

My Spinner in layout (it is into a RelativeLayout)

<LinearLayout
    android:id="@+id/lyt_locationSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/LinearLayoutLogo"
    android:layout_below="@+id/lyt_drink_food_deals"
    android:orientation="horizontal"         
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"         
    >                
    <Spinner android:id="@+id/cmb_location"
        style="@style/style_btn_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"                     
        android:spinnerMode="dropdown"            
        />                   
</LinearLayout>

My Activity

   private Spinner mySpinner;
   private AdapterSpinnerListCities adapter;
   List<City> listDeployedLocations = new ArrayList<City>();

    .....
    .....

    adapter = new AdapterSpinnerListCities (this,  android.R.layout.simple_spinner_dropdown_item, listDeployedLocations);        
    mySpinner = (Spinner) findViewById(R.id.cmb_location);
    mySpinner.setAdapter(adapter); // Set the custom adapter to the spinner
    
    mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected (AdapterView<?> parentView, View selectedItemView, int position, long id) {
            citySelected = listDeployedLocations.get(position);
            System.out.println("Spinner value...."+ citySelected.getCityID()+"-"+citySelected.getCityName());
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // code here
        }


    });   


回答1:


This would happen if the item which inflates the spinner is close to the bottom of the screen. Since the spinner would not have enough room (or just barely enough room) if inflated downward, it is inflated upward instead. Try putting the spinner higher (vertically) in the layout.




回答2:


Try adding marginEnd to the spinner. The spinner does not have enough room to open the downwards.



来源:https://stackoverflow.com/questions/16910565/spinner-dropup-instead-dropdown-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!