【问题标题】:Avoid click on same items of Navigation drawer concurrently one after other for fragments and for activities避免为片段和活动同时单击导航抽屉的相同项目
【发布时间】:2017-03-08 14:49:22
【问题描述】:

我对所有活动和片段使用通用的导航抽屉,但是当我点击导航抽屉的同一个项目时,它也会膨胀同一个屏幕。我想要的是不应该第二次加载相同的项目。

这是我的代码:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (id == R.id.nav_profile) {
        Intent i = new Intent(MainFragmentsActivity.this, ProfileActivity.class);
        startActivity(i);
        drawer.closeDrawer(GravityCompat.START);
    }
}

【问题讨论】:

  • 显示您的DrawerItemClickedListener。你是怎么编码的?
  • 下面是我的代码
  • @Override public boolean onNavigationItemSelected(MenuItem item) { int id = item.getItemId();DrawerLayout 抽屉 = (DrawerLayout) findViewById(R.id.drawer_layout); if (id == R.id.nav_profile) {Intent i = new Intent(MainFragmentsActivity.this, ProfileActivity.class);开始活动(一);抽屉.closeDrawer(GravityCompat.START);}

标签: android


【解决方案1】:

您不应该对所有活动和片段使用通用导航抽屉。常见的设计模式是在导航抽屉菜单项的主要活动上显示不同的片段。如果用户在任何片段内导航,您应该打开一个没有任何导航抽屉但在操作栏中带有后退箭头的活动。

现在,如果您不想显示相同的片段,那么在 onNavigationItemSelected 你可以编写如下逻辑 -

Fragment fragment = getSupportFragmentManager().findFragmentByTag("TAG");
      if (fragment == null) 
       {
         Create and attach new fragment
       }else{
          //Do not do anything.
       }

【讨论】:

    【解决方案2】:

    简单的事情,将点击视图的 id 存储到一个变量中并比较是否相同,

    int id; // use this variable to store id of clicked view.
    @Override public boolean onNavigationItemSelected(MenuItem item) {
    
         if(id != item.getItemId) {
    
            id = item.getItemId(); // store clicked id in variable
            // Do your clicking stuffs here...
            if (id == R.id.nav_profile) {
               Intent i = new Intent(MainFragmentsActivity.this,   ProfileActivity.class);
               startActivity(i);
               drawer.closeDrawer(GravityCompat.START);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:
      For fragments:
      
      if (id == R.id.nav_things_to_do) {
                  if (id == R.id.nav_things_to_do && viewPager.getCurrentItem() != 0) {
                      Intent main1 = new Intent(getApplicationContext(), MainFragmentsActivity.class);
                      main1.putExtra("position", 0);
                      main1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                      main1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                      main1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                      finish();
                      startActivity(main1);
                      overridePendingTransition(0, 0);
                      drawer.closeDrawer(GravityCompat.START);
                  }
                  else {
                      drawer.closeDrawer(GravityCompat.START);
                  }
              }
      if (id == R.id.nav_all_tasks) {
                  if (id == R.id.nav_all_tasks && viewPager.getCurrentItem() != 1) {
                      Intent main2 = new Intent(getApplicationContext(), MainFragmentsActivity.class);
                      main2.putExtra("position", 1);
                      main2.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                      main2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                      main2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                      finish();
                      startActivity(main2);
                      overridePendingTransition(0, 0);
                      drawer.closeDrawer(GravityCompat.START);
                  }
                  else {
                      drawer.closeDrawer(GravityCompat.START);
                  }
              }
      
      For Activities:
      
      if (id == R.id.nav_images) {
                  try {
                      Intent i = new Intent(MainFragmentsActivity.this,
                              SelectFilesActivity.class);
                      i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                      startActivity(i);
                      drawer.closeDrawer(GravityCompat.START);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-26
        • 1970-01-01
        相关资源
        最近更新 更多