Android 选择一个选项时,导航抽屉挂起2秒钟

Android 选择一个选项时,导航抽屉挂起2秒钟,android,google-maps,android-fragments,navigation-drawer,Android,Google Maps,Android Fragments,Navigation Drawer,我有一个导航抽屉,里面有很多片段,这些片段是在选择导航抽屉的任何项目时出现的。在一个项目上,我展示了一张地图。我不知道为什么在选择该项目时,导航抽屉会挂起2-3秒,然后关闭抽屉。我希望它像导航抽屉里的其他物品一样平滑 以下是我在MainActivity中导航抽屉的代码: private void displayView(int position) { // update the main content by replacing fragments switch

我有一个导航抽屉,里面有很多片段,这些片段是在选择导航抽屉的任何项目时出现的。在一个项目上,我展示了一张地图。我不知道为什么在选择该项目时,导航抽屉会挂起2-3秒,然后关闭抽屉。我希望它像导航抽屉里的其他物品一样平滑

以下是我在MainActivity中导航抽屉的代码:

private void displayView(int position) {
        // update the main content by replacing fragments

        switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment = new FindPeopleFragment();
            break;
        case 2:
            fragment = new PhotosFragment();
            break;
        case 3:
            fragment = new CommunityFragment();
            break;
        case 4:
            fragment = new PagesFragment();
            break;
        case 5:
            fragment = new WhatsHotFragment();
            break;

        default:
            break;
        }

        if (fragment != null) {

            FragmentManager fragmentManager = getFragmentManager();
            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");
        }
    }
这是抽屉挂在上面的碎片代码:

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


        View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
    mPlaceType = getResources().getStringArray(R.array.place_type);
    mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();
        // Array of place type names
        mPlaceTypeName = getResources().getStringArray(R.array.place_type_name);

        // Creating an array adapter with an array of Place types
        // to populate the spinner
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, mPlaceTypeName);

        // Getting reference to the Spinner 
        mSprPlaceType = (Spinner) rootView.findViewById(R.id.spr_place_type);

        // Setting adapter on Spinner to set place types
        mSprPlaceType.setAdapter(adapter);

        Button btnFind;

        // Getting reference to Find Button
        btnFind = ( Button ) rootView.findViewById(R.id.btn_find);


        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());


        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), requestCode);
            dialog.show();

        }else { 
            mGoogleMap.setMyLocationEnabled(true);



            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = 
                    (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);


            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);

            // Getting Current Location From GPS
            Location location = locationManager.getLastKnownLocation(provider);

            if(location!=null){
                    onLocationChanged(location);
            }

            locationManager.requestLocationUpdates(provider, 20000, 0, this);
                         }      
         return rootView;
    }
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u find\u people,container,false);
mPlaceType=getResources().getStringArray(R.array.place\u类型);
mGoogleMap=((MapFragment)getFragmentManager().findFragmentById(
R.id.map)).getMap();
//地名类型名称数组
mPlaceTypeName=getResources().getStringArray(R.array.place\u type\u name);
//使用位置类型数组创建数组适配器
//填充微调器的步骤
ArrayAdapter=新的ArrayAdapter(getActivity(),android.R.layout.simple\u微调器\u下拉菜单\u项,mPlaceTypeName);
//获取对微调器的引用
mSprPlaceType=(微调器)rootView.findviewbyd(R.id.spr\u place\u type);
//在微调器上设置适配器以设置放置类型
mSprPlaceType.setAdapter(适配器);
按钮btnFind;
//获取“查找”按钮的引用
btnFind=(按钮)rootView.findviewbyd(R.id.btn\u find);
//获取Google Play可用性状态
int status=GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
如果(status!=ConnectionResult.SUCCESS){//Google Play服务不可用
int requestCode=10;
Dialog Dialog=GooglePlayServicesUtil.getErrorDialog(状态,getActivity(),requestCode);
dialog.show();
}否则{
mGoogleMap.setMyLocationEnabled(true);
//从系统服务位置\u服务获取LocationManager对象
LocationManager LocationManager=
(LocationManager)getActivity().getSystemService(Context.LOCATION\u服务);
标准=新标准();
//获取最佳提供商的名称
字符串提供程序=locationManager.getBestProvider(条件为true);
//从GPS获取当前位置
Location Location=locationManager.getLastKnownLocation(提供者);
如果(位置!=null){
onLocationChanged(位置);
}
locationManager.RequestLocationUpdate(提供程序,20000,0,此);
}      
返回rootView;
}

我也面临这样的问题。 这对我完全有效。希望对你有帮助

在代码中

在显示视图功能中,将所有代码置于可运行状态

mPendingThread = new Runnable() {
        @Override
        public void run() {
             //Here is all your code
             switch(position){
             .........
             .........
        }
    };
onCreate方法中

    mHandler = new Handler();

// Getting reference to the ActionBarDrawerToggle
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        /** Called when drawer is closed */
        @Override
        public void onDrawerClosed(View view) {

            invalidateOptionsMenu();

            if (mPendingThread != null) {
                mHandler.post(mPendingThread);
                mPendingThread = null;
            }

        }

        /** Called when a drawer is opened */
        @Override
        public void onDrawerOpened(View drawerView) {

            invalidateOptionsMenu();
        }

    };

    // Setting DrawerToggle on DrawerLayout
    mDrawerLayout.setDrawerListener(mDrawerToggle);

尝试将mDrawerLayout.closeDrawer(mDrawerList)放入;在switch语句上方。if(mDrawerLayout!=null)mDrawerLayout.closeDrawer(mDrawerList);让我知道它有什么作用吗?没有,我按照你的建议试过了,但结果是一样的。我尝试了一些研究,发现在片段中,如果我注释mGoogleMap=((MapFragment)getFragmentManager().findffragmentbyid(R.id.map)).getMap()以外的其他代码,就会发现:;它运转平稳。如果我取消对所有内容的注释,它将再次挂起。因此,如果您在视图rootView和返回中注释掉/*您的代码*/它是否仍然挂起?是的,如果我在视图rootView和返回中注释所有内容,它仍然挂起:(该死的绿巨人。它没有对任何其他片段进行处理?你能在我的代码中编辑吗?以前从未使用过runnable。请注意,现在一切正常。我将开关的代码设置为runnable,但所有单击都会显示白色屏幕。是的,对不起,我忘记了一些代码,因为太长了。我现在就编辑