简体   繁体   English

导航抽屉在某些设备上打开/关闭时会挂起

[英]Navigation Drawer hangs while opening/closing on some devices

While working with my android app, I found out that navigation drawer hangs while opening as well as closing. 在使用我的Android应用程序时,我发现导航抽屉在打开和关闭时都会挂起。 It hangs for 2-3 seconds. 它挂了2-3秒。 On most devices, it's fine (mostly with Stock Android) but on some devices, it hangs (Mostly Samsung). 在大多数设备上,它很好(主要是使用Stock Android),但在某些设备上,它挂起(主要是三星)。

Home.java (activity in which drawer is implemented): Home.java(实现抽屉的活动):

public class Home extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener{

    Fragment fragment;

    public void MsgBox(String title, String msg,int id){
        new LovelyInfoDialog(this)
                .setTopColorRes(R.color.primary)
                .setIcon(R.drawable.ic_info_white_24dp)
                //This will add Don't show again checkbox to the dialog. You can pass any ID as argument
                .setNotShowAgainOptionEnabled(id)
                .setTitle(title)
                .setMessage(msg)
                .show();
    }

    public boolean showAds() {
        SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean showAd = getPrefs.getBoolean("showAd", true);
        return showAd;
    }

    public static boolean hasPermissions(Context context, String... permissions) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        hasPermissions(this,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.GET_ACCOUNTS);
        int PERMISSION_ALL = 1;
        String[] PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.GET_ACCOUNTS};
        if(!hasPermissions(this, PERMISSIONS)){
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }
        AppRate.with(this)
                .setInstallDays(5) // default 10, 0 means install day.
                .setLaunchTimes(10) // default 10
                .setRemindInterval(2) // default 1
                .setShowLaterButton(true) // default true
                .setDebug(false) // default false
                .monitor();

        AppRate.showRateDialogIfMeetsConditions(this);

        final NavigationView navigationView = (NavigationView) findViewById(nav_view);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                navigationView.getMenu().getItem(1).setChecked(true);
                onNavigationItemSelected(navigationView.getMenu().getItem(1));
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
        fragment=new QuestionPap();
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment, "Home");
            ft.commit();
        }
    }

    boolean doubleBackToExitPressedOnce = false;
    boolean drawe=false;
    void Back(){
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        this.drawe=true;
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;
                drawe=true;
            }
        }, 2000);
    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (!drawe) {
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                if (fragment == getSupportFragmentManager().findFragmentByTag("Home")) {
                    super.onBackPressed();
                } else {
                    drawer.openDrawer(GravityCompat.START);
                    Back();
                }
            }
        }else{
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        Intent i = new Intent(this,DownloadService.class);
        stopService(i);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
   if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        try {
            int id = item.getItemId();
          //  fragment = null;
            String TAG=null;
            AppBarLayout appbar = (AppBarLayout)findViewById(R.id.appbar);
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

            if (id == R.id.home && fragment!=getSupportFragmentManager().findFragmentByTag("Home")) {
                TAG="Home";
                fragment = new QuestionPap();
                fab.setVisibility(View.VISIBLE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
                    appbar.setTranslationZ(0);
                }
            } else if (id == R.id.offline && fragment!=getSupportFragmentManager().findFragmentByTag("Offline")) {
                TAG="Offline";
                MsgBox("Save Files Offline","Tap and hold any file to delete it.",2);
                fragment = new offline();
                fab.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
                    appbar.setTranslationZ(6*2);
                }
            } else if (id == R.id.syll && fragment!=getSupportFragmentManager().findFragmentByTag("Syllabus")) {
                TAG="Syllabus";
                fragment = new Syllabus();
                fab.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
                    appbar.setTranslationZ(0);
                }
            } else if (id == R.id.Tools && fragment!=getSupportFragmentManager().findFragmentByTag("Tools")) {
                TAG="Tools";
                fragment = new Tools();
                fab.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
                    appbar.setTranslationZ(6*2);
                }
            } else if (id == R.id.contribute && fragment!=getSupportFragmentManager().findFragmentByTag("Contribute")) {
                TAG="Contribute";
                fragment = new contribute();
                fab.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
                    appbar.setTranslationZ(6*2);
                }
            } else if (id == R.id.about && fragment!=getSupportFragmentManager().findFragmentByTag("About")) {
                TAG="About";
                fragment = new about();
                fab.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
                    appbar.setTranslationZ(6*2);
                }
            } else if (id==R.id.notices && fragment!=getSupportFragmentManager().findFragmentByTag("Notices")) {
                TAG="Notices";
                fragment = new notices();
                fab.setVisibility(View.GONE);
                MsgBox("Get Latest News", "Don't miss out on any department news.",3);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
                    appbar.setTranslationZ(6*2);
                }
            } else if (id==R.id.donate && fragment!=getSupportFragmentManager().findFragmentByTag("Donate")) {
                TAG="Donate";
                fragment = new donate();
                fab.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    appbar.setTranslationZ(6 * 2);
                }
            }  else if (id==R.id.assig && fragment!=getSupportFragmentManager().findFragmentByTag("Assignments")) {
                TAG = "Assignments";
                fragment = new assignments();
                fab.setVisibility(View.GONE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    appbar.setTranslationZ(6 * 2);
                }
            }
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            if (fragment != null ) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.content_frame, fragment,TAG);
                ft.commit();
            }
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
        }
        catch (Exception ex){

        }
            return true;
    }
}

I don't know what's the reason behind it. 我不知道背后的原因是什么。 Whenever I try to open it, it hangs. 每当我试图打开它时,它就会挂起。 Whenever I open a fragment by clicking an option, it hangs (I don't think it has something to do with loading of fragment because drawer hangs while it opens when no fragment loading is done). 每当我通过单击一个选项打开一个片段时,它就会挂起(我不认为它与片段的加载有关,因为抽屉在没有片段加载时打开时会挂起)。 If any other code is required, I'm ready to show it. 如果需要任何其他代码,我已准备好显示它。

I solved the problem by decreasing the image resolution of Navigation Drawer header image. 我通过降低Navigation Drawer标题图像的图像分辨率来解决了这个问题。 It was 1600*1200 and I changed the resolution to 400*300. 它是1600 * 1200,我将分辨率更改为400 * 300。 After doing this, the drawer opens smoothly on affected phones. 完成此操作后,抽屉在受影响的手机上平稳打开。

It wasn't my coding fault but it might be a problem with customized ROMs how they handle the images with high resolution because the drawer was already opening smoothly on Stock Android. 这不是我的编码错误,但它可能是定制ROM的问题,他们如何处理高分辨率的图像,因为抽屉已经在股票Android上顺利打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM