Android导航抽屉在打开和关闭时滞后

时间:2015-02-22 10:07:58

标签: android navigation-drawer lag

我有导航抽屉的活动。当我单击ActionBar中的图标时,导航栏会打开,但有滞后。与结束相同。我使用了本教程:https://www.youtube.com/watch?v=6f6sbtgd6v0如何删除滞后?

    drawerListener = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close)
    {
        @Override
        public void onDrawerClosed(View drawerView)
        {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView)
        {
            super.onDrawerOpened(drawerView);
        }
    };

    lvMenu.setOnItemClickListener(this);
    lvMenu.setAdapter(menuAdapter);
    drawerLayout.setScrimColor(Color.TRANSPARENT);
    drawerLayout.setDrawerListener(drawerListener);

    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    return drawerListener.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(Bundle savedInstanceState)
{
    super.onPostCreate(savedInstanceState);
    drawerListener.syncState();
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
    lvMenu.setItemChecked(position, true);
}

2 个答案:

答案 0 :(得分:2)

我和我的团队面临着导航抽屉滞后的问题。在一半的屏幕上表现不错,但另一半则一直落后。事实证明,在某些情况下,ViewRoorImpl.performTraversals()正在调用performLayout(),这会导致性能下降。

要解决此问题,我们会将常量值分配给导航抽屉的android:layout_widthandroid:layout_height属性,即

android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"

您可能还想在AndroidManifest.xml

中启用活动的硬件加速
<activity
    android:name=".ui.SomeActivity"
    android:hardwareAccelerated="true" />

答案 1 :(得分:0)

你应该在抽屉关闭后进行交易操作

然后在AndroidManifest.xml中启用活动的硬件加速

private void DrawerLayout_DrawerClosed(object sender, DrawerLayout.DrawerClosedEventArgs e)
{
    if (fragment != null)
    {
        Android.Support.V4.App.FragmentTransaction ft = this.SupportFragmentManager.BeginTransaction();
        ft.Replace(Resource.Id.HomeFrameLayout, fragment);
        ft.Commit();
    }
}

public async  void NavigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
    try
    {
        this.HideSoftKeyboard();
        x= e.MenuItem.ItemId;
        switch (x)
        {
            case (Resource.Id.nav1):
                fragment = new Fragment1();
                title = "Fragment1";
                break;
            case (Resource.Id.nav2):
                fragment = new Fragment2();
                title = "Fragment2";
                break;
            case (Resource.Id.nav3):
                fragment = new Fragment3();
                title = "Fragment3";
                break;
            case (Resource.Id.nav4):
                fragment = new Fragment4();
                title = "Fragment4";
                break;
            case (Resource.Id.nav_settings):
                Intent intent = new Intent(this, typeof(SettingsActivity));
                StartActivity(intent);
                break;
            case (Resource.Id.nav_About):
                Intent intentabout = new Intent(this, typeof(AboutActivity));
                StartActivity(intentabout);
                break;
        }
        if (SupportActionBar != null)
        {
            SupportActionBar.Title = title;
        }
    }
    catch (Exception ex)
    {
        this.WriteExceptionLog(ex);
    }
}