2013-12-18 43 views
0

我有這個列表佈局,我試圖讓我的加載列表內容在兩個佈局之間移動,這樣我就可以在一個頂部和另一個底部使用按鈕。Android ListFragment Layout

下面是XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_list_contrainer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/progress_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     <ProgressBar 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/rel1_layout_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="25dp" 
     android:background="@color/Gray" > 

     <TextView 
      android:id="@+id/text_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="TEXTTTTHEREEE" 
      android:textColor="@color/White" 
      android:textSize="18sp" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/list_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/layout_list_view" > 

     <TextView 
      android:id="@+id/empty_id" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" /> 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:drawSelectorOnTop="false" > 
     </ListView> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/selection_layout_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="25dp" 
     android:layout_alignParentBottom="true" 
     android:background="@color/Gray" > 

     <Button 
      android:id="@+id/button1" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@color/Gray" 
      android:text="BUTTON1" 
      android:textColor="@color/White" /> 

     <Button 
      android:id="@+id/button2" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:background="@color/Gray" 
      android:text="BUTTON2" 
      android:textColor="@color/White" /> 

     <Button 
      android:id="@+id/button3" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:background="@color/Gray" 
      android:text="BUTTON3" 
      android:textColor="@color/White" /> 
    </RelativeLayout> 

</RelativeLayout> 

我有在那裏僅僅只顯示列表內容的麻煩。它顯示爲好像其他佈局不存在。我一直在尋找建議,看看我做錯了什麼,或者我只是在這裏錯過了一些東西。

這裏是充氣代碼以及:

// Set-up inflater 
     _inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

下面的反應,謝謝你。這對xml文件來說沒什麼問題,我發現你的輸出結果與我之前所做的一樣準確。我很愚蠢,沒有正確使用自定義片段。我已經應用的修復是這個。

答:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = super.onCreateView(inflater, container, savedInstanceState); 
     ListView listView = (ListView) view.findViewById(android.R.id.list); 
     ViewGroup parent = (ViewGroup) listView.getParent(); 

     // Remove ListView and add CustomView in its place 
     int listViewIndex = parent.indexOfChild(listView); 
     parent.removeViewAt(listViewIndex); 
     RelativeLayout relLayout = (RelativeLayout) inflater.inflate(
       R.layout.main_list_contrainer_layout, container, false); 
     parent.addView(relLayout , listViewIndex, 
       listView.getLayoutParams()); 
     return view; 
    } 

這解決了我的問題。如果有人看到這樣做的更有效的方式,讓我知道,但從我後來看到的,我認爲這是如何使用自定義片段佈局與列表視圖完成。

再次感謝你們

回答

0

答:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = super.onCreateView(inflater, container, savedInstanceState); 
     ListView listView = (ListView) view.findViewById(android.R.id.list); 
     ViewGroup parent = (ViewGroup) listView.getParent(); 

     // Remove ListView and add CustomView in its place 
     int listViewIndex = parent.indexOfChild(listView); 
     parent.removeViewAt(listViewIndex); 
     RelativeLayout relLayout = (RelativeLayout) inflater.inflate(
       R.layout.main_list_contrainer_layout, container, false); 
     parent.addView(relLayout , listViewIndex, 
       listView.getLayoutParams()); 
     return view; 
    } 
1

您需要的上方和下方設置爲包含ListView佈局。

現在,listview正在其他視圖前面。

試試這個佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_list_contrainer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/progress_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:visibility="gone" > 

     <ProgressBar 
      style="?android:attr/progressBarStyleLarge" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/rel1_layout_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="25dp" 
     android:background="@color/Gray" > 

     <TextView 
      android:id="@+id/text_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="TEXTTTTHEREEE" 
      android:textColor="@color/White" 
      android:textSize="18sp" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/list_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@id/selection_layout_list_view" 
     android:layout_below="@id/rel1_layout_list_view" > 

     <TextView 
      android:id="@+id/empty_id" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" /> 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:drawSelectorOnTop="false" > 
     </ListView> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/selection_layout_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="25dp" 
     android:layout_alignParentBottom="true" 
     android:background="@color/Gray" > 

     <Button 
      android:id="@+id/button1" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@color/Gray" 
      android:text="BUTTON1" 
      android:textColor="@color/White" /> 

     <Button 
      android:id="@+id/button2" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:background="@color/Gray" 
      android:text="BUTTON2" 
      android:textColor="@color/White" /> 

     <Button 
      android:id="@+id/button3" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:background="@color/Gray" 
      android:text="BUTTON3" 
      android:textColor="@color/White" /> 
    </RelativeLayout> 

</RelativeLayout> 

OUTPUT:

enter image description here

+0

不幸的是,這並沒有奏效,我不明白爲什麼非那些其他佈局甚至出現。 – thekevshow

1

只需使用這一個:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/progress_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:visibility="gone" > 

    <ProgressBar 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="10" > 

    <LinearLayout 
     android:id="@+id/rel1_layout_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#c0c0c0" > 

     <TextView 
      android:id="@+id/text_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TEXTTTTHEREEE" 
      android:layout_gravity="center" 
      android:textColor="#fff" 
      android:textSize="18sp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/list_container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="8" 
     android:orientation="vertical" > 

     <!-- <TextView 
      android:id="@+id/empty_id" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" /> --> 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:drawSelectorOnTop="false" > 
     </ListView> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/selection_layout_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#c0c0c0" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button1" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:background="#c0c0c0" 
      android:text="BUTTON1" 
      android:textColor="#fff" /> 

     <Button 
      android:id="@+id/button2" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:background="#c0c0c0" 
      android:text="BUTTON2" 
      android:textColor="#fff" /> 

     <Button 
      android:id="@+id/button3" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:background="#c0c0c0" 
      android:text="BUTTON3" 
      android:textColor="#fff" /> 
    </LinearLayout> 
    </LinearLayout> 

</LinearLayout>