This diagram illustrates the complexity of my activity. There are multiple (3 in this case) layers of fragments that need to load dynamically. The repeating fragments are loaded using LinearListView, a view library I found here: https://github.com/frankiesardo/LinearListView. This allows the list to load like a ListView, but avoids the problems of having a ListView inside a ScrollView.
Here is some of the sample code:
line_item_list.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:id="@+id/line_item_list">
<TextView
android:layout_width="171dp"
android:layout_height="wrap_content"
android:text="LineItem List"
android:id="@+id/line_item_list_text" />
<com.linearlistview.LinearListView
android:id="@+id/line_item_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#f00"
android:orientation="vertical"
android:showDividers="middle"
app:dividerThickness="16dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"/>
</LinearLayout>
line_item.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:id="@+id/line_item">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="my line item."
android:id="@+id/line_item_text"
android:layout_gravity="center_horizontal" />
<!-- notes child items go here -->
</LinearLayout>
LineItemFragment.java loads the fragment with an adapter (lineItem in this code is displayed with the word, "Part" in the illustration above.)
public class LineItemFragment extends Fragment {
LineItemAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearListView lineItems = (LinearListView)container.findViewById(R.id.line_item_wrapper);
adapter = new LineItemAdapter(this.getContext(), getChildFragmentManager());
lineItems.setAdapter(adapter);
return null;
}
}
LineItemAdapter.java
public class LineItemAdapter extends BaseAdapter{
ArrayList<String> lineItems = new ArrayList<String>();
Context context;
FragmentManager fm;
public LineItemAdapter(Context context, FragmentManager fragmentManager) {
this.context = context;
lineItems.add("Item A");
lineItems.add("Item B");
lineItems.add("Item C");
this.fm = fragmentManager;
}
@Override
public int getCount() {
return lineItems.size();
}
@Override
public Object getItem(int position) {
return lineItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.line_item, parent, false);
}
((TextView)convertView.findViewById(R.id.line_item_text)).setText(getItem(position).toString());
// *** The getView method could load the child fragments
return convertView;
}
}
In this previous file (***), I am currently thinking the getView method should load the next level child fragment, but everything I have tried has not worked. It appears that the layout is not yet inflated, and so I cannot add child views to it.
My question is, "how do i add child fragments from within the getView?" Or maybe I am going about this wrong.
Here is one example of what I have tried:
getView Method (***) in lineItemAdapter.java:
.
.
.
LinearLayout notes = (LinearLayout) ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.line_item, parent, false);
LinearLayout b = (LinearLayout)((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.note_list, notes).getRootView();
NoteListFragment noteListFragment = new NoteListFragment();
fm.beginTransaction().add(b.getId(), noteListFragment).commit();
.
.
.
And I got an exception like this.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 12207
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.myapplication/com.example.myapplication.DetailActivity}: java.lang.IllegalStateException: Fragment does not have a view
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
.
.
.
Thanks in advance.
EDIT
Do I need to add a fragment to the xml, i.e.:
line_item.xml...new node:
<fragment android:name="com.example.noteListFragment"
android:id="@+id/note_list_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />