2017-03-27 1 views
0

J'ai une activité avec la disposition d'armature. Lorsque vous cliquez sur les options dans le tiroir de navigation, le fragment spécifique est ouvert. J'essayais d'implémenter observablescrollview dans mon fragment. https://github.com/ksoichiro/Android-ObservableScrollView. Je suis capable de l'implémenter mais je ne reçois pas l'icône hamburger dans la barre d'action. Je me suis rendu compte qu'il doit être livré avec le drawertoggle mais cela réside dans ma mainactivité. Comment l'obtenir dans mon fragment? Les utilisateurs peuvent même pas réaliser la présence d'un tiroir de navigation sans elle. Il était disponible lorsque j'utilisais une barre d'outils dans app_bar_main.xml. J'espère avoir été clair. S'il vous plaît laissez-moi savoir si vous avez besoin d'autres informations. Aide un débutant à sortir. Merci d'avance.ObservableScrollView icône hamburger manquant

Voici les captures d'écran.

enter image description here enter image description here

Voici mon code

activity_main.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_main" 
     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_main" 
     app:menu="@menu/activity_main_drawer" /> 

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

app_bar_main.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="net.simplifiedcoding.navigationdrawerexample.MainActivity"> 

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



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

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

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

content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="net.simplifiedcoding.navigationdrawerexample.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 


    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

fragment_menu_1.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.github.ksoichiro.android.observablescrollview.ObservableScrollView 
     android:id="@+id/scroll" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <ImageView 
       android:id="@+id/image" 
       android:layout_width="wrap_content" 
       android:layout_height="@dimen/parallax_image_height" 
       android:scaleType="centerCrop" 
       android:src="@drawable/example" /> 

      <View 
       android:id="@+id/anchor" 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/parallax_image_height" 
       android:minHeight="@dimen/parallax_image_height" /> 

      <TextView 
       android:id="@+id/body" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/anchor" 
       android:background="@android:color/white" 
       android:paddingBottom="@dimen/activity_vertical_margin" 
       android:paddingLeft="@dimen/activity_horizontal_margin" 
       android:paddingRight="@dimen/activity_horizontal_margin" 
       android:paddingTop="@dimen/activity_vertical_margin" 
       android:text="@string/lipsum" /> 
     </RelativeLayout> 

    </com.github.ksoichiro.android.observablescrollview.ObservableScrollView> 

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

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:minHeight="?attr/actionBarSize" 
     app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar" 
     app:theme="@style/Toolbar" /> 
</FrameLayout> 

Menu1.java

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.github.ksoichiro.android.observablescrollview.ObservableScrollView; 
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks; 
import com.github.ksoichiro.android.observablescrollview.ScrollState; 
import com.github.ksoichiro.android.observablescrollview.ScrollUtils; 
import com.nineoldandroids.view.ViewHelper; 


public class Menu1 extends Fragment implements ObservableScrollViewCallbacks { 

    private View mImageView; 
    private View mToolbarView; 
    private ObservableScrollView mScrollView; 
    private int mParallaxImageHeight; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     //returning our layout file 
     //change R.layout.yourlayoutfilename for each of your fragments 
     View v = inflater.inflate(R.layout.fragment_menu_1, container, false); 
     ((AppCompatActivity)getActivity()).setSupportActionBar((Toolbar)v.findViewById(R.id.toolbar)); 
     ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true); 

     mImageView = v.findViewById(R.id.image); 
     mToolbarView = v.findViewById(R.id.toolbar); 
     mToolbarView.setBackgroundColor(ScrollUtils.getColorWithAlpha(0, getResources().getColor(R.color.primary))); 

     mScrollView = (ObservableScrollView) v.findViewById(R.id.scroll); 
     mScrollView.setScrollViewCallbacks(this); 

     mParallaxImageHeight = getResources().getDimensionPixelSize(R.dimen.parallax_image_height); 
     return v; 

    } 


    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     //you can set the title for your toolbar here for different fragments different titles 
     getActivity().setTitle("Menu 1"); 
    } 


    @Override 
    public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) { 
     int baseColor = getResources().getColor(R.color.primary); 
     float alpha = Math.min(1, (float) scrollY/mParallaxImageHeight); 
     mToolbarView.setBackgroundColor(ScrollUtils.getColorWithAlpha(alpha, baseColor)); 
     ViewHelper.setTranslationY(mImageView, scrollY/2); 
    } 

    @Override 
    public void onDownMotionEvent() { 
    } 

    @Override 
    public void onUpOrCancelMotionEvent(ScrollState scrollState) { 
    } 

} 

MainActivity.java

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     //setSupportActionBar(toolbar); 
     //getSupportActionBar().setDisplayHomeAsUpEnabled(true); 



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

     //add this line to display menu1 when the activity is loaded 
     displaySelectedScreen(R.id.nav_menu1); 
    } 

    @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.main, 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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    private void displaySelectedScreen(int itemId) { 

     //creating fragment object 
     Fragment fragment = null; 

     //initializing the fragment object which is selected 
     switch (itemId) { 
      case R.id.nav_menu1: 
       fragment = new Menu1(); 
       break; 
      case R.id.nav_menu2: 
       fragment = new Menu2(); 
       break; 
      case R.id.nav_menu3: 
       fragment = new Menu3(); 
       break; 
     } 

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

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
    } 


    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 

     //calling the method displayselectedscreen and passing the id of selected menu 
     displaySelectedScreen(item.getItemId()); 
     //make this method blank 
     return true; 
    } 


} 

styles.xml

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

    <style name="Toolbar" parent="Theme.AppCompat"> 
     <item name="colorPrimary">@color/primary</item> 
     <item name="colorPrimaryDark">@color/primaryDark</item> 
     <item name="colorAccent">@color/accent</item> 
    </style> 

</resources> 

Répondre

0

Sur votre MainActivity#onCreate, avant d'appeler toggle.syncState(), appelez toggle.setDrawerIndicatorEnabled(true)

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