SimonVT / android-menudrawer

*DEPRECATED* A slide-out menu implementation, which allows users to navigate between views in your app.
http://simonvt.github.com/android-menudrawer/
Apache License 2.0
2.59k stars 1.11k forks source link

ActionBarSherlock + Tabs + menu-drawer (Very Slow when switching between activities) #92

Closed ahmadalinasir closed 11 years ago

ahmadalinasir commented 11 years ago
public class MainActivity extends RoboSherlockFragmentActivity implements ActionBar.TabListener {
/**
 * Called when the activity is first created.
 */
SectionsPagerAdapter mSectionsPagerAdapter;
ActionBar actionBar;
/*@InjectView(R.id.pager) */ViewPager mViewPager;

//Menu Drawer
private static final String STATE_ACTIVE_POSITION =   "com.example.candroid.MainActivity.activePosition";
private static final String STATE_CONTENT_TEXT = "com.example.candroid.MainActivity.contentText";

private static final int MENU_OVERFLOW = 1;

private MenuDrawer mMenuDrawer;
private int mActivePosition = -1;
private MenuAdapter mAdapter;
private ListView mList;
private String mContentText;
private TextView mContentTextView;

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

    mMenuDrawer = MenuDrawer.attach(this, MenuDrawer.MENU_DRAG_WINDOW, Position.RIGHT);
    mMenuDrawer.setContentView(R.layout.main);

    if (savedInstanceState != null) {
        mActivePosition = savedInstanceState.getInt(STATE_ACTIVE_POSITION);
        mContentText = savedInstanceState.getString(STATE_CONTENT_TEXT);
    }
    actionBar = getSupportActionBar();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.actionbar_background);
        bg.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        actionBar.setBackgroundDrawable(bg);
    }

    configureActionBar();

    // For each of the sections in the app, add a tab to the action bar.

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by the adapter.
        // Also specify this Activity object, which implements the TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }

    List<Object> items  = StaticMethod.getOverFlowMenuList();

    // A custom ListView is needed so the drawer can be notified when it's scrolled. This is to update the position
    // of the arrow indicator.

    mList = new ListView(this);
    mAdapter = new MenuAdapter(items,mMenuDrawer,mActivePosition,MainActivity.this);
    mList.setAdapter(mAdapter);
    mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mActivePosition = position;
            mMenuDrawer.setActiveView(view, position);

            //Toast.makeText(getApplicationContext(),position+"",15).show();

                switch (position){
                    case 1:

                        mMenuDrawer.closeMenu();
                        break;

                    case 2:
                        mMenuDrawer.closeMenu();
                        Intent intent = new Intent(MainActivity.this,NewsActivity.class);                   
                        startActivity(intent);
                        break;
                    default:
                        mMenuDrawer.closeMenu();
                }

        }
    });
    mList.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            mMenuDrawer.invalidate();
        }
    });

    mMenuDrawer.setMenuView(mList);

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(STATE_ACTIVE_POSITION, mActivePosition);
    outState.putString(STATE_CONTENT_TEXT, mContentText);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getSupportMenuInflater().inflate(R.menu.menuoverflow,menu);

    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menu_overflow:
            mMenuDrawer.toggleMenu();
            return true;

    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    final int drawerState = mMenuDrawer.getDrawerState();
    if (drawerState == MenuDrawer.STATE_OPEN || drawerState == MenuDrawer.STATE_OPENING) {
        mMenuDrawer.closeMenu();
        return;
    }

    super.onBackPressed();
}

private void configureActionBar() {
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

}

@Override
public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
    //To change body of implemented methods use File | Settings | File Templates.
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    String[] locations = getResources().getStringArray(R.array.locations);

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {

            case 0:
                return new CurrentFragment();
            case 1:
                return new RecentFragment();
            case 2:
                return new UpcomingFragment();
            default:
                return null;               //TODO replace null by some default fragment
        }

    }

    @Override
    public int getCount() {
        return locations.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return locations[position];
    }

}

}

ahmadalinasir commented 11 years ago

By slow I mean glitches... and actionBar and tabs are also loaded after a certain delay.

SimonVT commented 11 years ago

Please provide a full project containing a test case.

ahmadalinasir commented 11 years ago

sure thanks just give me a couple of days.. can you provide an email id where I can send you the project archive or send you a link through mediafire ?

SimonVT commented 11 years ago

You can just link it here

On Mon, Mar 18, 2013 at 7:31 PM, ahmadalinasir notifications@github.comwrote:

sure thanks just give me a couple of days.. can you provide an email id where I can send you the project archive or send you a link through mediafire ?

— Reply to this email directly or view it on GitHubhttps://github.com/SimonVT/android-menudrawer/issues/92#issuecomment-15072666 .

ahmadalinasir commented 11 years ago

http://www.mediafire.com/download.php?l7avo31628y01l6

ahmadalinasir commented 11 years ago

I use intelliJ, I hope that won't be of any problem...

SimonVT commented 11 years ago

Starting activities is expensive, you shouldn't do that while the drawer is animation. In fact, you probably shouldn't touch the ui at all while the drawer is animating. You can either start the new activity before or after you animation the drawer, but not during.

ahmadalinasir commented 11 years ago

@SimonVT is there any flag which I can use ? I have used menuDrawer.isVisible(), menuDrawer.isActivated, menuDrawer.isShown() ... none of them worked .. help me out on this one please..