【问题标题】:Slow navigation drawer缓慢的导航抽屉
【发布时间】:2018-03-15 18:30:24
【问题描述】:

当我打开复杂视图时,我的导航抽屉滞后。这是我的 MenuActivity 代码:

public class MenuActivity extends AppCompatActivity implements View.OnClickListener {

private DrawerLayout mDrawer;
private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);
    // Set a Toolbar to replace the ActionBar.
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    // Set the menu icon instead of the launcher icon.
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // Setup drawer view
    findViewById(R.id.building_status_fragment).setOnClickListener(this);
    findViewById(R.id.news_fragment).setOnClickListener(this);
    findViewById(R.id.register_fragment).setOnClickListener(this);
    findViewById(R.id.phone_view).setOnClickListener(this);
    findViewById(R.id.services_fragment).setOnClickListener(this);
    findViewById(R.id.contacs_fragment).setOnClickListener(this);
    // first init
    getFragmentManager().beginTransaction().replace(R.id.container, new BuildingStatusFragment()).commit();
    setTitle(getString(R.string.title_building_progress)); // toolbar.setTitle dont work, hm
}

@Override
public void onClick(View v) {
    mDrawer.closeDrawers();
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    switch(v.getId()) {
        case R.id.building_status_fragment:
            fragmentTransaction.replace(R.id.container, new BuildingStatusFragment());
            toolbar.setTitle(getString(R.string.title_building_progress));
            break;
        case R.id.news_fragment:
            fragmentTransaction.replace(R.id.container, new NewsFeedFragment());
            toolbar.setTitle(getString(R.string.title_news));
            break;
        case R.id.register_fragment:
            fragmentTransaction.replace(R.id.container, new RegisterFragment());
            toolbar.setTitle(getString(R.string.title_look));
            break;
        case R.id.services_fragment:
            fragmentTransaction.replace(R.id.container, new ServicesFragment());
            toolbar.setTitle(getString(R.string.title_services));
            break;
        case R.id.contacs_fragment:
            fragmentTransaction.replace(R.id.container, new ContactsFragment());
            toolbar.setTitle(getString(R.string.title_contacts));
            break;
        case R.id.phone_view:
            startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:74996782873")));
            break;
    }
    fragmentTransaction.commit();
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // https://stackoverflow.com/questions/13721049/how-to-hide-default-keyboard-when-click-on-menu
            if(getCurrentFocus() != null && getCurrentFocus() instanceof EditText){
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
            mDrawer.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

}

我有 2 张图片的“复杂”视图:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:scrollbars="none">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:src="@drawable/auto" />

        <TextView
            android:id="@+id/toptext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="17dp"
            android:layout_marginTop="17dp"
            android:background="#B300A3DA"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:text="Покупка"
            android:textColor="#fff"
            android:textSize="18sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toptext"
            android:layout_marginLeft="17dp"
            android:layout_marginTop="4dp"
            android:background="#B300A3DA"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:text="машиномест"
            android:textColor="#fff"
            android:textSize="18sp" />

        <!--Костыль-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:paddingBottom="15dp">
            <!--Костыль-->
            <LinearLayout
                android:id="@+id/auto_form"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#B3000000"
                android:orientation="vertical"
                android:paddingBottom="7dp"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:paddingTop="7dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="Посмотреть"
                    android:textColor="@color/white"
                    android:textSize="18sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="предложение"
                    android:textColor="@color/white"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:src="@drawable/rights" />

        <TextView
            android:id="@+id/toptext2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="17dp"
            android:layout_marginTop="17dp"
            android:background="#B300A3DA"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:text="Оформление"
            android:textColor="#fff"
            android:textSize="18sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/toptext2"
            android:layout_marginLeft="17dp"
            android:layout_marginTop="4dp"
            android:background="#B300A3DA"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:text="прав собственности"
            android:textColor="#fff"
            android:textSize="18sp" />
        <!--Костыль-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:paddingBottom="15dp">
            <!--Костыль-->
            <LinearLayout
                android:id="@+id/rights_form"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#B3000000"
                android:orientation="vertical"
                android:paddingBottom="7dp"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:paddingTop="7dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="Посмотреть"
                    android:textColor="@color/white"
                    android:textSize="18sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="предложение"
                    android:textColor="@color/white"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

每次我打开这个片段时它都会滞后。我还注意到带有地图视图的片段也有一些滞后。我读了this post,看起来我应该在导航抽屉关闭后完成所有工作(如果我在片段事务之前调用 mDrawer.closeDrawers() 不一样吗??),但是如果我打开滞后的原因是什么已初始化具有复杂视图的片段时的抽屉?这对我来说很奇怪。

调试器还说 uithread 需要 2 多工作并跳过帧。那么我应该优化我的“复杂”片段还是优化导航抽屉出现?如何?谢谢

【问题讨论】:

    标签: android android-fragments navigation-drawer


    【解决方案1】:

    我看到了你的代码,下面是我想要做的更正

    1. 从 Ui 线程加载位图(而不是在 xml 中提及大位图,而是使用占位符并在恢复时开始加载)
    2. 如果可能,使用按比例缩小的位图版本
    3. 如果位图不透明,您可以加载 RGB565 格式的位图,它将位图的大小减半,几乎没有质量损失。
    4. 不要为相对布局设置背景颜色。它永远不会显示,因为 Image 会透支它。过度绘制会消耗处理能力。它将帮助您加快速度。

      1. 避免不必要的布局嵌套。 Linear 中的 Linear,请尝试使用 RelativeLayout 或 PercentRelativeLayout

      2. 尝试使用 getWindow().setBackgroundDrawable(null);因为您已将 ScrollView 背景设置为白色。

    我希望这足以加快您的代码速度。

    【讨论】:

    • 谢谢!所以唯一的方法是修复我的代码片段以使导航抽屉打开顺畅?在我的地图视图片段中,我还应该尝试在后台线程中移动所有加载?
    • 您可以为谷歌地图调用 getMapAsync 并且您必须提供 onMapReady 回调。当您的地图加载(准备使用)时,您会收到通知
    • 很伤心,但我还是不明白为什么在片段已经加载时打开菜单时导航抽屉会滞后?
    • 您能否详细说明问题并提供您的代码,以便我查看并说明问题所在??
    【解决方案2】:

    我认为您正在使用导航抽屉标题背景图片,删除或更改导航抽屉标题背景图片(小尺寸),然后尝试一次

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多