2017-01-25 6 views
-2

Ich habe ein Projekt mit der Navigation Schublade Vorlage erstellt und ich habe verschiedene Fragmente für jedes Navigationselement. Ich habe eine ActionBar mit Tabs auf einem der Fragmente und ich habe die Breite eingestellt, um Eltern zu entsprechen, aber wenn ich es ausführen, bleibt die Registerkarte nicht an der Aktionsleiste und die Breite entspricht auch nicht dem Elternteil. Überprüfen Sie den Code unten:Android Studio ActionBar mit Tabs in einem Fragment

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.yardbird.justice.yardbird.Fragments.DrinksFragment"> 
<android.support.design.widget.AppBarLayout 
android:layout_height="wrap_content" 
android:layout_width="match_parent" 
android:theme="@style/AppTheme.AppBarOverlay"> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tab_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabMode="fixed" 
    app:tabGravity="fill"/> 

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

<android.support.v4.view.ViewPager 
    android:id="@+id/viewPager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="47dp" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
/> 

Java:

public class DrinksFragment extends Fragment { 


public DrinksFragment() { 
    // Required empty public constructor 
} 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_drink, container, false); 

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout); 
    tabLayout.addTab(tabLayout.newTab().setText("Frabment 1")); 
    tabLayout.addTab(tabLayout.newTab().setText("Frabment 1")); 
    final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager); 
    viewPager.setAdapter(new CustomAdapter(getFragmentManager(), 
      tabLayout.getTabCount())); 

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){ 

     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 

    return view; 
} 

private class CustomAdapter extends FragmentStatePagerAdapter { 
    int numberOfTabs; 

    public CustomAdapter(FragmentManager fragmentManager, int numberOfTabs) { 
     super(fragmentManager); 
     this.numberOfTabs = numberOfTabs; 
    } 

    @Override 
    public Fragment getItem(int position) { 

     switch (position) 
     { 
      case 0: 
       Tab1Fragment tab1 = new Tab1Fragment(); 
       return tab1; 
      case 1: 
       Tab2Fragment tab2 = new Tab2Fragment(); 
       return tab2; 
      default: 
       return null; 
     } 
    } 

    @Override 
    public int getCount() { 

     return numberOfTabs; 
    } 
} 

}

Bildschirm Schuss von der App:

screenshot showing when running

Kann mir jemand helfen beheben, dass?

+0

Ich glaube, Sie brauchen nicht '' AppBarLayout, wenn Sie es innerhalb Fragment verwenden. Verwenden Sie einfach 'Tablayout' mit' viewpager'. –

+0

Das hat nicht funktioniert, ist immer noch das gleiche –

Antwort

0

Versuchen Sie folgendes:

<?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" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
    <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/ThemeOverlay.AppCompat.Light" 
     app:layout_scrollFlags="scroll|enterAlways" /> 
    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</android.support.design.widget.AppBarLayout> 
<android.support.v4.view.ViewPager 
    android:id="@+id/viewPager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</android.support.design.widget.CoordinatorLayout> 
+0

Ich habe das versucht und funktioniert immer noch nicht. Die Tabs sind immer noch an der gleichen Stelle –

Verwandte Themen