2016-10-31 38 views
-2

Ich habe Symbolleiste und framelayout (für Fragmente) in meinem Hauptaktivitätslayout. Ich muss die Symbolleiste in jedem Fragment zeigen. Aber das Problem ist, dass jedes Fragment ein anderes Hintergrundbild hat, das sich dynamisch ändert. Das Symbolleistenmenüsymbol wird zum Öffnen und Schließen der Navigationsleiste verwendet. Wie werde ich mit diesem Problem umgehen?Android Toolbar mit Fragmenten

+0

auf welchen Hintergrund beziehen Sie sich ?? Fragment oder Symbolleiste ?? – Sanjeet

+0

Ich denke, es ist genau dasselbe Szenario, dem ich in letzter Zeit begegnet bin. Benötigte viel Klärung. Sie haben also eine Navigationsleiste und möchten, dass diese Navigationsleiste auf allen Fragmenten angezeigt wird und diese Fragmente aus dem Menü der Navigationsleiste ersetzt werden. Und der Symbolleistenstil hängt vom Fragment ab, aber die Menüelemente in der Symbolleiste sind gleich. Ist das wonach Sie suchen? – Manikanta

+0

@manikanta: Ja, das ist das gleiche Szenario, nach dem ich gesucht habe. Können Sie mir bitte für eine Lösung helfen? –

Antwort

0

Verwenden Sie diesen Code. Ich half

0

Bevor Sie den Code blockieren, lassen Sie uns zunächst verstehen, was wir tun werden.

1. Erstellen Sie eine HomeActivity, die alle Fragmente enthält.

Hier ist die activity_home.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_home" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_home" 
     app:menu="@menu/activity_home_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

Hier ist die app_bar_home.xml

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

die anderen xmls von navigationview - activity_home_drawer.xml enthält Inhalte Navigationsleiste und nav_header_home.xml ist die Navigationsleiste Header-Layout.

2.Let ist gekommen, um Java-Teil des HomeActivity.java

public class HomeActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener{ 

    private HomeBean bean; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     ButterKnife.bind(this); 


     initListener(); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     //this menu contains all the common menu item actions 
     getMenuInflater().inflate(R.menu.home, menu); 
     return true; 
    } 

//common options bar item click here i have notifications icon common for all the fragments 
    @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. 
     switch (item.getItemId()){ 
      case R.id.action_notifications: 
       AppUtils.launchActivity(HomeActivity.this, NotificationActivity.class,intentData); 
       return false; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     // Handle navigation view item clicks here. 
     Fragment selectedFragment = null; 

     switch (item.getItemId()){ 
      case R.id.nav_dashboard: 
        //you can cache this fragment in case of repeated creations 
        selectedFragment = new DashBoardFragment(); 
       } 

       break; 

     } 

     if(selectedFragment != null){ 
      displayFragment(selectedFragment); 
      DrawerLayout drawer = ButterKnife.findById(this,R.id.drawer_layout); 
      drawer.closeDrawer(GravityCompat.START); 
     } 
     return true; 
    } 

    private void displayFragment(Fragment fragment) { 

     if(fragment !=null){ 
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      ft.replace(R.id.content_frame, fragment); 
      ft.commit(); 
     } 
    } 

    @Override 
    public void initToolBar() {} 

    public void setToolbar(Toolbar toolbar){ 
     setSupportActionBar(toolbar); 
     setActionBarToggle(toolbar); 
    } 



    private void setActionBarToggle(Toolbar toolbar) { 
     DrawerLayout drawer = ButterKnife.findById(this, 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(); 
    } 

    @Override 
    public void initListener() { 
     NavigationView navigationView = ButterKnife.findById(this,R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     //as the first item is show by default 
     navigationView.getMenu().getItem(0).setChecked(true); 
    } 


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

} 

nun als Beispiel lassen erkennen, wie die Fragmente geschrieben werden.

public class SettingsFragment extends Fragment { 

    @BindView(R.id.toolbar) 
    Toolbar toolbar; 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_settings,container,false); 
     ButterKnife.bind(this,view); 
     return view; 
    } 


    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     ((HomeActivity)getActivity()).setToolbar(toolbar); 

    } 
} 

entsprechende XML-Fragment fragment_settings.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="Fragment Name"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/notification_appbar" 
     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" 
      app:title="@string/settings"/> 

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

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


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

die entsprechende xml content_settings.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="@dimen/min_widget_padding" 
     android:id="@+id/nsv_settings_content" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

// your content goes here 
</<android.support.v4.widget.NestedScrollView> 

Ich hoffe, das klare Bild gibt, wie in dem Fall zu tun, wenn Sie irgendwelche anderen Zweifel haben, bitte kommentieren.