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

时间:2017-02-14 13:12:22

标签: android android-fragments

在使用我的Android应用程序时,我发现导航抽屉在打开和关闭时都会挂起。它挂了2-3秒。在大多数设备上,它都很好(主要是使用Stock Android),但在某些设备上,它会挂起(主要是三星)。

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;
    }
}

我不知道背后的原因是什么。每当我试图打开它时,它就会挂起。每当我通过单击一个选项打开一个片段时,它就会挂起(我不认为它与片段的加载有关,因为抽屉在没有片段加载时打开时会挂起)。如果需要任何其他代码,我已准备好显示它。

1 个答案:

答案 0 :(得分:0)

我通过降低Navigation Drawer标题图像的图像分辨率来解决了这个问题。它是1600 * 1200,我将分辨率更改为400 * 300。完成此操作后,抽屉在受影响的手机上平稳打开。

这不是我的编码错误,但它可能是定制ROM的问题,他们如何处理高分辨率的图像,因为抽屉已经在Stock Android上顺利打开。