0

Also habe ich kürzlich eine Navigationsleiste in meiner App implementiert. Zusammen mit Navigationsschublade habe ich auch Menüpunkte implementiert, um auf Optionen wie Einstellungen zuzugreifen. Ich möchte jedoch diese Schaltfläche mit den fließenden Aktionen auf allen Seiten der Navigationsschublade beibehalten, aber ich möchte sie nicht auf meiner Einstellungsseite haben, auf die über das Menü zugegriffen wird. Hier ist der aktuelle Code, den ich habe.FloatingActionButton nur von der Einstellungsseite löschen

Das on create für die Navigationsleiste.

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_nav_drawer); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        //.setAction("Action", null).show(); 
      Intent sendIntent = new Intent(); 
      sendIntent.setAction(Intent.ACTION_SEND); 
      sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); 
      sendIntent.setType("text/plain"); 
      startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 
     } 
    }); 

    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.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

Dann ist dies der Code für die Floating-Aktion Schaltfläche selbst im app_bar_nav_drawer.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.jamessingleton.chffrapi.NavDrawerActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_nav_drawer" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:srcCompat="@drawable/ic_menu_share"/> 

</android.support.design.widget.CoordinatorLayout> 

Hier ist der Code für die settings_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 


    <TextView 
     android:text="Use Cell Data to retrieve driving data:" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView" 
     android:textSize="18sp" 
     android:textColor="@color/cast_expanded_controller_background_color" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 

    <RadioGroup 
     android:layout_width="89dp" 
     android:layout_height="69dp" 
     android:weightSum="1" 
     android:layout_below="@+id/textView"> 

     <RadioButton 
      android:text="Enable" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/radioButton7" 
      android:layout_weight="1" /> 

     <RadioButton 
      android:text="Disable" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/radioButton6" 
      android:layout_weight="1" 
      android:checked="true" /> 
    </RadioGroup> 


</RelativeLayout> 

Gerade FYI ein I Ich möchte dies nur für die Menüseiten beibehalten, nicht für die Einstellungsseite. Lass mich wissen, ob ich noch Code hinzufügen kann, der helfen würde.

Entire Nav Drawer Aktivität

