androidandroid-layoutslidingmenujfeinstein

Android SlidingMenu lag at first use


I am experiencing some problems with the slidingmenu library from jfeinstein10: https://github.com/jfeinstein10/SlidingMenu

The problem I observe is that at first use, when the menu opens, it lags showing some black space. Then when I close it and reuse it again, everything is smooth, nothing to declare.

I basically have an Activity extending the SlidingFragmentActivity, and 2 fragments. One for the left menu and another one for the main content.

Here is how I implemented it, I'm investigating this but I really don't know what I've missed:

public class CavendishSlidingFragmentActivity extends SlidingFragmentActivity {

private final String TAG = CavendishSlidingFragmentActivity.class.getName();

protected FragmentManager fragmentManager;
protected Fragment mContent;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    fragmentManager = getSupportFragmentManager();

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    getSlidingMenu().setBehindOffset((int) (metrics.widthPixels * 0.2));
}

public void onClickMenu(View v)
{

    if (getSlidingMenu().isMenuShowing())
        getSlidingMenu().showContent();
    else
        getSlidingMenu().showMenu();
}

public void onClickMenuItem(View v)
{

    Long position = (Long) v.getTag();
    switch (position.intValue())
    {
        case Utils.CAT_RESTAURANT:
            showFragment(new RestaurantFragment());
            break;
        case Utils.CAT_SLEEP:
            showFragment(new SleepFragment());
            break;
        case Utils.CAT_DISCOVER:
            showFragment(new DiscoverFragment());
            break;
        case Utils.CAT_TRANSPORT:
            showFragment(new TransportFragment());
            break;
    }
}

private void showFragment(Fragment fragment)
{
    if (mContent != fragment)
    {
        mContent = fragment;
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_fragment, fragment);
        fragmentTransaction.commit();
    }

    getSlidingMenu().showContent();
}
}

MainActivity.java

public class MainActivity extends CavendishSlidingFragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setBehindContentView(R.layout.fragment_menu);
    setContentView(R.layout.fragment_main);

    // set the Above View Fragment
    if (savedInstanceState != null)
        mContent = fragmentManager.getFragment(savedInstanceState, "mContent");
    if (mContent == null)
    {
        mContent = new RestaurantFragment();
    }

    fragmentManager
            .beginTransaction()
            .replace(R.id.main_fragment, mContent)
            .commit();
}

@Override
public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    fragmentManager.putFragment(outState, "mContent", mContent);
}

}

I can provide further code but I think the issue might be here, since the menu is drawing itself at first use, causing the lag. I believe I have to force the menu to be drawn before showing it.


Solution

  • Looks like there is something wrong with the library when rendering the menu while it's being opened. My design didn't fit the library I guess, well I just passed to the default Android navigation drawer. Bit different in behavior but the design remains unchanged, works fine for me.