Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:动态添加选项卡的自定义视图(带有ViewPager的TabLayout)_Android_Android Viewpager_Android Tablayout - Fatal编程技术网

Android:动态添加选项卡的自定义视图(带有ViewPager的TabLayout)

Android:动态添加选项卡的自定义视图(带有ViewPager的TabLayout),android,android-viewpager,android-tablayout,Android,Android Viewpager,Android Tablayout,我正在使用ViewPager实现TabLayout,其中没有固定的选项卡,并且随着用户滚动页面而增加。我还必须为每个TabLayout.Tab实现自定义视图。我正在尝试使用以下代码段设置TabLayout private void setupTabLayout() { tabLayout.setupWithViewPager(viewPager); for (int i = 0; i < tabLayout.getTabCount(); i++) { T

我正在使用
ViewPager
实现
TabLayout
,其中没有固定的选项卡,并且随着用户滚动页面而增加。我还必须为每个
TabLayout.Tab
实现自定义视图。我正在尝试使用以下代码段设置
TabLayout

 private void setupTabLayout() {
    tabLayout.setupWithViewPager(viewPager);

    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        View v = adapter.getTabView(i, true);
        tab.setCustomView(v);
    }


    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            tab.setCustomView(adapter.getTabView(tab.getPosition(), true));
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

最初,当应用程序打开时,选项卡设置正确且可见。但是,当我动态添加更多选项卡并滚动时,选项卡的自定义视图不会得到更新。请帮助我为动态添加的选项卡添加自定义视图。

选项卡的内容是什么?更新了问题您的自定义视图包含文本和图标还是什么?您是否为自定义视图进行布局?选项卡的内容是什么?更新了问题您的自定义视图包含文本和图标还是什么?您是否为customView制作布局?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAllCaps="true"
    android:id="@+id/month"
    android:text="SEP"
    android:layout_gravity="center_horizontal"
    android:textColor="#FFF"
    android:textSize="14sp"
    android:gravity="center_horizontal"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/date"
    android:textAllCaps="true"
    android:text="21"
    android:textSize="18sp"
    android:textColor="#FFF"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"/>

 </LinearLayout>
         <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

<ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorHeight="0dp" />
</ViewPager>

</LinearLayout>