Snippets inside an Android fragment app

I am trying to put 2 fragments inside a fragment. I found some code on the Internet, but as far as I could, I was unable to place 2 fragments in 1 fragment. I saw tips regarding the FragmentManager and especially the getChildFragmentManager () method, but I don’t know how it works with 2 fragments.

For the story, I use an action with an ActionBar that creates 3 fragments. In one of them I need to process the chart and a kind of menu to change the scale of the chart. So I need 2 fragments in one fragment.

Here is the code:

A fragment that processes others:

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; } } 

Code for drawing a graph:

 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 files:

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 with test implementation

 <?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> 

The strange thing is that it works at the beginning when I switch between fragments in an ActionBar, but after 3-4 moves I get this error:

 android.view.InflateException: Binary XML file line #7: Error inflating class fragment 

If anyone has a solution, it will be awesome!

+4
source share
3 answers

So first change your xml to graph_fragment.xml to this

 <?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> 

Then in code, to inflate them from a fragment, use something like this

 @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; } 
+2
source

MyFragment myFragment = (MyFragment) getChildFragmentManager (). findFragmentById (R.id.my_fragment);

+1
source

You can try to use the * findFragmentById (fragment_id) * method from SupportFragmentManager to get a fragment instance from your XML file and insert your fragment there.

Sort of:

 Fragment myFragment = getSupportFragmentManager().findFragmentById(R.id.myFragmentId); 

I hope to help you.

Sincerely. Adriano

0
source

Source: https://habr.com/ru/post/1492098/


All Articles