The Spinner behaviour is not expected for our requeriments. My solution is not work with Spinners, make it in one similar way, with one ListView inside one BaseFragment to research the functionality we are expected.
The beneficts are:
- No more headaches extending Spinner defaults.
- Easy implementation and customization.
- Full compatibility along all Android APIs.
- No face against first OnItemSelectedListener.onItemSelected call.
The main idea, is do something like this:
The BaseFragment layout could looks similar to:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@null"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ListView
android:id="@+id/fragment_spinnerList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
The code looks something like this:
public class SpinnerListFragment extends android.support.v4.app.DialogFragment {
static SpinnerListFragment newInstance(List<String> items) {
SpinnerListFragment spinnerListFragment = new SpinnerListFragment();
Bundle args = new Bundle();
args.putCharSequenceArrayList("items", (ArrayList) items);
spinnerListFragment.setArguments(args);
return spinnerListFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(), R.style.dialog);
final View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_spinner_list, null);
dialog.getWindow().setContentView(view);
dialog.setCanceledOnTouchOutside(true);
// CUSTOMIZATION...
final List items = (ArrayList) getArguments().getCharSequenceArrayList("items");
final ListView spinnerList = (ListView) view.findViewById(R.id.fragment_spinnerList);
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(
getActivity(),
R.layout.search_spinner_list_item,
items);
spinnerList.setAdapter(arrayAdapter);
spinnerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// DO SOMETHING...
SpinnerListFragment.this.dismiss();
}
});
return dialog;
}
}