首页 > 解决方案 > 为什么片段没有显示在tablayout中

问题描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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=".HomeActivity"
    android:orientation="horizontal"
    android:fillViewport="true

    <com.google.android.material.appbar.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/MenuStyle">

            <de.hdodenhof.circleimageview.CircleImageView
                android:layout_height="30dp"
                android:layout_width="30dp"
                android:id="@+id/profile_image" />

            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/display_username"
                android:layout_marginLeft="25dp"
                android:textColor="#ffff"
                android:textStyle="bold"
                android:layout_marginStart="25dp"/>

            </androidx.appcompat.widget.Toolbar>

        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tab_layout"
            android:background="#2F84C8"
            app:tabSelectedTextColor="#fff"
            app:tabIndicatorColor="#f66"
            app:tabTextColor="#fff"/>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewpager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</RelativeLayout>

在xml代码中

TabLayout tabLayout=findViewById(R.id.tab_layout);
ViewPager viewPager=findViewById(R.id.viewpager);
ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());

viewPagerAdapter.addFragment(new ChatsFragment(),"Chats");
viewPagerAdapter.addFragment(new UserFragment(),"Users");

viewPager.setAdapter(viewPagerAdapter);

tabLayout.setupWithViewPager(viewPager);

class ViewPagerAdapter extends FragmentPagerAdapter{

    private ArrayList<Fragment>fragments;
    private  ArrayList<String> titles;

    ViewPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragments=new ArrayList<>();
        this.titles=new ArrayList<>();
    }

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

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

    public  void addFragment(Fragment fragment,String title){
        fragments.add(fragment);
        titles.add(title);
    }

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

在 Java 代码
野兔是截图

它仅显示选项卡和选项卡名称,但不显示选项卡。
它显示标题但不显示片段。
它在标签中显示标题,它们是聊天、用户,但不显示片段。我还有一个工具栏和一个操作栏。请帮我。

标签: androidandroid-fragmentsandroid-tablayout

解决方案


Possibility 1: because you use RelativeLayout without some layout_below tag, the content could be under AppBar

Possibility 2: ViewPager doesn't need android:fillViewport="true" and app:layout_behavior="@string/appbar_scrolling_view_behavior"

Possibility 3: remove android:orientation="horizontal" from RelativeLayout. Is not necessary. Try to change to ConstraintLayout

OR: provide code from your fragment initialisation. Perhaps content is not setted.


推荐阅读