17

我試圖通過從onCreateView(LayoutInflater, ViewGroup, Bundle)返回我自己的視圖層次結構來自定義片段佈局。這膨脹了我的自定義視圖,但似乎堆疊的意見,而不是內部膨脹,我看到一切。任何幫助表示讚賞。Android +具有自定義視圖層次結構的ListFragment

MyActivity.java:

public class MyActivity extends FragmentActivity{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) { 
      ArrayListFragment list = new ArrayListFragment(); 
      getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit(); 
     } 
    } 

    public static class ArrayListFragment extends ListFragment { 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inflater.inflate(R.layout.main, container); 
      return super.onCreateView(inflater, container, savedInstanceState); 
     } 

     @Override 
     public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 
      String List[] = {"Larry", "Moe", "Curly"}; 
      setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, List)); 
     } 
    } 
} 

main.xml中

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

    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="THIS IS A BUTTON" /> 

    <ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#00FF00" 
    android:layout_weight="1" 
    android:drawSelectorOnTop="false" /> 

    <TextView 
    android:id="@android:id/empty" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FF0000" 
    android:text="No data" /> 
</LinearLayout> 
+0

另外要注意,我使用的是Android的支持-V4。 從Android文檔,它明確規定: 「ListFragment具有由單個列表 視圖的默認佈局。然而,如果你願意,你可以通過 自定義片段佈局從onCreateView返回自己的視圖層次結構(。 LayoutInflater, ViewGroup,Bundle)爲此,您的視圖層次結構必須包含一個ID爲「@android:id/list」的 ListView對象(或者列表如果它位於 代碼中)。 – worked

回答

33

我沒有返回ListFragment類的onCreateView方法中的新視圖。例如:

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

現在所有的作品在Android的土地!

+4

你的'onCreateView'中的第一行是沒有必要的(它實際上並沒有做任何事......)inflater已經被傳入(你使用)。 – ashughes

+1

好點...更新。 – worked

+0

你能提供更多細節嗎? –

1
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit(); 

列表不被接受,它正在尋找一個片段。

2

我覺得你最好

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
View root = inflater.inflate(R.layout.main, container, false); 
return root; 
} 
0

使用這種模式,完美的作品!

inflater.inflate(R.layout.list_layout, container, false); 

公衆查看充氣(XmlPullParser解析器的ViewGroup根,布爾attachToRoot)

attachToRoot無論充氣層次結構應附加到根參數?如果爲false,則root僅用於爲XML中的根視圖創建LayoutParams的正確子類。

2

優化的方式,命中代碼

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.list_alphabet, container, false); 
    } 
相關問題