-1

ich versuchte, eine Tabbed-Ansicht mit ViewPager und TabLayout in einem fragment zu implementieren, ich es geschafft, das zu tun, aber die Registerkarten sind nicht in der Symbolleiste enthalten, die an der Spitze gelegen ist des Bildschirms. Ich habe den Screenshot von dem, was ich habe, aufgenommen. kann mir bitte jemand sagen, wie ich die tabs in die toolbar bewegen kann und auch die tabs nicht ändern wenn sie berührt werden sie nur ändern, wenn nach links oder rechts swipped.Wie ein TabLayout in einer Symbolleiste in Fragmente enthalten

Ich habe versucht, die android.support.design.widget.TabLayout in android.support.design.widget.AppBarLayout in der tabslayout.xml Datei hinzufügen es einen blauen Hintergrund auf ihm hinzufügen und die Berührungen arbeiten, aber noch ist es nicht in der Symbolleiste enthalten.

Hier sind die Codes.

1.Tabs.java

import android.os.Bundle; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


import java.util.ArrayList; 
import java.util.List; 


public class Tabs extends Fragment { 

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

    private TabLayout tabLayout; 
    private ViewPager viewPager; 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View mekeleLayout = inflater.inflate(R.layout.tabs_layout,container,false); 



     viewPager = (ViewPager) mekeleLayout.findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 

     tabLayout = (TabLayout) mekeleLayout.findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 

     return mekeleLayout; 
    } 
    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager()); 
     adapter.addFrag(new OneFragment(), "RESTAURANTS"); 
     adapter.addFrag(new TwoFragment(), "CAFES"); 
     adapter.addFrag(new ThreeFragment(), "HOTELS"); 
     viewPager.setAdapter(adapter); 
    } 

    class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

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

     @Override 
     public Fragment getItem(int position) { 
      return mFragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFrag(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 
    } 

} 

2.tabslayout.xml

<android.support.design.widget.CoordinatorLayout 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"> 

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



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

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

4.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="com.example.surafel.information.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> 

Antwort

1

Das ist für mich arbeitet.

<?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.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    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" 
     app:layout_collapseMode="pin" 
     app:popupTheme="@style/AppTheme.PopupOverlay" 
     android:background="@color/colorPrimary" 
     android:layout_weight="1"> 
     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="top" 
      app:tabIndicatorColor="@color/colorAccent" 
      app:tabMaxWidth="0dp" 
      app:tabGravity="fill" 
      app:tabMode="scrollable" 
      android:background="@color/colorPrimary" /> 
    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/tabanim_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

Ja das scheint die Antwort zu sein, vielen Dank – surafel

0

Sie sollten ein Thema für die Aktivität verwendet werden, die keine Aktionsleiste hat.

+0

Es tut mir leid ich bin neu auf Android können Sie Ihre Antwort ein wenig in detail.thanks – surafel

+0

Im Aktivitäts-Tag der Haupttätigkeit des Manifests Fügen Sie diesen Code „" –

+0

Verwenden Sie keine Aktivität am Verwenden von Fragmenten – surafel

0

Versuchen unter einem

<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="fill_parent" 
> 

<android.support.design.widget.CoordinatorLayout 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"> 

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

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.v7.widget.Toolbar 
        android:id="@+id/app_bar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        android:minHeight="?android:attr/actionBarSize" 
        app:layout_scrollFlags="scroll|enterAlways" 
        app:theme="@style/appBar"> 

      </android.support.v7.widget.Toolbar> 

      <android.support.design.widget.TabLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:tabBackground="@color/app_bar_color" 
       app:tabGravity="center" 
       app:tabMode="scrollable" /> 
     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/viewPagerTheme" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
    </LinearLayout> 
</android.support.design.widget.CoordinatorLayout> 

AppBar Thema in Ihrem styles.xml hinzufügen

<style name="appBar" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="android:textColorPrimary">@android:color/black</item> 
    <item name="android:textColorSecondary">@android:color/white</item> 

</style> 
+0

Dies löst das Touch-Problem, es fügt auch einen Hintergrund wie die Symbolleiste, aber es ist noch nicht enthalten. – surafel

+0

check out mein bearbeitetes post in dem ich bearbeite appBar theme nur in styles.xml –

+0

wenn seine arbeit so accpt meine antwort danke! –

Verwandte Themen