how to create smooth navigation drawer

前端 未结 5 492
醉话见心
醉话见心 2021-01-30 09:45

I am using this example for navigation drawer. When clicking on of item of left drawer listview it shows some images but drawerLayout closes not smoothly.

What should I

5条回答
  •  猫巷女王i
    2021-01-30 10:00

    Please review my MainActivity to make smooth DrawerLayout (open/close) with a custom topBar layout.

    public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    
    Fragment fragment = null;
    Class fragmentClass = null;
    FrameLayout frameLayout;
    TextView topTilte;
    DrawerLayout drawer;
    NavigationView navigationView;
    private boolean isDrawerOpen = false;
    
    @Override
    protected void onCreateFinished() {
        setContentView(R.layout.activity_main);
    
        drawer = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
    
        topTilte = findViewById(R.id.toolbar_title);
        fragmentClass = HomeFragment.class;
        topTilte.setText(R.string.menu_home);
        setFragment();
    
        drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View view, float slideOffset) {
                if(slideOffset > .55 && !isDrawerOpen){
                    onDrawerOpened(view);
                    isDrawerOpen = true;
                } else if(slideOffset < .45 && isDrawerOpen) {
                    onDrawerClosed(view);
                    isDrawerOpen = false;
                }
            }
    
            @Override
            public void onDrawerOpened(@NonNull View view) {
    
            }
    
            @Override
            public void onDrawerClosed(@NonNull View view) {
    
            }
    
            @Override
            public void onDrawerStateChanged(int i) {
    
            }
        });
    
        navigationView.setNavigationItemSelectedListener(this);
    }
    
    @Override
    protected void afterInjection() {
    
    }
    
    public void openMenu(View v) {
        drawer.openDrawer(Gravity.LEFT);
    }
    
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if (id == R.id.nav_home) {
            topTilte.setText(R.string.menu_home);
            fragmentClass = HomeFragment.class;
            setFragment();
    
        } else if (id == R.id.nav_profile) {
    
        } 
    
        return true;
    }
    
    private void setFragment() {
    
    
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    fragment = (Fragment) fragmentClass.newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
    
                drawer.closeDrawer(GravityCompat.START);
            }
        }, 200);
    }
    }
    

提交回复
热议问题