Ok, now after many attempts I found a working solution, which is more or less a "dirty" workaround.
I just add a dummy item (e.g. an empty string) to the end of my list which is used to populate the adapter. Then in my adapter in the getDropDownView()
I check if the position is the last element of my adapter/list and set all my elements in my ViewHolder
to the height 0 as well as the height of the root layouts LayoutParams.
But be careful with the LayoutParams. It doesn't matter which Layout you have around your item widgets. Android converts the root layout to an AbsListView
. So you have to use the AbsListView.LayoutParams
to be able to set the layout height to zero.
Here is a little example how I did it.
public class CustomSpinnerAdapter extends BaseAdapter {
public static class ViewHolder {
public RelativeLayout mBaseLayout;
public TextView mDayLabel;
public TextView mDateLabel;
}
private List<String> mItems;
private String[] mDropdownDates;
public CustomSpinnerAdapter() {
mItems = new ArrayList<String>();
mItems.add("Today");
mItems.add("Yesterday");
mItems.add("2 days ago");
mItems.add("3 days ago");
mItems.add("");
// used to fill String array with dates in specified format
mDropdownDates = populateDates("dd:mm");
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ...
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_spinner_dropdown_date, parent, false);
viewHolder.mBaseLayout = (RelativeLayout) convertView.findViewById(R.id.spinnerDropdownLayout);
viewHolder.mDayLabel = (TextView) convertView.findViewById(R.id.spinnerDropdownDay);
viewHolder.mDateLabel = (TextView) convertView.findViewById(R.id.spinnerDropdownDate);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
if(position < getCount() - 1) {
viewHolder.mDayLabel.setText(mItems.get(position));
viewHolder.mDateLabel.setText(mDropdownDates[position]);
}
else {
viewHolder.mDayLabel.setHeight(0);
viewHolder.mDateLabel.setHeight(0);
viewHolder.mBaseLayout.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
viewHolder.mBaseLayout.setVisibility(View.GONE);
}
parent.setVerticalScrollBarEnabled(false);
return convertView;
}
}
I hope this helps some of you.