android导航抽屉,在片段之间切换

时间:2014-10-18 05:37:14

标签: android navigation-drawer

当我在导航抽屉中的片段之间切换时,导航抽屉缓慢关闭,我假设它等待替换新选择的片段并关闭抽屉,

我怎样才能加快速度?在 Gmail 应用中,当您在 Navigation-drawer 中选择一个标签时,不会立即创建新片段,而是显示 ProgressBar ,然后内容是加载,导航抽屉关闭速度在这里。

我该怎么做,感谢任何代码示例。

修改

    @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);

}

谢谢,

2 个答案:

答案 0 :(得分:0)

我认为此链接可以帮助您:Speed up Android Navigation drawer

public boolean smoothSlideViewTo(... int duration) {
    ...
    return forceSettleCapturedViewAt(... int duration);
}

答案 1 :(得分:0)

当您更换像这样的片段时,您必须关闭抽屉:

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

//要替换或添加片段的代码

注意: - 如果您的抽屉从右侧打开,请将Gravity.START更改为Gravity.END。先关闭抽屉,然后替换片段