Android jfeinstein10/SlidingMenu's animation is not smooth when changes fragment - android

I use jfeinstein10/SlidingMenu as my slidingmenu for my app and the slidingmenu will change different fragment for a activity.When the fragment contains only one or two items it seems good.But if I tried to make the fragment contains more thing ,the slidingmenu will be a short stop when I change the fragment.The animation is not smooth
Is there anyway to slove the problem?Thanks.

I experience same animation lag when I'm showing heavy fragment.
I tried to close menu with animation and show fragment with animation at same time. The animation was laggy, even with native drawer.
So the way to go is:
set menuOnClose listener
on menu item click remember the choice
close menu
in onCloseListener show fragment.
that works great for me.

I have a somewhat clunky solution for this but it works okay.
I use a base class for my fragments which is called BaseFragment. In this class I override the onResume method and close the menu if it is showing:
#Override
public void onResume() {
super.onResume();
if(sm.isMenuShowing()){
sm.showContent();
}
}
sm is a reference for a SlidingMenu from the SlidingFragmentActivity.
This will make the animation smoother but it will create a small delay between the click on a menu button and the start of the close animation. In my case this worked better then when the animation stops mid-animation.

Related

Android: Smooth DrawerLayout animations on Fragment Change

I'm working on an Android app that uses a navigation drawer and multiple navigation menu items. Inflating the views and running onCreate() takes some time, which makes the drawer close animation very laggy. I'm already working on optimizing the fragment layout itself to reduce jank, but the animation is still laggy.
My quick fix was adding a drawer listener and calling getSupportFragmentManager().beginTransaction().replace(...).commit() on drawer close. However the problem with this is that the previous fragment still remains in the UI for a few milliseconds after the drawer is closed, which looks arguably worse than with the laggy animation.
How is it possible to ensure a smooth drawer close animation and concurrently load the new fragment - or is there another way to make the UI transitions look more natural?

What should I do to a get smooth navigation drawer close on navigation item click

I have used basic navigation drawer generated in android studio. I noticed my navigation slider slides off smooth and lag sometimes. I found that the lag cause of the LayoutInflater I used to inflate another layout on my fragment.
What I have done is,
ViewGroup myLayout = view.findViewById(R.id.current_layout);
View mayLayoutTwo = LayoutInflater.from(CurrentFragment.this.getActivity()).inflate(R.layout.cardview_to_inflate, myLayout, false);
myLayout.addView(mayLayoutTwo);
I want to know is that possible to avoid that lag on navigation drawer slide. Help me to fix this issue.
Navigation drawer with multiple fragments is common usage. So based on your fragments loading at onCreate() lifecycle stage, your Naviagtion drawer may not smooth while closing.
So solution 1 :
Avoid heavy loading at your fragment initiate state. (e.g loading data asynchronously.)
Solution 2 :
Display place holder view (e.g whit screen) while navigation drawer is closing. Once NV drawer is closed, show your fragment.
You can use animation for fragment transition to give the better UX, since the default one looks some kinda awkward transition so giving your own transition will make the user feel some more smoothness, and yes avoid making the larger async calls on initiating the fragment.

How to update the ActionBar Spinner when pressing the back button

