Tablayout项目与ViewPager一起添加时消失

时间:2018-04-25 06:41:50

标签: android android-viewpager android-tablayout

我知道这是一个已经被问到的问题,每当我搜索这些问题时,我都没有选项卡和片段。我试图以动态的方式实现相同的逻辑。也就是说,我的标签中的项目是从一个arraylist填充的。

    public void MethodToPopulateTabs(){
        for (int i = 0;i<tabListings.size();i++){
           TabListing l = tabListings.get(i);
           String a = l.getName();
           int b = l.getImage();

           tabList.addTab(tabList.newTab().setText(a).setIcon(b));
           myTabAdapter = new MyTabAdapter(getSupportFragmentManager(),tabListings);
           tabList.setupWithViewPager(viewpager);
           viewpager.setAdapter(myTabAdapter);
        }
   }

这是我的适配器...

public class MyTabAdapter extends FragmentStatePagerAdapter {

     ArrayList<TabListing> tabListings;

     public MyTabAdapter(FragmentManager supportFragmentManager,ArrayList<TabListing> tabListings) {
      super(supportFragmentManager);
      this.tabListings = tabListings;
     }

      @Override
      public Fragment getItem(int position) {
      for (int i = 0;i<tabListings.size();i++){
        OneFragment frag = new OneFragment();
        return frag;
       }
       return null;
      }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return super.getPageTitle(position);
    }
 }

当我没有使用tablayout设置viewpager时,

我的结果是...... Before setting up with viewpager

但是当我使用tablyout设置viewpager时,

我得到.. After Setting Up the ViewPager

我不明白,我可能做错了什么..

更新

活动的XML

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:background="@color/colorBase"
android:fitsSystemWindows="true"
tools:context=".DashBoardActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.TabLayout
    android:id="@+id/tab_list"
    android:layout_width="0dp"
    app:tabMode="scrollable"
    app:tabGravity="fill"
    app:tabTextColor="@android:color/white"
    app:tabSelectedTextColor="@android:color/white"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@android:color/white"
    app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tab_list" />

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/colorGrey"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:padding="8dp"
        android:text="Skip Item Selection"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:padding="8dp"
        android:text="Order"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

更改getItem()

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