It's impossible. A fragment cannot dynamically update it. However, you have other options.
1. Not a fan of this, but you can have a mock fragment with both a portrait and horizontal views at the same time both show and hide.
fragment_library.xml:
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview_portrait" android:cacheColorHint="@android:color/transparent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="200dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="20dp" android:stretchMode="columnWidth" android:gravity="bottom" /> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview_landscape" android:cacheColorHint="@android:color/transparent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="400dp" android:numColumns="2" android:verticalSpacing="50dp" android:horizontalSpacing="50dp" android:stretchMode="columnWidth" android:gravity="bottom" android:visible="gone" />
Then some private member variables:
private GridView mGridViewPortrait; private GridView mGridViewLandscape;
Then in onConfigurationChanged(Configuration newConfig) :
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { mGridViewPortrait.setVisibility(View.VISIBLE); mGridViewLandscape.setVisibility(View.GONE); } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { mGridViewPortrait.setVisibility(View.GONE); mGridViewLandscape.setVisibility(View.VISIBLE); } }
Some points: note that I did not use the code to reference both GridViews. I also changed my GridView to private, and also changed the name to mGridView *. Private, so that he is "encapsulated" and "m" because he is a member of the class, he simply agrees. I also changed the if-else clause because I wanted the portrait check to be done first.
This method is the fastest and easiest, but it can be difficult for the system if you have large layouts, so do not use it if you have a lot of things. It is preferable not to use this approach at all.
2. The right way is to let Andorid take care of the orientation, as well as move your XML to the correct directory. This, however, recreates your fragment (if you do not set setRetainInstance(true); which you will not in this case, it will cause the Fragment not to recreate its layout (actually looking at the save method it does not mention onCreateView , so you can try to set this value as true, but try)).
Move the fragment_library_land.xml file to the layout mockup of the directory instead of the layout and name it fragment_library .xml. Pay attention to the bold font, it will have the same name, but will remain in different directories. In this way, Android will recognize and accept the correct orientation-based layout.
If I understand why you donβt want to recreate the fragment, because onCreate(Bundle savedInstanceState) will be called again (with setRetainInstance(true); it wonβt be and with respect to what I wrote earlier, you can try), thus creating a new GetLibraryTask instance and reload the images. This can be prevented if you used a database to store images, and if you had a logical value that was tracked if you uploaded the image or not. In GetLibraryTask you select images that do not load, whether it is the first time you run the task or the orientation changes. You would also need to put a completion check in the library task in the load cycle before each element checks whether the image needs to be loaded or if the fragment is no longer available and thus exit the task.
Now, when you change the orientation, the Activity will recreate the LibraryFragment, and depending on the orientation, a layout or layout will be used.
Some side notes in your code:
- As I wrote earlier, never use public access, always use private or secure when necessary. Private ones can be used all the time, although they have getters and setters (accesors and mutators) for communication.
- Use "m" as a prefix for member variables, in this case
public GridView gridview will be private GridView mGridView and private Boolean isImageAdapterPopulated will be private boolean mIsImageAdapterPopulated - Never use classes for primitive types if you do not need them. You may need this on lists that do not support primitive types or class retention, etc.
- In your
onConfigurationChanged(Configuration newConfig) you are inflating the XML and it will return the view, but you are not doing anything with it.
I wish you good luck!