Optimizing drawer and activity launching speed

前端 未结 7 2003
傲寒
傲寒 2020-12-04 07:44

I\'m using the Google DrawerLayout.

When an item gets clicked, the drawer is smoothly closed and an Activity will be launched. Turning thes

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 08:18

    I was facing same issue with DrawerLayout.

    I have research for that and then find one nice solution for it.

    What i am doing is.....

    If you refer Android Sample app for the DrawerLayout then check the code for selectItem(position);

    In this function based on the position selection fragment is called. I have modify it with below code as per my need and works fine with no animation close stutter.

    private void selectItem(final int position) {
        //Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
        mDrawerLayout.closeDrawer(drawerMain);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Fragment fragment = new TimelineFragment(UserTimeLineActivity.this);
                Bundle args = new Bundle();
                args.putInt(TimelineFragment.ARG_PLANET_NUMBER, position);
                fragment.setArguments(args);
    
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    
                // update selected item and title, then close the drawer
                mCategoryDrawerList.setItemChecked(position, true);
    
                setTitle("TimeLine: " + mCategolyTitles[position]);
            }
        }, 200);
    
    
        // update the main content by replacing fragments
    
    
    }
    

    Here i am first closing the DrawerLayout. which takes approx 250 miliseconds. and then my handler will call the fragment. Which works smooth and as per the requirement.

    Hope it will also helpful to you.

    Enjoy Coding... :)

提交回复
热议问题