重新选择在动作条微调项 [英] Reselecting Spinner item in ActionBar

查看:88
本文介绍了重新选择在动作条微调项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在开发一个Android应用程序,我们使用的是带有旋转器导航特定视图的操作栏。

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

的主要问题是:用户应该可以重新选择一个操作杆微调的项目,此前他已经选择了该项。 Android的似乎prevent第二选择一个操作栏纺纱项目。

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?

推荐答案

好了,现在很多的尝试后,我找到了一个可行的解决方案,这或多或少是一个肮脏的解决方法。

Ok, now after many attempts I found a working solution, which is more or less a "dirty" workaround.

我只需要添加一个虚拟物品(例如,一个空字符串)到我的列表,用来填充适配器结束。然后在我的适配器 getDropDownView()我检查,如果位置是我的适配器/列表中的最后一个元素,并把我在我的 ViewHolder所有元素的高度0以及根布局的LayoutParams的高度。

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.

但要小心的的LayoutParams。这并不重要的布局,你必须在你的项目的部件。 Android的根布局转换为 AbsListView 。所以,你必须使用 AbsListView.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.

这篇关于重新选择在动作条微调项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