2012-07-13 64 views
0

我開始使用Fragments,並且我已經完成了API指南,但是......當然它太容易了;) 當我啓動應用程序時,它崩潰了。經過一番研究,我發現這篇文章Android fragment is not working 和Stephen Wylie的迴應似乎糾正了阿里的事情,但..我不明白! 我應該在哪裏放置FrameLayout? 「where_i_want_my_fragment」id ...它是我想要的,對吧? 最後,我應該在哪裏放置Java代碼?在我的活動中(通過這種方式顯示2個片段)。Android碎片:錯誤修復說明

謝謝!

尼科

編輯:這麼說吧,我想要什麼設計,你會更好地瞭解我的想法。 我想要一個顯示字符串列表的左邊的列表片段,並且在右邊我想要一個片段顯示有關列表中選定字符串的信息。我想能夠用手指移動我的應用程序的右側(我不知道是否更好刷卡片或任何..這是相同的佈局,但充滿不同的數據)

好吧,我只是發佈我的代碼,因爲我真的不明白爲什麼它沒有做任何事情。 這是我activity_main.xml中

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:baselineAligned="false" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/list_frag" 
    android:name="main.courante.c.DateListFragment" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" > 
</FrameLayout> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fiche_frag" 
    android:name="main.courante.c.fiche_frag" 
    android:layout_weight="2" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" > 
</FrameLayout> 

</LinearLayout> 

這是我的主要活動: 公共類MainActivity擴展活動{

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    DateListFragment fragment = new DateListFragment(); 
    getFragmentManager().beginTransaction().add(R.id.list_frag, fragment).commit(); 
    fiche_freg frag2 = new fiche_frag(); 
    getFragmentManager().beginTransaction().add(R.id.fiche_frag,frag2).commit(); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

這裏是DateListFragment(無onCreateView,因爲它是自動生成的)

public class DateListFragment extends ListFragment { 
private int mposition = 1; 
private String[] mListItem = new String[] { 
     "Lundi 9 Juilllet", 
     "Mardi 10 Juillet", 
     "Mercredi maintenant" 
}; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    this.setListAdapter(new ArrayAdapter<String> 
        (this.getActivity(),R.layout.frag_list_view ,mListItem)); 
    this.getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
} 

這裏是fiche_frag: public class fiche_frag延伸片段{

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    return inflater.inflate(R.layout.checks_matin,container,false); 
} 

R.layout.checks_matin獨自運作良好。

我已經再次感謝您的幫助。我是Android環境的初學者,我發現很難將UI的每個概念一下子放在一起...... !!

+0

如果您要使用活動將碎片放入您的應該從xml中刪除'android:name'標記。 – Barak 2012-07-13 15:22:37

+0

我也會在片段中使用'onActivityCreated'而不是'onCreate'。 – Barak 2012-07-13 15:30:19

+0

指向[Fragment文檔](http://developer.android.com/guide/components/fragments.html#UI)的鏈接可能會有所幫助。 – Barak 2012-07-13 15:33:04

回答

1

您瞭解基本知識。 FrameLayout去你想要片段去的任何地方。我已經完成了,之前我的整個屏幕是一個單獨的FrameLayout,我換了五個不同的片段進出它。

如果您有兩個要同時顯示的片段,則可以使用兩個FrameLayout s進行主佈局。然而,這意味着你被鎖定在那裏所有的時間(或者如果你刪除一個空的空間)

如果你想要兩個片段不必在屏幕上同時你使用實例片段單FrameLayout並根據需要編寫代碼來交換片段。

代碼應始終處於控制活性(如果它們是動態的)。

沒有代碼和一個更具體的問題,上面是最好的答案我可以給你。

編輯

一例的主要佈局把兩個片段並排:

<?xml version="1.0" encoding="utf-8"?> 
... 
    <LinearLayout 
     android:id="@+id/frames" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/hline1" 
     android:layout_below="@id/horizontalline" 
     android:orientation="horizontal" > 
     <FrameLayout 
      android:id="@+id/leftpane" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight=".4" /> 
     <FrameLayout 
      android:id="@+id/rightpane" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="1" > 
     </FrameLayout> 
    </LinearLayout> 
... 

要將片段添加到framelayouts之一:

FragmentClass fragment = new FragmentClass(); 
getFragmentManager().beginTransaction().add(R.id.leftpane, fragment).commit(); 

如果你想在其中一個框架佈局(比如左側窗格)中交換片段,您可以這樣做:

FragmentClass fragment = new FragmentClass(); 
getFragmentManager().beginTransaction().replace(R.id.leftpane, fragment).commit(); 

我建議從XML實例化,因爲它聽起來像你將有兩個片段,並沒有做任何改變。如果你要交換它們,那麼爲每個標籤添加一個標籤是適當的,這樣如果你想再次顯示它們,你可以再次找到它們。

+0

那。所以,如果我總是想要這兩個片段,我只需創建一個由兩個FrameLayout組成的佈局(例如,一個xml文件中的linearlayout)。我使用這個佈局創建了一個主要活動,並在主要活動的onCreate/resume函數中添加了java代碼。我理解了嗎?好吧,我會測試它。非常感謝 !! – Nikkolasg 2012-07-13 12:39:07

+0

你已經明白了。 :)你也可以在框架佈局中使用'android:name =「your.package.name.fragmentclassname'(使用適當的不同片段類名),並且這會在佈局膨脹時自動實例化片段,所以你除了'setContentView'之外,不需要在主要活動中放置任何代碼 – Barak 2012-07-13 12:54:26

+0

哦,如果答案幫助你解決了問題,你應該接受它(點擊旁邊的複選標記)。每個人都知道你的問題已經解決,並給予幫助你的人一些功勞 – Barak 2012-07-13 12:55:22