I'm going mad soon, been reading Google search results for 10+ hours soon, without any luck.
I think i will drop this whole idea of using a spinner and just use tabs, but it still irritates me that i haven't found a solution. So i hope anyone could help me to understand this issue.
The project im creating has one MainActivityContainer (the main FrameLayout), and than multiple fragments (different layouts) that gets first added and than replaced inside the MainActivityContainer.
So the app only opens ONE activity and than changes pages through different fragments. This makes it super fast to change pages.
Everything except for the ActionBar Spinner works great. It doesnt get updated when pressing the back button, i.e it is still displaying the value for the last fragment.
I want to use the spinner so i easily can navigate between different fragments.
The app start page look something like this:
http://1drv.ms/1jkJpy2
The spinner items are:
1. home , 2. ImageBtn1, 3. ImageBtn2, etc.
So i can reach each destination by either clicking the image button or using the spinner. This is the functionality that i want.
But if i click imagebutton (instead of using the spinner) the value in the spinner (home) does not change to ImageBtn1. So even though i am on the page for ImageBtn1 the spinner shows "Home".
I fixed this by using actionBar.setSelectedNavigationItem(1); in the method for the ImageButton. So imagebuttons uses the spinners methods manually. This sets the spinner to the correct value even if pressing the imagebutton on the start page.
This all works well until i press the back button. Than i have the same problem again, the spinners value doesnt update.
So please explain to me how i can solve this. In other words: How to use a spinner actionbar with ONE activity and many fragments and still get the back button to update the spinner. I have began experimenting with the onBackPressed() method. But there has to be an easier way to achieve what i want to do???
Ok I figured out how to update the navigation spinner in the action bar!
In my problem, I always called actionBar.setSelectedNavigationItem(int); inside the onCreate() method of the activity. However, onCreate() is only called when first making a new activity. So whenever I pressed the back button, the onCreate() doesnt get called and the navigation spinner doesn't get updated. However, the onResume() function always gets called when an activity becomes visible to the user (whether the activity was just created or whether the user pressed the back button into it). So I simply called actionBar.setSelectedNavigationItem(int) in onResume() and the navigation spinner now updates whenever the user presses back!.
However, since you are using one activity with multiple fragments, you should probably put your actionBar.setSelectedNavigationItem(int) inside the onResume() of your fragments instead of the activity. And that should work!
I guess this is a good case for really learning and internalising the activity and fragment lifecycles. Back to more learning for me :).
I was wondering if you managed to find a solution? I have a similar problem to you.
In the activity, inside the onBackPressed() method, try calling getSupportFragmentManager().findFragmentByTag(TAG); where TAG is a string that you can give when adding each of the frames (have a different TAG for each fragment).
So in code, when you are putting the fragment in:
getSupportFragmentManager().beginTransaction().add(R.id...., new MyFragment(), "TAG1");
Then the onBackPressed method would look like:
#Override
public void onBackPressed() {
MyFragment myFragment = (MyFragment)getSupportFragmentManager().findFragmentByTag("TAG1");
if (myFragment.isVisible()) {
getActionBar().setSelectedNavigationItem(int);
}
}
Let me know if that works!
NEVER use NAVIGATION_MODE_LIST and onNavigationItemSelected it is not worth it !
#Override
public boolean onNavigationItemSelected(int position, long itemId)
You also cannot use menu's to do this:
#Override public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
due to inflation ordering.
Reasons:
(1) it generates a "hidden" spinner, for which you can not get the id.
(2) you cannot customize this spinner
(3) you save 30 lines of code, but are permanently limited if you want to add a second bi-directional spinner
(4) not even in the special case of "simple code" (one spinner), you lose to much.
(5) you cannot use tabs.
(6) without the id, you have no chance with fragments.
the key is actionBar.setCustomView(R.layout.action_bar_custom);
and spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()...
for each spinner.
Trust me I lost hours trying each solution.

Navigate up to last shown fragment

I've implemented a NavigationDrawer within the MainActivity which containts two Fragment. The second Fragment contains a ListView that opens a new Activity (onItemClick) that shows the according detailed data. (I guess that's pretty much the master/detail flow).
When I use the up button to navigate back I got shown the first fragment and not the second fragment with the ListView.
Any ideas how to solve that problem?
Make method in MainActivity for example setFragment(int whichFragment);
and set fragment you want in it, you should already have code that do that and than call that method in onBackPressed() method.
For your question about another fragment, well it depends on how your master/detail flow is suppose to work, it is not a problem to use another activity if you dont need left menu any more, but if you do need left menu then use another fragment.
Best regards

android fragment fires itself when using tabs

I have this Android architecture:
Activity that has an action bar with two tabs.
each tab is a fragment
my problem is, when i start the activity, the two tabs start working.
I don't want the second tab to start working unless i click on it.
How please?
note that i am using onCreateView on my second fragment
note
i tried to make my functions on onResume() but still fires itself
This comes from caching of the ViewPager : it will initiate the visible fragment and one on each side (left and right). There is no thing you can do against that. The method setOffscreenPageLimit will refuse a value below 1.
So the solution is to fire your own even to actually "start" your fragment when it becomes visible. Don't use the fragment lifecycle per say, but add a custom method : startBeingVisible().
To fire this custom event, you can just use an OnPageChangeListener.

Categories

Resources