public class NavDrawerActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener{ 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_nav_drawer); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         //.setAction("Action", null).show(); 
       Intent sendIntent = new Intent(); 
       sendIntent.setAction(Intent.ACTION_SEND); 
       sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); 
       sendIntent.setType("text/plain"); 
       startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 
      } 
     }); 

     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.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 


    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.nav_drawer, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     FragmentManager fragmentSettingsManager = getFragmentManager(); 
     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      fragmentSettingsManager.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit(); 
      setTitle(R.string.action_settings); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
     FragmentManager fragmentManager = getFragmentManager(); 
     if (id == R.id.nav_first_layout) { 
      fragmentManager.beginTransaction().replace(R.id.content_frame, new FirstFragment()).commit(); 
      setTitle(R.string.speed_graph); 
     } else if (id == R.id.nav_second_layout) { 
      fragmentManager.beginTransaction().replace(R.id.content_frame, new SecondFragment()).commit(); 
      setTitle(R.string.drive_player); 
     } else if (id == R.id.nav_third_layout) { 
      fragmentManager.beginTransaction().replace(R.id.content_frame, new ThirdFragment()).commit(); 
      setTitle(R.string.google_maps); 
     } else if (id == R.id.nav_share) { 
      Intent sendIntent = new Intent(); 
      sendIntent.setAction(Intent.ACTION_SEND); 
      sendIntent.putExtra(Intent.EXTRA_TEXT, "Go Check Out All Driving Data in the Play Store!"); 
      sendIntent.setType("text/plain"); 
      startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 
+0

Bitte aktualisieren Sie den Code des Navigationspunkt gewählten Methode müssen dort Änderungen vorgenommen werden. –

+0

@MohammedAtif, ich habe hinzugefügt, wo ich die Einstellungen Schaltfläche auswählen. Ich denke, das ist es, was du verlangst? –

+0

ja genau, ich werde die Antwort posten, indem Sie Änderungen an Ihrem Code –

Antwort

0

Sie könnten "fab.setVisibility (View.GONE)" fügen dem OnCreate nur in jenen Tätigkeiten, bei denen es

Hoffnung versteckt werden sollte diese

+0

sehr schlechten Vorschlag. Er wird seine Zeit verschwenden, indem er mit Ihrem Verfahren die Nullpunkterregung löst. –

+0

Wenn er dies nach dem LayoutInflate verwendet, sollte es gut funktionieren –

+0

Eine Sache, die Sie sich merken müssen, ist 'fab' ist variabel in der Hauptklasse. Der Zugriff darauf oder das Ändern von anderen Klassen ist möglich, aber absolut nicht zu empfehlen. –

0
geholfen

Es empfiehlt sich, solche Fälle über Schnittstellen zu behandeln. Also zuerst eine Schnittstelle erstellen.

FabVisibilityController.java

public interface FabVisiblityController{ 
    toggleVisibility(boolean visibility); 
} 

dann in der Hauptklasse

NavigationDrawerActivity.java

public class NavigationDrawerActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener, FabVisibilityController{ 
    //write this code at the end of the class 
    @override 
    toggleVisibility(boolean visibility){ 
     if(visibility) 
      fab.setVisibility(View.VISIBLE); 
     else 
      fab.setVisibility(View.GONE); 
    } 
} 

dann in Ihren Einstellungen Fragment

public class SettingsFragment extends Fragment{ 
    Context context; 
    FabVisibilityController controller; 
    //then in onAttach method 
    @override 
    onAttach(Context context){ 
     this.context = context; 
     if(context instanceof FabVisibilityController) 
       controller = (FabVisibilityController) context; 
    } 

    //then in onResume Method 
    @override 
    onResume(){ 
     super.onResume(); 
     //this will hide FAB whenever settings page is called 
     controller.toggleVisibility(false); 
    } 
} 

Mach dasselbe in deinen anderen Fragmenten, aber nur der Unterschied wird sein, controller.toggleVisibility(true) sollte in onResume() aufgerufen werden, um FAB sichtbar zu machen. Hier

+0

Womit würde ich "etwas anderes" machen? Die ID der Fab? –

+0

Nein, ID Ihrer verbleibenden Schubladenelemente. –

+0

Alles andere als Einstellungen sollte 'fab.setVisibility (View.VISIBLE)' haben, sonst wird Ihre Schaltfläche fab nicht wieder angezeigt, wenn Sie zu anderen Seiten kommen. –

0

ist die Lösung, die ich am besten fand für mich

@Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // Handle action bar item clicks here. The action bar will 
      // automatically handle clicks on the Home/Up button, so long 
      // as you specify a parent activity in AndroidManifest.xml. 
      int id = item.getItemId(); 
      FragmentManager fragmentSettingsManager = getFragmentManager(); 
      //noinspection SimplifiableIfStatement 
      if (id == R.id.action_settings) { 
       fragmentSettingsManager.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit(); 
       setTitle(R.string.action_settings); 
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
       fab.setVisibility(View.GONE); 
      } 
      return super.onOptionsItemSelected(item); 
     } 

     @SuppressWarnings("StatementWithEmptyBody") 
     @Override 
     public boolean onNavigationItemSelected(MenuItem item) { 
      // Handle navigation view item clicks here. 
      int id = item.getItemId(); 
      FragmentManager fragmentManager = getFragmentManager(); 
      if (id == R.id.nav_first_layout) { 
       fragmentManager.beginTransaction().replace(R.id.content_frame, new FirstFragment()).commit(); 
       setTitle(R.string.speed_graph); 
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
       fab.setVisibility(View.VISIBLE); 
      } else if (id == R.id.nav_second_layout) { 
       fragmentManager.beginTransaction().replace(R.id.content_frame, new SecondFragment()).commit(); 
       setTitle(R.string.drive_player); 
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
       fab.setVisibility(View.VISIBLE); 
      } else if (id == R.id.nav_third_layout) { 
       fragmentManager.beginTransaction().replace(R.id.content_frame, new ThirdFragment()).commit(); 
       setTitle(R.string.google_maps); 
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
       fab.setVisibility(View.VISIBLE); 
      } else if (id == R.id.nav_share) { 
       Intent sendIntent = new Intent(); 
       sendIntent.setAction(Intent.ACTION_SEND); 
       sendIntent.putExtra(Intent.EXTRA_TEXT, "Go Check Out All Driving Data in the Play Store!"); 
       sendIntent.setType("text/plain"); 
       startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); 
      } 
+0

Hat es funktioniert, wenn Sie von der Einstellungsseite zur normalen Seite zurückkehren? –

+0

@MohammedAtif Ja es tut, wegen der 'View.VISIBLE' in jedem. Die einzige Sache, mit der ich Probleme habe, ist, zu der ursprünglichen Seite zurückzukehren, zu der man gegangen ist, wenn man den Anmeldebildschirm passiert hat, aber das war ein Problem, bevor man dies umsetzte. –

+0

Das ist es, worüber ich spreche. Ihr Fab-Button ist nur sichtbar, wenn Sie die Seite aus der Schublade auswählen. Es ist nicht sichtbar, wenn Sie auf der Einstellungsseite die Zurück-Taste drücken. Versuchen Sie die Lösung, die ich erwähnte. Es wird dir viel helfen und auch die App für bessere Leistung optimieren. In Ihrer Lösung haben Sie viele unnötige Objekte erstellt, die schließlich auch Ihre App verlangsamen werden. –