2015-07-20 66 views
5

我有一個ScrollView它最初包裝了​​兩個MvxListView控件。如何將`ItemClick`綁定到`MvxLinearLayout`?

擁有ListView控制在ScrollView不受Android支持,這是有道理的,因爲它們都試圖填充父級高度並提供自己的滾動邏輯。

我想要的是在我的ScrollView裏面有兩個不可滾動的列表,它們的全高度。 ListView其中MvxListView延伸不支持此手動黑客高度。

我想要這個的原因是因爲我有兩個單獨的列表,我必須分開來源,他們都有自己的標題。我需要所有這些可在一個ScrollView內滾動。

然後我發現MvxLinearLayout這是一個可綁定的LinearLayout它有一個ItemSource屬性,我可以綁定到。它非常棒,它顯示我的物品並獲得所有物品的全部高度,因此我可以在我的ScrollView中滾動我的列表。問題是它似乎沒有ItemClick屬性,所以我沒有辦法從我的列表中獲取用戶輸入。

有沒有人知道一個乾淨的方式做到這一點的可綁定方式?我不想在後面的代碼中附加onItemClick處理程序。有另一個MvvmCross控件可以做我想要的嗎?

+0

我不明白爲什麼你不能把所有的數據填充到唱歌中列表視圖? – Cheesebaron

回答

10

可以擴展MvxLinearLayout支持ItemClick

public class MvxClickableLinearLayout : MvxLinearLayout 
{ 
    public MvxClickableLinearLayout(Context context, IAttributeSet attrs) 
     : this(context, attrs, new MvxClickableLinearLayoutAdapter(context)) 
    { 
    } 

    public MvxClickableLinearLayout(Context context, IAttributeSet attrs, MvxClickableLinearLayoutAdapter adapter) 
     : base(context, attrs, adapter) 
    { 
     var mvxClickableLinearLayoutAdapter = Adapter as MvxClickableLinearLayoutAdapter; 
     if (mvxClickableLinearLayoutAdapter != null) 
     { 
      mvxClickableLinearLayoutAdapter.OnItemClick = OnItemClick; 
     } 
    } 

    public ICommand ItemClick { get; set; } 

    public void OnItemClick(object item) 
    { 
     if (ItemClick != null && ItemClick.CanExecute(item)) 
     { 
      ItemClick.Execute(item); 
     } 
    } 
} 

適配器:

public class MvxClickableLinearLayoutAdapter : MvxAdapterWithChangedEvent, View.IOnClickListener 
{ 
    public delegate void ItemClickDelegate(object item); 

    public ItemClickDelegate OnItemClick; 

    public MvxClickableLinearLayoutAdapter(Context context) 
     : base(context) 
    { 
    } 

    public void OnClick(View view) 
    { 
     var mvxDataConsumer = view as IMvxDataConsumer; 

     if (mvxDataConsumer != null && OnItemClick != null) 
     { 
      OnItemClick(mvxDataConsumer.DataContext); 
     } 
    } 

    protected override View GetView(int position, View convertView, ViewGroup parent, int templateId) 
    { 
     View view = base.GetView(position, convertView, parent, templateId); 
     view.SetOnClickListener(this); 
     return view; 
    } 
} 

現在可以綁定到ItemClick就像你要做一個ListView做:

local:MvxBind="ItemClick SomeCommand" 
+2

在MvvmCross 5.1中不起作用,因爲解析時mvxDataConsumer爲空(查看爲IMvxDataConsumer)。任何想法爲什麼? –

0

您需要將點擊綁定添加到佈局中的單獨項目。 您可以添加點擊任何像這樣的佈局:

<RelativeLayout 
    android:background="?android:attr/selectableItemBackground" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/relativeLayout1" 
    local:MvxBind="Click SomeCommand"> 
+0

嗨,感謝您的回覆。是的,我意識到我可以做到這一點,但與'MvxLinearLayout'的整個點是它有一個'ItemsSource'這是數據綁定,所以它會自動將我的項目添加到列表。按照你的建議,我必須手動爲我的項目創建所有視圖,並綁定到'Click'或者在他們通過'MvxLinearLayout'自動獲得數據綁定後循環遍歷它們,這從MVVM的角度來看並不是很乾淨。 –

0

您是否嘗試過以指定MvxLinearLayout項模板?例如local:MvxItemTemplate="@layout/item_template"?您可以在要處理點擊的控件的項目模板中設置MvvmCross點擊綁定。

相關問題