Reselecting Spinner item in ActionBar

不问归期 提交于 2019-12-23 04:02:20

问题


Currently I'm developing an Android application and we are using the action bar with a spinner navigation for a specific view.

The main problem is: The user should be able to reselect an action bar spinner item, after he already selected this item. Android seems to prevent a second selection of an action bar spinner item.

Is there a way to be able to select an item more than once or is there a completely other way to achieve this behavior?


回答1:


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.



来源:https://stackoverflow.com/questions/22430498/reselecting-spinner-item-in-actionbar

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