How can I use chenupt/SpringIndicator in Fragment - android

I want to use it in a v4.Fragment to load childFragment, and the SpringIndicator sample code based on MultipleModel.
I tryed to deliver a ChildFragmentManager to ModelPagerAdapter in Fragment follow this:
ModelPagerAdapter adapter = new ModelPagerAdapter(getChildFragmentManager(), new PagerModelManager());
But that not works, nothing displayed on phone's screen.
Actually,The SpringIndicator will works in a Activity.(use getSupportFragmentManager as the arg)
How can I do?Thank you very much.

Sorry,everyBody.That code in question taked effect but not display beacause of background color.So, You can use SpringIndicator in Fragment to load childFragment with:
ModelPagerAdapter adapter = new ModelPagerAdapter(getChildFragmentManager(), new PagerModelManager());
The first arg is getChildFragmentManager() worked for me.

Related

FragmentTransaction adding fragment instead of replace

I'm building an QR Code Scanner using gms Vision API. The scanner is working properly, but as soon as I try to replace the fragment, it's not getting replaced, it's getting added.
Code to replace:
val manager: FragmentManager? = (context as MainActivity).supportFragmentManager
val transaction: FragmentTransaction? = manager?.beginTransaction()
transaction?.replace(R.id.container, ScannedFragment())
transaction?.addToBackStack(null)
transaction?.commit()
There are no error messsages. The app looks like this then:
Image
I found the solution myself. I had to use a FrameLayout to replace.

Android fragment `setEnterTransition` doesn't work

I'm trying to animate the showing of a fragment in a very basic way but it doesn't work:
MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentById(R.id.myFragment);
getFragmentManager().beginTransaction()
.hide(myFragment)
.commit();
Slide slideRight = new Slide(Gravity.RIGHT);
slideRight.setDuration(3000);
Slide slideLeft = new Slide(Gravity.LEFT);
slideLeft.setDuration(3000);
myFragment.setEnterTransition(slideRight);
myFragment.setExitTransition(slideLeft);
then later
getFragmentManager().beginTransaction()
.show(myFragment)
.commit();
But fragment just appears without any transition. I can't figure out why it doesn't animate.
Edit:
I ended up using the fragment support library with setCustomAnimations cause the native library doesn't support animation with relative values.
Also using the new API with setEnterTransition doesn't seem to work properly with show/hide and for some unknown reason worked only with replace which doesn't fit my needs.
setCustomAnimation will do the trick.
FragmentManager.beginTransaction()
.replace(R.id.fragment, new Fragment)
.setCustomAnimations(R.anim.slide_out_left, R.anim.slide_in_right).commit();

Use two osmdroid Map Views in one Activity

I want to use two Map Fragments, each with a different fragment, side-by-side in an activity. The left map is planned to show a overview over a specific area, the right one zooms in to a specific point.
The problem I'm having is, that only the first (left) map fragment is being correctly shown, the second (right) one shows a world map:
Zooming is not working on the right map.
Here is a part of the code I'm using:
private void initializeMap()
{
MapTileProviderBasic tileProviderOSM = new MapTileProviderBasic(getApplicationContext());
ITileSource tileSourceOSM = new XYTileSource("OpenFireMap", null, 1, 18, 256, ".png", new String[]{"http://openfiremap.org/hytiles/"});
tileProviderOSM.setTileSource(tileSourceOSM);
TilesOverlay tilesOverlayOSM = new TilesOverlay(tileProviderOSM, this.getBaseContext());
tilesOverlayOSM.setLoadingBackgroundColor(Color.TRANSPARENT);
OsmFragment frag = (OsmFragment) getFragmentManager().findFragmentById(R.id.osmMap);
osmMap = frag.getMap();
osmMap.setMultiTouchControls(true);
osmMap.getOverlays().add(tilesOverlayOSM);
MapTileProviderBasic tileProviderOSMDetail = new MapTileProviderBasic(getApplicationContext());
ITileSource tileSourceOSMDetail = new XYTileSource("OpenFireMap", null, 1, 18, 256, ".png", new String[]{"http://openfiremap.org/hytiles/"});
tileProviderOSMDetail.setTileSource(tileSourceOSMDetail);
TilesOverlay tilesOverlayOSMDetail = new TilesOverlay(tileProviderOSMDetail, this.getBaseContext());
tilesOverlayOSMDetail.setLoadingBackgroundColor(Color.TRANSPARENT);
OsmFragment fragDetail = (OsmFragment) getFragmentManager().findFragmentById(R.id.osmMapDetail);
osmMapDetail = fragDetail.getMap();
osmMapDetail.setMultiTouchControls(true);
osmMapDetail.getOverlays().add(tilesOverlayOSMDetail);
}
If I change either one of the fragments to a GoogleMaps fragment, the osmdroid map loads correctly.
Any ideas why this is happening and how I can fix it? I'm using the newest osmdroid version from gradle (v5.6.4)
Thanks in advance.
EDIT:
I changed the variable declation, but it still doesn't work.
You have to create separate TilesOverlays for different map views. Currently you are using the single overlay in two fragments, this can not work.
So I found the solution. The problem was, that the MapView is within a fragment and the fragment is a custom class which had the hardcoded ID's and layouts in it. The solution which worked for me was duplicating the custom fragment class and editing the ID and Layout. Then in MainActivity I had to change the varaible declaration and the cast from OsmFragment to OsmFragmentDetail
Now both maps are showing the wanted content.

