I am trying to create an application that displays a large amount of text and images in a specific layout when the user clicks on the corresponding list item. Since I need specific .xml layouts for individual “sections”, I want only the fragment layout to change to the corresponding chapter.
Googling tells me that I can do this with fragments, but as I understand it, I need separate fragment classes and .xml layouts for each chapter I want to implement. With 2 or 3 sections that can be done, but with a large number of chapters that will become, as I thought, it is not easier to just save two fragments (one with a list and one with the text of the chapter), but dynamically change the layout of the second fragment if the user clicks per item in the list.
Can this be done with some code like this (just out loud)?
Int[] layouts = {
{R.layout.chapter1, R.layout.chapter2, R.layout.chapter3}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
index = intent.getIntent("index", 0);
return inflater.inflate(layouts[index], container, false);
}
Or is there any other way to achieve this?
source
share