2016-07-18 3 views
1

Je règle l'onglet Balayage sur l'activité qui fonctionne correctement, mais le problème est que la barre d'action disparaît pendant qu'elle était affichée avant l'affichage de l'onglet. S'il vous plaît me guider comment à nouveau apparaître barre d'action en haut de l'onglet.La barre d'action disparaît dans l'onglet Balayage

MainActivity:

public class MainActivity extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Get the view from activity_main.xml 
     setContentView(R.layout.activity_main); 

Toolbar myToolbar = (Toolbar) findViewById(R.id.tool_bar); 
     setSupportActionBar(myToolbar); 

     // Locate the viewpager in activity_main.xml 
     ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 

     ((ViewPager.LayoutParams) (findViewById(R.id.tab_strip)).getLayoutParams()).isDecor = true; 

     // Set the ViewPagerAdapter into ViewPager 
     viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); 
    } 

} 

Tab1: simlar pour tab2 et tab3 FragmentTab1 public class {étend Fragment

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Get the view from fragmenttab1.xml 
     View view = inflater.inflate(R.layout.fragmenttab1, container, false); 
     return view; 
    } 

} 

ViewPageAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter { 

    final int PAGE_COUNT = 3; 
    // Tab Titles 
    private String tabtitles[] = new String[] { "Tab1", "Tab2", "Tab3" }; 
    Context context; 

    public ViewPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

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

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 

      // Open FragmentTab1.java 
     case 0: 
      FragmentTab1 fragmenttab1 = new FragmentTab1(); 
      return fragmenttab1; 

      // Open FragmentTab2.java 
     case 1: 
      FragmentTab2 fragmenttab2 = new FragmentTab2(); 
      return fragmenttab2; 

      // Open FragmentTab3.java 
     case 2: 
      FragmentTab3 fragmenttab3 = new FragmentTab3(); 
      return fragmenttab3; 
     } 
     return null; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return tabtitles[position]; 
    } 
} 

styles.xml:

<!-- 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> 
     <item name="windowActionBar">true</item> 
     <item name="windowNoTitle">false</item> 
    </style> 

Manifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.zohaibsiddique.myapplication"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity android:name=".MainActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/tool_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:elevation="10dp" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     android:minHeight="?attr/actionBarSize" /> 

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v4.view.PagerTabStrip 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tab_strip" 
     android:layout_gravity="top" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp" 
     android:textColor="#000000" /> 

</android.support.v4.view.ViewPager> 
    </LinearLayout> 
+0

Pouvez-vous mettre votre fichier manifeste ici? –

+0

Je l'ai mis. Veuillez voir maintenant –

+0

essayez de changer 'FragmentActivity' en' ActionBarActivity' ou 'AppCompatActivity'. – Lawrance

Répondre

0

Ajouter le PagerTabStrip pour afficher le titre de la page:

<android.support.v4.view.ViewPager 
    android:id="@+id/vpPager" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v4.view.PagerTabStrip 
     android:id="@+id/pager_header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:paddingBottom="4dp" 
     android:paddingTop="4dp" /> 

</android.support.v4.view.ViewPager> 

Pour plus de détails, s'il vous plaît voir ce lien:

https://github.com/codepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter

+0

il ne fonctionne pas –

+0

s'il vous plaît voir le fichier styles.xml, j'ai modifié mon code. –

+0

Voir ma réponse modifiée. –

0

Je résous mon problème par moi-même. Je viens d'étendre MainActivity à AppCompatActivity et de réapparaître la barre d'action.