I am trying to create a shared panel based on the sample code presented at the end of the Android documentation on fragments, mainly LinearLayout containing two fragments (all details can be found in the link above):
TitlesFragmentto display a list of namesDetailsFragmentto display information about the currently selected title
My problem is that the following unexpected behavior occurs:
- I launch the application in the Landscape orientation : the list of headers is displayed on the left, and the data of the currently selected header (the first one at the beginning) is displayed on the right.
- I select the X element from the list of titles: the title becomes highlighted, since the ListView matters
CHOICE_MODE_SINGLE, and its elements implement the interface Checkable. - I switch to portrait orientation (by rotating the device or emulator): as expected, only the list of titles is displayed, and no element is selected in this configuration, since the ListView is recreated and the default value is set
CHOICE_MODE_NONE. - I select the Y element from the list of titles: as expected, an action is displayed showing only the one
DetailsFragmentthat matches the selected selection. - (, ):
DetailsFragment Y ( ), TitlesFragment - X ( , ).
, , , , TitlesFragment, , X, Y.
, ListView ( 'onActivityCreated'), ( 'showDetails'):
public static class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
getListView().setItemChecked(index, true);
DetailsFragment details = (DetailsFragment)
getFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != index) {
details = DetailsFragment.newInstance(index);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
- , ?
.