2012-08-02 105 views
6

如何創建一個ListFragment(支持V4庫)佈局從XML? ListFragment以編程方式設置其佈局,因此如果要修改它,則需要混亂添加/刪除視圖。ListFragment佈局從XML

+0

請編輯此所以很明顯的問題是什麼(發佈它作爲一個問題),並把下面的解決方案中的答案部分。 – 2012-08-27 12:58:26

+0

你有沒有一個可以參考的例子? – sherpya 2012-08-27 15:30:58

+0

該帖子應該只是一個問題和答案的形式。這看起來像一個解決方案,但它不清楚原來的問題是什麼答案。 – 2012-08-27 15:56:21

回答

14

我已將創建的佈局轉換爲xml,您可以添加/刪除您的小部件或例如添加pulltorefresh解決方案。你不應該改變需要的ID。 由於某些小部件需要內部id,因此需要調用幫助器函數,並且需要支持v4命名空間,因爲常量是內部默認可見性。

list_loader.xml

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

    <LinearLayout 
     android:id="@+id/progress_container_id" 
     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> 

    <FrameLayout 
     android:id="@+id/list_container_id" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <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> 
    </FrameLayout> 
</FrameLayout> 

android.support.v4.appListFragmentLayout(在你的項目,你並不需要修改支持V4 JAR)

package android.support.v4.app; 

import your.package.R; 
import android.view.View; 

public class ListFragmentLayout 
{ 
    public static void setupIds(View view) 
    { 
     view.findViewById(R.id.empty_id).setId(ListFragment.INTERNAL_EMPTY_ID); 
     view.findViewById(R.id.progress_container_id).setId(ListFragment.INTERNAL_PROGRESS_CONTAINER_ID); 
     view.findViewById(R.id.list_container_id).setId(ListFragment.INTERNAL_LIST_CONTAINER_ID); 
    } 
} 
在擴展 ListFragment

您的片段

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View view = inflater.inflate(R.layout.list_loader, container, false); 
    ListFragmentLayout.setupIds(view); 
    return view; 
} 
+0

你的意思是這個改變它會工作,就好像它不是自定義視圖一樣? – seb 2012-09-06 23:51:01

+0

你真棒! – seb 2012-09-07 00:06:21

+0

不適用於我。看起來我正在使用舊版本的支持庫。它甚至不包含ListFragmentLayout類。我不建議使用這種方法。在未來的支持lib版本中,它可能無法正常工作。 – WindRider 2013-01-31 21:32:24

-2

從androi d SDK(ListFragment)(http://developer.android.com/reference/android/app/ListFragment.html):

公共查看onCreateView(LayoutInflater吹氣,ViewGroup中容器,包savedInstanceState)

提供默認實現返回一個簡單的列表視圖。子類可以覆蓋以替換它們自己的佈局。如果這樣做,返回的視圖層次結構必須有一個ListView,其ID是android.R.id.list,並且可以有一個兄弟視圖ID android.R.id.empty,當列表爲空時,該視圖將被顯示。

如果您用自己的自定義內容重寫此方法,請考慮在佈局文件中包含標準佈局list_content,以便繼續保留ListFragment的所有標準行爲。特別是,這是目前唯一能夠顯示內置的不確定進度狀態的方法。

所以,當你想讓你自己的layout.xml在FragmentList上覆蓋onCreateView並膨脹自己的佈局。

例如:

創建一個Android的佈局(yourlistlayout.xml),並把下面的XML上要顯示你的ListFragment位置。

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 
    </ListView> 
ListFragment

把下面的代碼:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.yourlistlayout, null);  
    return v;  
} 
+0

由於id不同,這不適用於支持庫 – sherpya 2014-04-10 23:03:38