0

我想要一个带有下拉模板的 MvxSpinner,其中有一个按钮。单击按钮会打开一个包含有关该项目的信息的网络浏览器。按钮单击工作正常,但现在无法选择该项目。有没有解决的办法?

4

1 回答 1

1

我想出了一种方法来完成这项工作。不确定这是否是最佳实践,但它对我有用。

我使 Spinner 的 ItemsSource 成为可枚举的视图模型。有一个按钮单击命令和一个事件来处理项目中的任何其他触摸。我将 MvxSpinner 和 MvxAdapter 子类化。在子类适配器中,我将事件分配给以编程方式单击后退按钮以关闭下拉列表的处理程序。我还将一个方法从适配器路由到微调器,以设置所选项目。

public class ButtonSpinner : MvxSpinner
{
    public ButtonSpinner(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        var adapter = new ButtonAdapter(context, routedItemClick);
        adapter.ItemTemplateId = Adapter.ItemTemplateId;
        adapter.DropDownItemTemplateId = Adapter.DropDownItemTemplateId;
        adapter.SimpleViewLayoutId = Adapter.SimpleViewLayoutId;
        this.Adapter = adapter;
    }

    private void routedItemClick(object sender, ItemClickEventArgs e)
    {
        this.SetSelection((int)sender);
    }
}

public class ButtonAdapter : MvxAdapter
{
    EventHandler<Android.Widget.AdapterView.ItemClickEventArgs> _routedItemClick;
    View _parent;

    public override IEnumerable ItemsSource {
        get {
            return base.ItemsSource;
        }
        set {
            base.ItemsSource = value;
            if (value != null)
            {
                foreach(var item in value)
                {
                    var dialectItem = item as DialectItemViewModel;
                    dialectItem.DialectSelected += (object obj, EventArgs args) => {
                        var dialectItems = ItemsSource as List<DialectItemViewModel>;
                        _routedItemClick(dialectItems.IndexOf(dialectItem), null);

                        _parent.RootView.DispatchKeyEvent(new KeyEvent(KeyEventActions.Down, Keycode.Back));
                        _parent.RootView.DispatchKeyEvent(new KeyEvent(KeyEventActions.Up, Keycode.Back));
                    };
                }
            }
        }
    }

    public ButtonAdapter(Context context, EventHandler<Android.Widget.AdapterView.ItemClickEventArgs> routedItemClick) : base(context)
    {
        _routedItemClick = routedItemClick;
    }

    protected override Android.Views.View GetView (int position, Android.Views.View convertView, Android.Views.ViewGroup parent, int templateId)
    {
        if (_parent == null)
            _parent = parent;

        return base.GetView (position, convertView, parent, templateId);
    }
}
于 2013-08-25T01:54:11.183 回答