片段中的选项卡布局(片段将不会显示)

时间:2019-02-03 13:13:49

标签: android android-fragments

我正在尝试在片段中制作选项卡布局,并在其中放置一些片段。 标签将被点击, 但它们不显示片段,那里什么也没有。 这是我的代码:

查看寻呼机适配器:

public class PrViewPAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> frgList = new ArrayList<>();
private final List<String> titleList = new ArrayList<>();
public PrViewPAdapter(@NonNull FragmentManager fm) {
    super(fm);
}
public void addfrg(Fragment frg,String title){
    frgList.add(frg);
    titleList.add(title);
}

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

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

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

这是主要片段

public class profile extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootview =  inflater.inflate(R.layout.activity_profile,container,false);
   final TabLayout tabLayout = (TabLayout) rootview.findViewById(R.id.tabLayout);
   final ViewPager viewPager = (ViewPager) rootview.findViewById(R.id.view_pager);
    PrViewPAdapter adapt = new PrViewPAdapter(getChildFragmentManager());
    adapt.addfrg(new neveshteFragment(),"نوشته ها");
    adapt.addfrg(new rezome(),"رزومه");
    adapt.addfrg(new nemonekarFragment(),"نمونه کار");
    adapt.addfrg(new licenseFragment(),"گواهینامه ها");
    viewPager.setAdapter(adapt);
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });

     return rootview;
}
}

这是xml文件

    <com.google.android.material.tabs.TabLayout
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/pureblack"
        android:layoutDirection="rtl">
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout">

    </androidx.viewpager.widget.ViewPager>

1 个答案:

答案 0 :(得分:1)

问题是您的布局xml不正确,因为它看起来像ConstraintLayout包装TabLayoutViewPager,但是2个视图未正确设置其约束。您可以通过以下方式对其进行设置:

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layoutDirection="rtl"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/linearLayout"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/black"
        >
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout"
        >

    </androidx.viewpager.widget.ViewPager>