如何避免滑动菜单滞后导致打开片段卡顿?

How to avoid sliding menu lag which causes stuttering in opening the fragment?

在我的应用程序中,我在主 activity 中实现了滑动菜单抽屉。 菜单的设计是自定义的,单击菜单中的列表项会打开一个片段。

private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment=new LastTransactionsDetails();

            break;
        case 1:
            fragment = new Statement_of_Transaction();
            break;
       case 2:
          fragment = new AccountDetails();

            break;
        case 3:
            fragment=new ProfileSetting();

            break;
           case 4:
            fragment=new LastTransactionsDetails();

            break;

        default:
            break;
    }

    if (fragment != null) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

滑动菜单的关闭显示有点延迟,导致菜单卡顿。 onCreateView 片段正在调用一个异步任务,其中包含对 web 服务方法的调用。是不是因为片段加载导致了延迟?如果是这样,那么我做了什么改变? 片段代码如下:

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    mActivity = context instanceof Activity ? (Activity) context : null;
    viewUtils = new ViewUtils(mActivity);
    webServicesParams = new WebServicesParams(mActivity);
    database = new DbAdapter(context);
    database.createDatabase();
    database.open();
    parseJsonResponse = new ParseJsonResponse();
    showProgressDialog();

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.last_transactions_details, container, false);

    listView_transactions = (ListView) rootView.findViewById(R.id.listView_transactions);

    try {


        String jsonResponse = webServicesParams.Last_Transaction();
        arr_last_trans = parseJsonResponse.getTierDetails(jsonResponse);

        adapter = new LastTransactionAdapter(arr_last_trans,getActivity());
        listView_transactions .setAdapter(adapter);

        dismissProgressDialog();
    }

在单击导航项之后和加载片段之前添加一些延迟,以顺利关闭幻灯片菜单。

private final Handler mDrawerHandler = new Handler();
//Wait for few millis before replacing fragment to allow slidemenu to fully close. 
public void smoothReplaceFragment(final Fragment fragment) {
    mDrawerHandler.removeCallbacksAndMessages(null);
    mDrawerHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
             replaceFragment(fragment);
        }
    }, 350);
}

//Replace Fragment
public void replaceFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}