Desired Result
I want to have a vertical list with custom elements on the left or right of the screen in landscape mode and a horizontal list at the top / bottom of the screen in portrait mode. The Horizontal / Vertical list should be Fragment , so I can reuse it later for the smartphone version. The minimum version of the SDK is 13 (Android 3.2).
My attempt
My custom Activity has one custom LayersFragment and another View . In portrait mode, the fragment is aligned with the parent left. In landscape mode, aligned to the bottom of the parent.
LayersFragment also has a different layout for portrait and landscape mode. In portrait mode Gallery and in landscape mode ListView .
Since Gallery and ListView are subclasses of AdapterView<Adapter> , I use this parent class and BaseAdapter to populate items and listen to OnItemClicks .


Resource Information
frag_layers.xml - XML layout for LayersFragment in the landscape.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
frag_layers.xml - XML layout for LayersFragment in portrait mode.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Gallery android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
activity_photo_editor.xml - An XML layout for my custom Activity in portrait mode. The layout for landscape mode instead of android:layout_alignParentBottom has android:layout_alignParentLeft .
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/photo_editor_layouts" class="rs.ailic.android.heritage.ui.LayersFragment" android:layout_width="match_parent" android:layout_height="@dimen/photo_editor_layouts_size" android:layout_alignParentBottom="true" /> </RelativeLayout>
Code Details
Class LayersFragment .
public class LayersFragment extends Fragment implements OnItemClickListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.frag_layers, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mLayersAdapter = new LayersAdapter(); mLayersView = (AdapterView<Adapter>) getView().findViewById(android.R.id.list); mLayersView.setOnItemClickListener(this); mLayersView.setAdapter(mLayersAdapter); } @Override public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
My user activity
public class PhotoEditorActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photo_editor); }
Problem
I get this ClassCastException when turning from Landscape to portrait (ListView → Gallery)
Caused by: java.lang.ClassCastException: android.widget.AbsListView$SavedState cannot be cast to android.widget.AbsSpinner$SavedState at android.widget.AbsSpinner.onRestoreInstanceState(AbsSpinner.java:421) at android.view.View.dispatchRestoreInstanceState(View.java:8341) at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:2038) at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:766) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2024) at android.view.View.restoreHierarchyState(View.java:8320) at android.app.Fragment.restoreViewState(Fragment.java:583) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:801) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:977) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:960) at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1679) at android.app.Activity.performStart(Activity.java:4413) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1791) ... 12 more
and this one, when turning from "Portrait in a Landscape" (Gallery → List)
Caused by: java.lang.ClassCastException: android.widget.AbsSpinner$SavedState cannot be cast to android.widget.AbsListView$SavedState at android.widget.AbsListView.onRestoreInstanceState(AbsListView.java:1650) at android.view.View.dispatchRestoreInstanceState(View.java:8341) at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:2038) at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:766)
How can I solve this problem or look for another solution?
My opinion
The problem occurs when the screen orientation changes. I believe the problem is the "default implementation" of ListView and Gallery . They will try to restore their SavedState to onRestoreInstanceState after changing orientation, but View has changed and a ClassCastException is thrown.
Thanks,
Aleksandar Ilyich