2013-07-29 48 views
3

我有一個項目的水平列表,我希望平均分享可用的屏幕寬度。有沒有什麼辦法讓Mvx.MvxLinearLayout尊重android:layout_weight?

以前我用索引綁定到每個項目和一個靜態LinearLayout與每個項目具有相同的layout_weight。

當我用Mvx.MvxLinearLayout和ItemsSource綁定替換靜態LinearLayout,並將項目的標記移動到Itemtemplate中時,MvxLinearLayout不再考慮layout_weight。

我已經嘗試過重構項目模板的各種方式,並且在安排項目時似乎沒有辦法讓這個控件服從layout_weight。

有沒有什麼辦法讓Mvx.MvxLinearLayout尊重android:layout_weight,或者任何可以給我一個動態列表項目的動態列表,這些列表項目會按照固定維度的邊界進行平等安排?

編輯

假設3項列表,一個字符串屬性 '名稱'。

此標記按預期工作,給予同等寬度的3文本項:

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     local:MvxBind="Text Items[0].Name" 
     /> 
    <TextView 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     local:MvxBind="Text Items[1].Name" 
     /> 
    <TextView 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     local:MvxBind="Text Items[2].Name" 
     /> 
</LinearLayout> 

這個方法行不通,佈局權重不正確應用到TextViews:

<Mvx.MvxLinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    local:MvxBind="ItemsSource Items" 
    local:MvxItemTemplate="@layout/item_template" /> 

的ItemTemplate佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    local:MvxBind="Text Name" /> 
+0

請包括一些以前的工作和你已經嘗試過的代碼。層次結構查看器顯示的差異是什麼? – Stuart

+0

我已經添加了請求的信息,任何人都可以幫忙嗎? – Dave

+0

是否有解決此問題的方法?我目前面臨同樣的問題。有一個MvxLinearLayout不尊重其中的項目layout_weight。 – Nexus2k

回答

0

沒有辦法讓MvxLinearLayout尊重android:layout_weight

沒有,我知道的 - 但它聽起來就像你在一個完美的位置,調試和實驗。

要調試的佈局問題,一個非常有用的工具是層次觀衆 - http://developer.android.com/tools/debugging/debugging-ui.html - 使用此兩個佈局可能會顯示您的區別是什麼時,他們實際上是在設備上運行。

MvxLinearLayout只是一個LinearLayout - 從它繼承和簡單地增加了數據綁定Adapter允許您添加/動態刪除兒 - 看MvxLinearLayout.cs.

在此基礎上,一個假設是,也許LinearLayout本身不支持layout_weight與這樣的動態孩子。要調查此問題:

  • 您可以嘗試在不使用Mvx的情況下對UI進行原型設計 - 在添加/刪除子項時,會執行簡單的LinearLayout「尊重layout_weight」嗎?如果答案是肯定的,那麼你的原型和MvxLinearLayout.cs.之間有什麼區別。如果答案是否定的,那麼你知道這個問題在LinearLayout的某處。
  • 您可以嘗試查看LinearLayout來源 - 例如這個調查可能會發現一些前進的方向 - 例如,它可能會找到一些方法來強制LinearLayout重新測量,然後您可以從ChildViewAddedChildViewRemoved事件中調用「force remeasure」方法。

任何替代方法,它可以給我說,將安排同樣給定的固定尺寸的邊界項目的動態列表?

如果內置佈局不適合您,那麼創建您自己的自定義佈局非常簡單 - 例如,看到http://arpitonline.com/blog/2012/07/01/creating-custom-layouts-for-android/ - 一旦你這樣做,然後添加綁定的複製一樣簡單和粘貼從MvxLinearLayout.cs.

+0

是否有可能獲得線性佈局項目的點擊事件,以便對點擊的項目執行操作(如刪除它)? – Telemat

2

的問題是ViewGroup綁定代碼,mvvmcross使用MvxLinearLayout有一個ItemTemplate時插入一個額外的視圖。因此,而不是得到一個視圖層次結構:

MvxLinearLayout - > TextView的

你結束了:

MvxLinearLayout - > MvxListItemView - > TextView的。

因此,應用於TextView的佈局屬性,特別是權重,對於LinearLayout是不可見的。 MvxListItemView使用默認的佈局屬性(包裝內容)準備好,並且沒有看到重量效果。

動作發生的位置在MvxViewGroupExtensions(例如Refill)中,其中子視圖添加到LinearLayout,以及MvxAdapter中的CreateBindableView方法,其中子視圖打包在IMvxListItemView中。

解決此問題的一種方法是重寫MvxViewGroupExtensions中的方法,並添加視圖以更新MvxListItemViews的佈局屬性(例如,從基礎視圖中複製它們)。例如:

private static void Refill(this ViewGroup viewGroup, IAdapter adapter) { 
    .... // rest of code 
    Invalidate(); 
    for (int j = 0; j < viewGroup.ChildCount; j++) 
    { 
     var child = GetChildAt(j) as MvxListItemView; 
     var param = new LinearLayout.LayoutParams(
        0, ViewGroup.LayoutParams.WrapContent, 1.0f); 
     child.LayoutParameters = param; 
    } 
} 
相關問題