How to display loading animation when transitioning between fragmens?

I am still in fragment, for display new fragment using the code:
CatalogFragment catalogFragment = new CatalogFragment();
var fragmentManager = Activity.SupportFragmentManager.BeginTransaction();
fragmentManager.Replace(Resource.Id.flContent, (SupportFragment)catalogFragment);
fragmentManager.AddToBackStack(null);
fragmentManager.Commit();
How to display loading animation (such as "animated circle" or gif) when transitioning between fragmens?
Sorry for my English.
For animated circle you should put a progress dialog before replacing the fragment. After that you should disable it in new fragment. But fragments can be animated natively. You should checkout this link. https://developer.android.com/training/animation/cardflip.html

How to use default animations in Android?

I'm trying to use the default animations for the Activity with fragments.. here I found something about it:
Android: using Activity's default animation for Fragments
the problem is: ok, I need (for example) "activityOpenEnterAnimation".. how can I use it?
Using the following code won't work:
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(android.R.anim.activityOpenEnterAnimation, android.R.anim.activityOpenExitAnimation);
transaction.replace(R.id.container, fragment)
.addToBackStack(((Object) fragment).getClass().getName())
.commit();
Hints? Thanks! :)
Nowadays, Android documentation clearly recommends not to use resources directly from android.R.*, since every release of the platform has changes on them. Even some resources dissapear from one version to another, so you shouldn't rely on them. On the other hand, lots of resources are private and not available from a developer's code.
The safest (and recommended) way is to simply copy & paste the resources you need (in this case, the animations) from the source code of the Android version you want into your own code, and use them through the regular R.*.
You can browse the Android source code in many ways, as explained in [1].
[1] Where can I find Android source code online?
example of using default android animation when back pressed happen
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
}
for using default animations you should use android.R.anim.ANIMATIONNAME
I managed to get it to work this way:
static public int getResourceIdFromCurrentThemeAttribute(FragmentActivity activity, int attribute){
TypedValue a = new TypedValue();
activity.getTheme().resolveAttribute(attribute, a, false);
return a.resourceId;
}
//This type of fragment will be opened like an activity
static public void openActivityLikeFragment(FragmentActivity activity, BaseFragment fragment, int containerId, String stackRef) {
FragmentManager fm = activity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
//The fragment open/close transition should have the same animations as its activity
ft.setCustomAnimations(
getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityOpenEnterAnimation),
getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityOpenExitAnimation),
getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityCloseEnterAnimation),
getResourceIdFromCurrentThemeAttribute(activity, android.R.attr.activityCloseExitAnimation)
);
ft.replace(containerId, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(stackRef);
ft.commit();
}
This solution is safer than directly referencing a resource, since its referencing an attribute which won't change without some deprecation warnings first.

Categories

Resources