3

我試圖在Android Tablayout和PagerAdapter的每個標籤上加載不同的碎片。選項卡正常工作,但相應的片段不會加載在我的碎片上。以下是代碼:帶有碎片的標籤佈局

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


public class Tab1 extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.tab1, container, false); 
    } 
} 

爲每個片段XML創建共3個選項卡類。這裏是我的片段XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="Tab 1" 
     android:textAppearance="?android:attr/textAppearanceLarge"/> 
</RelativeLayout> 

我的動態類:

import android.app.ActionBar; 
import android.content.Intent; 
import android.support.design.widget.TabLayout; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.widget.Toast; 

import java.util.logging.Logger; 

public class DashboardActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{ 

    //This is our tablayout 
    private TabLayout tabLayout; 

    //This is our viewPager 
    private ViewPager viewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_dashboard); 

     //Adding toolbar to the activity 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     //Initializing the tablayout 
     tabLayout = (TabLayout) findViewById(R.id.tabLayout); 

     //Adding the tabs using addTab() method 
     tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title")); 
     tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title")); 
     tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title")); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

     //Initializing viewPager 
     viewPager = (ViewPager) findViewById(R.id.pager); 

     //Creating our pager adapter 
     Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount()); 

     //Adding adapter to pager 
     viewPager.setAdapter(adapter); 

     //Adding onTabSelectedListener to swipe views 
     tabLayout.setOnTabSelectedListener(this); 
    } 

    @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) { 

    } 
} 

我的傳呼機適配器類是:

public class Pager extends FragmentStatePagerAdapter { 

    //integer to count number of tabs 
    int tabCount; 

    //Constructor to the class 
    public Pager(FragmentManager fm, int tabCount) { 
     super(fm); 
     //Initializing tab count 
     this.tabCount= tabCount; 
    } 

    //Overriding method getItem 
    @Override 
    public Fragment getItem(int position) { 
     //Returning the current tabs 
     switch (position) { 
      case 0: 
       Tab1 tab1 = new Tab1(); 
       return tab1; 
      case 1: 
       Tab2 tab2 = new Tab2(); 
       return tab2; 
      case 2: 
       Tab3 tab3 = new Tab3(); 
       return tab3; 
      default: 
       return null; 
     } 
    } 

    //Overriden method getCount to get the number of tabs 
    @Override 
    public int getCount() { 
     return tabCount; 
    } 
} 

活動儀表板佈局文件:

<android.support.design.widget.TabLayout 
    android:id="@+id/tabLayout" 
    android:layout_width="368dp" 
    android:layout_height="wrap_content" 
    app:tabMode="fixed" 
    tools:layout_editor_absoluteX="8dp" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginTop="8dp" 
    android:onClick="loadOption"/> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="@android:color/white" 
    tools:layout_editor_absoluteY="0dp" 
    tools:layout_editor_absoluteX="8dp" 
    tools:ignore="MissingConstraints" /> 

Styles.xml文件:

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- 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" /> 

</resources> 

任何建議。

+0

你確定你的'activity_dashboard'佈局是正確的嗎?也就是說,'ViewPager'是可見的,沒有被任何東西覆蓋等。另外,你確定'Fragment'佈局中的'TextView'具有不與背景混合的文本顏色嗎? –

+0

不確定。不過,我添加了有問題的樣式和佈局文件代碼。 –

+0

將'ViewPager'上的'layout_width'改爲'match_parent'。另外,我還沒有使用'ConstraintLayout',所以我不確定權重甚至在那裏工作。您可能還需要在其上設置一個非零的'layout_height'。 –

回答

1

加入這行,你是好去

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
      tabLayout)); 
+0

通過添加onCreate方法沒有運氣。 –

0

使用tabLayout.setupWithViewPager(viewPager)

............. 
    ..................... 

    //Adding adapter to pager 
    viewPager.setAdapter(adapter); 

    tabLayout.setupWithViewPager(viewPager) 

    //Adding onTabSelectedListener to swipe views 
    tabLayout.setOnTabSelectedListener(this); 

    ........... 
    .................... 
2

您需要添加下面一行onCreate()方法你的活動:

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
1

你已經做得很好,但是你錯過了綁定viewpag的一行呃與標籤佈局或顯示選項卡,

tabLayout.setupWithViewPager(viewPager); 

試試這可能會幫助你。

0

試試這個,希望它對你有幫助。

PagerAdapter類

public class HomeViewPagerAdapter extends FragmentPagerAdapter { 
private List<Fragment> mFragmentList = new ArrayList<>(); 
private List<String> mTitleList = new ArrayList<>(); 

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

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


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

public void addFragment(Fragment fragment, String title) { 
    mFragmentList.add(fragment); 
    mTitleList.add(title); 
} 

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

}

MyActivity類

HomeViewPagerAdapter homeViewPagerAdapter = new HomeViewPagerAdapter(getChildFragmentManager()); 

    // add whichever fragments you want to add 
    homeViewPagerAdapter.addFragment(new Fragment(), "yourfragment"); 

    viewpager.setAdapter(homeViewPagerAdapter); 
    viewpager.setOffscreenPageLimit(2); 
    tabLayout.setupWithViewPager(viewpager); 
2

如果它不存在,把你的主要佈局的LinearLayout或其他的ViewGroup內。如果您使用LinearLayout:

viewpager的layout_height是0dp。 將其更改爲match_parent以及layout_width

取出linear_layout weight="1"

您還可以添加android:layout_marginTop="?android:actionBarSize"的觀點尋呼機是操作欄下。