下拉列表框的大小

时间:2019-10-10 18:06:09

标签: android android-spinner

Cities list has around 400 items and maximum size of spinner is 400dp but it is showing only one item as you see in the image.PS:It is not the case everytime.sometimes it is showing maximum size.我正在根据上一个选定项目的响应来动态填充微调器。尽管列表的大小大于1,但下拉列表的大小有时仅显示一个项目(滑块正在工作)。并非每次都发生这种情况。有时它显示的是最大大小,有时不是。

下拉菜单的最大大小为400dp。

<com.jaredrummler.materialspinner.MaterialSpinner
                android:id="@+id/spinnerCollege"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                app:ms_dropdown_max_height="400dp"
                app:ms_background_color="@color/offWhite"/>

new CityPresenter().getCities(new OnEntitiesReceivedListener<City>(this) {
            @Override
            public void onReceived(final List<City> cities) {
                UpdateProfileActivity.this.cities = cities;
                final List<String> cityNames = new ArrayList<>();
                for(City city : cities){
                    cityNames.add(city.getName());
                }
              //  Collections.sort(cityNames);
                Log.d(TAG,cityNames.toString());
                cityNames.add(0,"Select City");
                citySpinner.setItems(cityNames);
}
}

预计在下拉菜单中显示多个项目,但仅显示一项。

1 个答案:

答案 0 :(得分:0)

我不知道您如何设置适配器,但是通常这就是让动态微调器工作的方式。

动态列表微调器

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get reference of spinner from XML layout
    Spinner spinner = findViewById(R.id.mySpinnerId);
    //Using this button to trigger spinner list change
    Button myButton=findViewById(R.id.myButtonId);

     final List<String> myArrayList= new ArrayList<>();
     myArrayList.add("Some String");
     myArrayList.add("Some Other String");
     //And....so on

     final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_spinner_item,myArrayList);
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     spinner.setAdapter(adapter);

     //Triggering the spinner list change using a button
     myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        myArrayList.add("New Item");
        adapter.notifyDataSetChanged();

        }

} }

希望这可以帮助您识别并解决问题。