2017-11-25 159 views
0

所以,我想構建一個只有一個activity的應用程序,它將使用multiple fragments。我有一個登錄活動,當我登錄後,我去我的maintactivity有一個片段和一個view-pager,所以我可以通過一些片段(這些片段將像一個日曆)掃描。我的導航抽屜會被用來打開其他碎片,與前面提到的不一樣。我的問題是,在我應用View-pager之後,打開抽屜的按鈕不起作用,當我點擊導航抽屜項目時,我的碎片不會改變,將碎片放在頂部(我用滑動移動,因爲按鈕不起作用)。這裏是我的代碼:一個帶有碎片和導航抽屜的活動

MainActivity.java

package amsi.dei.estg.ipleiria.pt.projeto; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentStatePagerAdapter; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
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; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    private static final int NUM_PAGES = 5; 
    private ViewPager mPager; 
    private PagerAdapter mPagerAdapter; 

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

     mPager = (ViewPager) findViewById(R.id.pager); 
     mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 
     mPager.setAdapter(mPagerAdapter); 

     setSupportActionBar(toolbar); 

     final 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 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(); 
     } 
    } 

//since I have two onBackPressed, on for the drawer and other for my pageview, I left the one I thought would be more useful. 

    /*@Override 
    public void onBackPressed() { 
     if (mPager.getCurrentItem() == 0) { 
      // If the user is currently looking at the first step, allow the system to handle the 
      // Back button. This calls finish() on this activity and pops the back stack. 
      super.onBackPressed(); 
     } else { 
      // Otherwise, select the previous step. 
      mPager.setCurrentItem(mPager.getCurrentItem() - 1); 
     } 
    }*/ 

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 
     public ScreenSlidePagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return new Dia(); 
     } 

     @Override 
     public int getCount() { 
      return NUM_PAGES; 
     } 
    } 

    public boolean onNavigationItemSelected(MenuItem item){ 
     int id = item.getItemId(); 
     Fragment fragment = null; 

     if(id==R.id.nav_dia) 
     { 
      fragment = new Dia(); 
     } 
     else if(id==R.id.nav_meusAlimentos) 
     { 
      fragment = new MeusAlimentos(); 
     } 
     else if(id==R.id.nav_login) 
     { 
      Intent login = new Intent(this, LogIn.class); 
      startActivity(login); 
     } 

     if(fragment != null){ 
      FragmentManager fm = getSupportFragmentManager(); 
      FragmentTransaction ft = fm.beginTransaction(); 

      ft.replace(R.id.screen_area, fragment); 

      ft.commit(); 

     } 

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

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.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

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

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="amsi.dei.estg.ipleiria.pt.projeto.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

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

</RelativeLayout> 

Dia.java

package amsi.dei.estg.ipleiria.pt.projeto; 

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

/** 
* Created by nelsu on 24/11/2017. 
*/ 

public class Dia extends Fragment { 

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

     getActivity().setTitle("Day"); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     ViewGroup rootview = (ViewGroup) inflater.inflate(R.layout.activity_dia_content, container, false); 
     return rootview; 
     //return inflater.inflate(R.layout.activity_dia_content, null); 
    } 

} 

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" 
    tools:context="amsi.dei.estg.ipleiria.pt.projeto.MainActivity"> 

    <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_main" /> 

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

activity_dia_content.xml

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="70dp" 
      android:orientation="horizontal" 
      android:clickable="false" 
      android:focusable="false"> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:background="@drawable/rectangle_dia_macros" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/textView4" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="gráficos e percentagem das macros" 
       android:textAlignment="center" 
       android:layout_gravity="center"/> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/textView8" 
       android:layout_width="match_parent" 
       android:layout_height="70dp" 
       android:layout_gravity="center" 
       android:background="@drawable/rectangle_dia_refeicao" 
       android:text="Pequeno Almoço" /> 

      <TextView 
       android:id="@+id/textView7" 
       android:layout_width="match_parent" 
       android:layout_height="70dp" 
       android:layout_gravity="center" 
       android:background="@drawable/rectangle_dia_refeicao" 
       android:text="Café da manhã" /> 

      <TextView 
       android:id="@+id/textView9" 
       android:layout_width="match_parent" 
       android:layout_height="70dp" 
       android:layout_gravity="center" 
       android:background="@drawable/rectangle_dia_refeicao" 
       android:text="Almoço" /> 

      <TextView 
       android:id="@+id/textView10" 
       android:layout_width="match_parent" 
       android:layout_height="70dp" 
       android:layout_gravity="center" 
       android:background="@drawable/rectangle_dia_refeicao" 
       android:text="Lanche" /> 

      <TextView 
       android:id="@+id/textView11" 
       android:layout_width="match_parent" 
       android:layout_height="70dp" 
       android:layout_gravity="center" 
       android:background="@drawable/rectangle_dia_refeicao" 
       android:text="Jantar" /> 

      <TextView 
       android:id="@+id/textView12" 
       android:layout_width="match_parent" 
       android:layout_height="70dp" 
       android:layout_gravity="center" 
       android:background="@drawable/rectangle_dia_refeicao" 
       android:text="Lanche da noite" /> 

      <Space 
       android:layout_width="match_parent" 
       android:layout_height="0dp" /> 

     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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="@dimen/nav_header_height" 
    android:background="@drawable/side_nav_bar" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    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:theme="@style/ThemeOverlay.AppCompat.Dark"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     app:srcCompat="@mipmap/ic_launcher_round" /> 

    <TextView 
     android:id="@+id/label_nome_user" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="Android Studio" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="[email protected]" /> 

</LinearLayout> 

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:showIn="navigation_view"> 

    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_dia" 
      android:icon="@drawable/ic_menu_camera" 
      android:title="Dia" /> 
     <item 
      android:id="@+id/nav_meusAlimentos" 
      android:icon="@drawable/ic_menu_gallery" 
      android:title="Meus Alimentos" /> 
     <item 
      android:id="@+id/nav_login" 
      android:icon="@drawable/ic_menu_slideshow" 
      android:title="Login" /> 
     <item 
      android:id="@+id/nav_manage" 
      android:icon="@drawable/ic_menu_manage" 
      android:title="Tools" /> 
     <item 
      android:id="@+id/nav_settings" 
      android:icon="@drawable/ic_menu_manage" 
      android:title="Settings" /> 
     <item 
      android:id="@+id/nav_about" 
      android:icon="@drawable/ic_menu_manage" 
      android:title="About" /> 
     <item 
      android:id="@+id/nav_quit" 
      android:icon="@drawable/ic_menu_manage" 
      android:title="Quit" /> 
    </group> 
</menu> 

回答

0

你不必爲此編寫自己的代碼。 這是一個不錯的圖書館,適合你所有需要的東西。

https://github.com/chrisbanes/cheesesquare

+0

感謝,但這些代碼的一半是像中國對我而言,我還挺新到Android我無法適應,爲我的項目... –

+0

也,我犯規開放的新片段上點擊使用導航按鈕抽屜 –