android - Fragment 的抽屉导航关闭不流畅

标签 android android-fragments navigation-drawer

我已经为我的应用实现了一个Navigation Drawer,它在大多数情况下都能正常工作。

我的 Home 选项卡执行几个 API 请求以显示一些信息,这些请求和它们所需的工作量(即使它是相当小的)会阻止抽屉中途关闭,使得它不顺利。

我的第一个“解决方案”是 to load the fragment only after the Drawer closes ,像这样:

toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);

            displaySelectedScreen(itemSelected.getItemId());
        }
    };

但是这会在 Fragments View 显示之前造成 0.5 秒的等待,从用户的角度来看这并不是很吸引人。

这是我的 activity_main.xml 的一部分:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

这是我目前显示Fragment的方式:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(final MenuItem item) {      
    displaySelectedScreen(item.getItemId());
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

这是 displaySelectedScreen 中发生的事情:

private void displaySelectedScreen(int itemId) {
    Fragment fragment;
    fragment = checkFragment(itemId); // Instantiates the right Fragment

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, fragment);
    ft.commit();

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

    // Close our Drawer since we have selected a valid item.
    drawer.closeDrawer(GravityCompat.START);
}

您有什么建议可以让我的 Navigation Drawer 顺利关闭?

编辑:

根据要求,这是我的 AsyncTask 代码,它执行 API 请求并返回 JSONObject:

/**
 * Requests information from the API via APIRequests
 * Extends AsyncTask in order to do network-related actions in background.
 */
private class SearchInfo extends AsyncTask<Void, Integer, JSONObject> {

    @Override
    protected JSONObject doInBackground(Void... params) {
        JSONObject information;

        APIRequests apiRequests = new APIRequests();
        information = apiRequests.getGameInfo();

        requestDone = true;

        return information;
    }
}

APIRequest 的方法通过 HttpURLConnection 执行简单的 HTTP GET 并返回包含检索到的数据的 JSONObject .

最佳答案

您似乎在抽屉关闭之前在 displaySelectedScreen() 中提交了 Fragment

@Override
public boolean onNavigationItemSelected(final MenuItem item) {      
    displaySelectedScreen(item.getItemId()); // Commit is there
    drawer.closeDrawer(GravityCompat.START); // And drawer closing simultaneously
    return true;
}

我会怎么做

// Create a field that marks that something needs to be displayed
// after drawer closes. When null means no action (drawer closed
// by the user, not by selecting an item)
Integer itemIdWhenClosed;

@Override
public boolean onNavigationItemSelected(final MenuItem item) {      
    itemIdWhenClosed = item.getItemId(); // Mark action when closed
    drawer.closeDrawer(GravityCompat.START); // Close it
    return true;
}

@Override
public void onDrawerClosed(View view) {
    super.onDrawerClosed(view);
    // If id to show is marked, perform action
    if (itemIdWhenClosed != null) {
        displaySelectedScreen(itemIdWhenClosed);
        // Reset value
        itemIdWhenClosed = null;
    }
}

// Now here just commit, don't close the drawer since this is already
// fired when it's closed
private void displaySelectedScreen(int itemId) {
    final Fragment fragment = checkFragment(itemId);
    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, fragment);
    ft.commit();
}

关于android - Fragment 的抽屉导航关闭不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41270823/

相关文章:

android - 起点和终点的画廊空间

android - JNI Android中的分配和释放问题

java - 毕加索 + Recyclerview

android - 在 fragment BroadcastReceiver 上显示 progressDialog

android - WebView android 不换行文本

java - android:如果不满足某些条件,则不应单击 RadioButton

android - fragment 内带有基本适配器的 ListView

android - 如何只用按钮打开抽屉布局?

android - 如何停止在多个 Activity 中使用的抽屉导航 Activity 中选中项目的点击事件

java - 错误 incompatible types : android. app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager