2

When i switch between fragments in Navigation-drawer, Navigation-drawer closes slowly , i assume it waits to replace the newly selected fragment and closes the drawer,

How can i speed up it? In Gmail app, when you select a tab in Navigation-drawer,the new fragment is not created immediately,instead it shows a ProgressBar,then content is load,Also Navigation-drawer closing is speed here.

How can i do it, Any code example is appreciated.

Edit

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    .....

    navigationDrawerTitles = getResources().getStringArray(R.array.navigation_drawer_titles);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, navigationDrawerTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    .......

}

private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    switch(position){
    case 0:
        getFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new HomeFragment(), "HomeFragment").commit();
        mDrawerList.setItemChecked(position, true);
        setTitle(navigationDrawerTitles[position]);
        break;
    case 1:
        getFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new HistoryFragment(), "HistoryFragment").commit();
        mDrawerList.setItemChecked(position, true);
        setTitle(navigationDrawerTitles[position]);
        break;
    case 2:
        Intent sintent = new Intent(MainActivity.this,
                SettingsActivity.class);
        startActivity(sintent);
        break;
    default:
        break;

    }
    mDrawerLayout.closeDrawer(mDrawerList);

}

Thanks,

Nasrudeen
  • 425
  • 5
  • 18
  • post your code first – Narendra Kumar Oct 18 '14 at 05:45
  • if you do any heavy work on fragment started then also its happen.. – Sagar Maiyad Oct 18 '14 at 06:11
  • then how can i make async(separate thread to load fragment without disturbing Navigation-drawer functioning) like in Gmail app which shows ProgressBar in MainWindow (on clicking Navigation-drawer item )and then load the actual content in MainWindow. Any code please. – Nasrudeen Oct 18 '14 at 06:19

2 Answers2

0

I think this link will help you: Speed up Android Navigation drawer

public boolean smoothSlideViewTo(... int duration) {
    ...
    return forceSettleCapturedViewAt(... int duration);
}
Community
  • 1
  • 1
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • i dont need to increase its speed, i need to make Navigation-drawer not to struck when loading new fragment. Do i need to add `getFragmentManager().beginTransaction() .replace(R.id.content_frame, new HomeFragment(), "HomeFragment").commit();` code in separate thread? – Nasrudeen Oct 18 '14 at 06:06
0

You have to close drawer when you replace fragment like this:

if (drawerLayout.isDrawerOpen(Gravity.START)) {
    drawerLayout.closeDrawers();
}else{
   drawerLayout.openDrawer(Gravity.START);
}

// your code to replace or add fragment

NOTE:- Change Gravity.START to Gravity.END if your drawer is open from right side. first close drawer and then replace fragment

Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
  • i closed the drawer when i replaced fragment as you said,still no use.i assume its because of heavy work on fragment,can you plz check Gmail app,they handle the issue by making fragment loading in separate thread, how can we implement it? – Nasrudeen Oct 18 '14 at 06:51
  • the data in the fragment is from local or from web-service.? – Pankaj Arora Oct 18 '14 at 06:56
  • use cursur loader to load data from local, neigher async task not runonuiThread method is works in this case, last and best option from developers site is cursur loader. – Pankaj Arora Oct 18 '14 at 07:07
  • http://stackoverflow.com/questions/8903104/how-to-use-cursor-loader-to-access-retrieved-data check this link – Pankaj Arora Oct 18 '14 at 07:08