2013-07-06 41 views
2

我開始爲Android開發。我想創建我的模式警報(如iOS中的UIAlertView)。我認爲使用活動工作正常。我花了一些時間來做到這一點。但後來,我使用DialogFragment找到了更好的解決方案。我將我的活動更改爲對話框片段,並修改了片段相關的所有必需部分。它工作正常。除了我的片段中的ListView不再滾動!問題可能是什麼?Android - ListView不會滾動DialogFragment裏面

注意:它已經在Activity解決方案中工作。沒有滾動視圖。

這裏是XML:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="@dimen/modal_list_outter_frame_margin_vertical" 
    android:layout_marginLeft="@dimen/modal_list_outter_frame_margin_horizontal" 
    android:layout_marginRight="@dimen/modal_list_outter_frame_margin_horizontal" 
    android:layout_marginTop="@dimen/modal_list_outter_frame_margin_vertical" 
    android:background="@drawable/modal_list_outter_frame" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/modal_list_padding_bottom" > 

    <TextView 
     android:id="@+id/title_text_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/modal_list_title_horizontal_margin" 
     android:layout_marginRight="@dimen/modal_list_title_horizontal_margin" 
     android:layout_marginTop="@dimen/modal_list_title_top_margin" 
     android:gravity="center" 
     android:maxLines="@integer/modal_list_title_number_of_lines" 
     android:shadowColor="@color/modal_list_text_shadow_color" 
     android:shadowDx="0" 
     android:shadowDy="@integer/modal_list_title_shadow_offset_y" 
     android:shadowRadius="@integer/modal_list_title_shadow_radius" 
     android:text="@string/modal_list_title_small" 
     android:textColor="@color/modal_list_text_color" 
     android:textSize="@dimen/modal_list_title_font_size" 
     android:textStyle="bold" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/modal_list_inner_frame_margin_bottom" 
     android:layout_marginLeft="@dimen/modal_list_inner_frame_margin_horizontal" 
     android:layout_marginRight="@dimen/modal_list_inner_frame_margin_horizontal" 
     android:layout_marginTop="@dimen/modal_list_inner_frame_margin_top" 
     android:background="@drawable/modal_list_inner_frame" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/modal_list_padding_bottom" > 

     <ListView 
      android:id="@+id/list_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="@dimen/modal_list_list_view_margin_horizontal" 
      android:layout_marginRight="@dimen/modal_list_list_view_margin_horizontal" 
      android:layout_marginTop="@dimen/modal_list_list_view_margin_top" 
      android:divider="@null" 
      android:dividerHeight="0dp" 
      android:listSelector="@color/modal_list_selector_color_selected" 
      > 

     </ListView> 

    </LinearLayout> 

</LinearLayout> 

更新:我發現了一些奇怪的東西!就像片段是透明的!如果我點擊片段中的任何東西,就好像我在點擊它下面的按鈕!下面是我使用的顯示片段代碼:

FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.addToBackStack("SingleSelectionCustomRowModalList"); 
modalList = ASModalList.newInstance(modalListStateForCustomRow) ; 
modalList.show(ft, "SingleSelectionCustomRowModalList"); 

更新2:看來問題是DialogFragment不是模態。我使用這種風格:

int style = DialogFragment.STYLE_NO_TITLE | DialogFragment.STYLE_NO_FRAME; 
setStyle(style, R.style.ASModaListDialogStyle); 

所使用的主題是:

<style name="ASModaListDialogStyle" parent="@android:style/Theme.Dialog"> 
    <item name="android:windowBackground">@drawable/modal_list_background_view</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowTitleStyle">@null</item> 
    <item name="android:colorBackgroundCacheHint">@null</item> 
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> 
    <item name="android:gravity">center</item> 
</style> 

我使用這個主題,使暗淡的對話框的背景。

+0

你可以發佈你的代碼嗎? –

回答

0

請提供一些代碼,否則很難說出了什麼問題。例如佈局XML或您的DialogFragment類。

+2

注意:最好給每個人添加這樣的內容作爲評論,而不是回答。 –

0

它以對話框爲非模態結束。修復它使用:

int style = DialogFragment.STYLE_NORMAL | DialogFragment.STYLE_NO_FRAME; 
    setStyle(style, R.style.ASModaListDialogStyle); 
0

老問題沒有一個答案,實際上爲我工作。 希望它能幫助別人。

解決方法是將列表高度設置爲0dp,並添加權重字段。並刪除內部LinearLayout。這樣,外部LinearLayout可以將正確的大小設置爲列表,並且它將滾動。

<android.support.v7.widget.ListViewCompat 
    android:id="@+id/ev_list" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:layout_marginLeft="16dp" 
    android:layout_marginRight="16dp"/>