I have a 2 pane view that is defined in XML. For xlarge, the left side of the view is a Fragment that deals with content selection. I want to initialize this selector ListFragment to have the first item highlighted on app start up.
I’ve written a method in the ListFragment to do this:
public void setSelectedItem (int position) {
//Unselect the previous item
if(mPreviousPosition != ListView.INVALID_POSITION) {
View oldSelected = getListView().getChildAt(mPreviousPosition);
oldSelected.setBackgroundResource(R.drawable.menu_button);
((ImageView) oldSelected.findViewById(R.id.selector_icon)).setImageDrawable(icons.getDrawable(mPreviousPosition));
((TextView) oldSelected.findViewById(R.id.selector_text)).setTextAppearance(getActivity(), R.style.text_selector);
}
//Select this item
View newSelected = getListView().getChildAt(position);
newSelected.setBackgroundResource(R.drawable.menu_button_active);
((ImageView) newSelected.findViewById(R.id.selector_icon)).setImageDrawable(icons_active.getDrawable(position));
((TextView) newSelected.findViewById(R.id.selector_text)).setTextAppearance(getActivity(), R.style.text_selector_active);
mPreviousPosition = position;
}
When I call this after an item has been selected, it works great. But when I try highlight the first item on startup, I get a NPE at
newSelected.setBackgroundResource(R.drawable.menu_button_active);
It seems getListView().getChildAt(position) is returning null because the Fragment’s view hasn’t been created yet.
So, from where in my Activity is it safe to try initialise the ListFragment? I’ve tried in onStart, onRestoreInstanceState and onResume but they all produce the NPE.
From what I understand you have two panes (left pane is ListFragment) and these two fragments are held within a FragmentActivity. The fragments are being set in the xml layout for the FragmentActivity. The fragments are available to the FragmentActivity following the onAttach() cycle but this doesn’t necessarily mean that the view elements in the child fragment are available yet.
First question is where are you trying to set the selected item? In the FragmentActivity or in the ListFragment? It should be handled in the ListFragment in the onResume() method.
I take it that based on the selection in the ListFragment you are then populating the right fragment with data for the selection. You will probably want to look at this link which will give you a good example of using listeners to avoid dependancies between the fragment activity and underlying fragments
http://developer.android.com/training/basics/fragments/communicating.html
You can then use listeners to handle the selection in the ListFragment and pass the selection to the right fragment. In the right fragment you can check for a selection in the FragmentActivity as well as register the fragment to listen for selections. This way you should cover the situation where the selection is fired before the right fragment has registered as a listener.
UDPATE
Look at responsive layout design for further information but here are some starting points.
Then inside the bools.xml file define a value for isDualPane
Then in your code in the listFragment you can then reference bools resource and know what the layout is.
You also have the benefit of using this in any class to know what the layout is and without needing to create dependancies between classes.
Here are some links that might help.
http://developer.android.com/training/multiscreen/screensizes.html
http://developer.android.com/training/multiscreen/adaptui.html