TabLayout中的标签不显示

时间:2017-02-08 22:04:55

标签: android android-layout android-fragments android-tablayout android-appbarlayout

我需要在“容器活动”中的选项卡“布局”中显示选项卡,但不显示选项卡。如果它有帮助,该项目还有三个活动,即登录活动,地图活动,注册活动。

注意:地图片段与地图活动不同。

Container.java

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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(viewPager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
}

private  void setupViewPager(ViewPager viewPager) {

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFragment(new MapFragment(), "Map");
    adapter.addFragment(new Chat(),"Chat");

    viewPager.setAdapter(adapter);

}

class ViewPagerAdapter extends FragmentPagerAdapter{

    private List<Fragment> mFragment = new ArrayList<>();
    private List<String> mFragmentTitle = new ArrayList<>();

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

    @Override
    public Fragment getItem(int position) { return null; }

    @Override
    public int getCount() {
        return 0;
    }

    public void addFragment(Fragment fragment, String fragmentTitle){

        mFragment.add(fragment);
        mFragmentTitle.add(fragmentTitle);

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

activity_container.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:background="@android:color/darker_gray"
        app:layout_scrollFlags="scroll|enterAlways"
        android:minHeight="22dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


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

</android.support.design.widget.AppBarLayout>

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

聊天片段 Chat.java

public class Chat extends Fragment {

public Chat(){

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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

}

activity_chat.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.downy.gpslocation.Chat">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hey there this is chat"
    android:layout_centerInParent="true"/>
</RelativeLayout>

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

你应该:

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

因为你返回0,说适配器是空的。

此外:

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

返回给定位置的正确片段,而不是null。

答案 1 :(得分:0)

我在您的代码ViewPagerAdapter中看到的内容得到了getCount return 0和getItem return null