Last active
December 18, 2015 04:59
-
-
Save StevenByle/5729753 to your computer and use it in GitHub Desktop.
Android: Example showing layout dependent event handling. Taking different actions for phones and tablets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
Log.d(TAG, "onOptionsItemSelected"); | |
switch (item.getItemId()) { | |
case R.id.menu_rotate: | |
Log.i(TAG, "onOptionsItemSelected: rotate menu item selected"); | |
// This menu option is only provided to phone layouts, since | |
// tablet layouts show the image rotator at all times | |
// Get the parent activity's fragment manager | |
FragmentManager fragmentManager = getFragmentManager(); | |
// Create the image rotator fragment and pass in arguments | |
ImageRotatorFragment imageRotatorFragment = ImageRotatorFragment.newInstance(mCurImageResourceId); | |
// Add the new fragment on top of this one, and add it to | |
// the back stack | |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | |
fragmentTransaction.replace(mContainer.getId(), imageRotatorFragment, ImageRotatorFragment.class.getName()); | |
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); | |
fragmentTransaction.addToBackStack(null); | |
// Commit the transaction | |
fragmentTransaction.commit(); | |
return true; | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onImageSelected(ImageItem imageItem, int position) { | |
Log.d(TAG, "onImageSelected: title = " + imageItem.getTitle() + " position = " + position); | |
FragmentManager fragmentManager = getSupportFragmentManager(); | |
ImageRotatorFragment imageRotatorFragment = (ImageRotatorFragment) fragmentManager.findFragmentByTag( | |
ImageRotatorFragment.class.getName()); | |
// If the rotating fragment is in the current layout, update its | |
// selected image | |
if (imageRotatorFragment != null) { | |
imageRotatorFragment.setImageSelected(imageItem, position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment