2012-07-13 63 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