I want to have a fragment for each item in a listview, because I want to separate some logic out. I am using a view holder for each item. If the view doesn't exist, I create a new fragment and add it into the container.
holder.mMyFragment = new MyFragment(mActivity, this);
mActivity.getSupportFragmentManager().beginTransaction().add(R.id.my_container, holder.mMyFragment).commit();
Also for each item, I call holder.mMyFragment.setUi(dataSource, position) to set UI of the fragment based on the data source and position.
The problem I'm having is I initialize the UI elements of fragment in onCreateView method of the fragment class, but it's not called when I add the fragment to the item. So later when I call setUi() which uses some UI elements in fragment it complains a NullPointerException. Does anyone have a suggestion? Thanks!
A simple way. One problem:You should store add restore fragment state.
"THERE IS A SOLUTION" for this.
The issue is, you cannot add fragment directly to the container(FrameLayout) with same "id" in listview as you have done in the above code.
The trick is, create listview(Recyclerview) of "LinearLayout". Then dynamically create FrameLayout in adapter and assign different id's for each. Inflate Fragment to FrameLayout and this FrameLayout to LinearLayout.
You can't use fragment as list item views because the API doesn't allow you -
View
andFragment
aren't even related so there's no way you can use it like that. Make custom views and use adaptergetViewTypeCount
andgetView
to use different list item behavior.Fragment are managed by Activity's FragmentManager or by other Fragments child FragmentManager; while list item views are managed by ListView & ListAdapter. You can use ListViews in Fragments, but not the other way around.
Hi I was facing the same problem and I found the way to do it.
My problem was similar to you:
In my app I have to give the option to display custom items in vertical (listView) and horizontal (ViewPager) mode. Additionally I had to deal with 18 custom items and each one with different logic, so the best approach was reusing the fragments that I was using for ViewPager in ListView.
I got it but not in the way you were trying, I mean, I used my fragments like "ViewHolders":
getViewTypeCount(), getItemViewType(int position), getCount(), getItem(int position) getView(int position, View convertView, ViewGroup parent)
In
getView
I checked what kind of layout I needed before "inflate" the respective XML, create a fragment, assign widget from XML to fragment (withrootView.findViewById
) and set "tag" with the new fragment.What you can see at this point is that fragments in ListView never got attached to Activity but you got what you wanted: logic distributed in several parts and all benefits of ListView (reuse of rows, scroll, etc).
If you need I can post part of my code but you have to deal with "spanglish" ;).
UPDATED
All the problem is because when you create a Fragment to be used with ViewPager, usually all "layout and "setup" code is inside
onCreateView
method, I mean:View rootView = inflater.inflate(R.layout.fragment_question_horizontal_container, container, false);
)answer = (EditText)rootView.findViewById(R.id.answer_question_text);
)Until this point there is nothing weird.
If you are going to use a fragment with the behavior described above you have to "emulate" the call to onCreateView, fill the data and attach it to the listView.
Here is the trick: split the code in
onCreateView
in some methods that doesn't care about who's calling them. An example of myonCreateView
:Now let's go to the CustomArrayAdapter for list view:
PreguntasVerticalArrayAdapter extends ArrayAdapter<Pregunta>
where "Pregunta" is a generic Fragment with the logic from above.getViewTypeCount(), getItemViewType(int position), getCount(), getItem(int position) getView(int position, View convertView, ViewGroup parent)
.The
getView
follow the same behavior: get the object for the given position in params, reuse a "viewholder" and fill the data. Here mygetView
:That is all... at this point, if you have enough experience dealing with CustomArrayAdapters and Fragments you probably got it! :D
From the Android Documentation : "A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities."
For your activity, do you want to add 2 fragments where the first one displays a listView (= ListFragment and the other one is in the right and is shown only when the user clicks on an item (from the first fragment or listView) ?