[英]Fragments inside a fragment Android application
我试图在片段中放入2个片段。 我在互联网上找到了一些代码但是,据我所知,我没有成功将2个片段放在1个片段中。 我已经看到了处理FragmentManager的提示,尤其是方法getChildFragmentManager(),但我不知道它如何与2个片段一起工作。
对于这个故事,我正在使用ActionBar的一个活动来创建3个片段。 在其中一个中,我需要处理一个图形和一种菜单来改变图形比例。 这样,我在一个片段中需要2个片段。
这是代码:
处理其他人的片段:
public class GraphDisplayFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.graph_fragment, container, false);
return myFragmentView;
}
}
绘制图形的代码:
public class GraphFragment extends Fragment {
private static final int SERIES_NR = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
GraphicalView myFragmentView = ChartFactory.getTimeChartView(this.getActivity(), getDateDemoDataset(), getDemoRenderer(),null);
return myFragmentView;
}
//some functions to set graph propreties
}
XML文件:
graph_fragment.xml
<?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" >
<fragment
android:id="@+id/graph_fragment"
android:name="com.test.GraphFragment"
android:layout_width="match_parent"
android:layout_height="259dp" >
</fragment>
<fragment
android:name="com.test.GraphDetailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/graph_detail_fragment">
</fragment>
</LinearLayout>
带有测试实现的graph_detail.xml
<?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="211dp"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
奇怪的是,当我在ActionBar中的片段之间切换但是在3-4次移动之后它开始工作时,我得到了这个错误:
android.view.InflateException: Binary XML file line #7: Error inflating class fragment
如果有人有解决方案,那就太棒了!
首先,将graph_fragment.xml的xml更改为此
<?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" >
<FrameLayout
android:id="@+id/graph_fragment_holder"
android:layout_width="match_parent"
android:layout_height="259dp" >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/graph_detail_fragment_holder">
</FrameLayout>
</LinearLayout>
然后在代码中从片段中膨胀它们使用类似的东西
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.graph_fragment,
container, false);
FirstChildFragment frag1 = (FirstChildFragment) getChildFragmentManager()
.findFragmentById(R.id.first_frag_layout);
SecondChildFragment frag1 = (SecondChildFragment) getChildFragmentManager()
.findFragmentById(R.id.second_frag_layout);
if (null == frag1) {
FirstChildFragment = new frag1();
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.add(R.id.graph_fragment_holder, frag1)
.addToBackStack(null).commit();
}
if (null == frag2) {
SecondChildFragment = new frag2();
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.add(R.id.graph_detail_fragment_holder, frag2)
.addToBackStack(null).commit();
}
return rootView;
}
MyFragment myFragment =(MyFragment)getChildFragmentManager()。findFragmentById(R.id.my_fragment);
您可以尝试使用SupportFragmentManager中的方法* findFragmentById(fragment_id)*从.xml文件中获取片段实例并在其中插入片段。
就像是:
Fragment myFragment = getSupportFragmentManager().findFragmentById(R.id.myFragmentId);
我希望能帮助你。
最好的祝福。 阿德里亚诺
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.