首页 > 解决方案 > 一个 TabLayout 中的相同片段

问题描述

我正在尝试开发一个包含 TabLayout 的活动,它是使用一个 Fragment 构建的,但使用了几次(相同的布局,不同的数据)。但是当我有这样的配置时,我的应用程序甚至不会进入 getItem() 方法,也不会显示我的布局。任何想法问题出在哪里?

我的代码: 活动:

public class IndoorArenaActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_arena);

    TabLayout tabLayout = findViewById(R.id.tabs);
    final ViewPager viewPager = findViewById(R.id.viewPager);
    final PageAdapterArena adapter = new PageAdapterArena
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.addTab(tabLayout.newTab().setText("today"));
    tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
    tabLayout.addTab(tabLayout.newTab().setText("13/11"));
    tabLayout.addTab(tabLayout.newTab().setText("14/11"));
    tabLayout.addTab(tabLayout.newTab().setText("15/11"));
    tabLayout.addTab(tabLayout.newTab().setText("16/11"));
    tabLayout.addTab(tabLayout.newTab().setText("17/11"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    // if (id == R.id.action_settings) {
    //    return true;
    // }

    return super.onOptionsItemSelected(item);
}
}

适配器:

public class PageAdapterArena extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PageAdapterArena(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    Fragment fragment = null;
    switch(position){
        case 0:
            fragment = IndoorArenaFragment.newInstance();
            break;
    }
    return fragment;
}

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

分段:

public class IndoorArenaFragment extends Fragment {

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

public static Fragment newInstance() {
    IndoorArenaFragment fragment = new IndoorArenaFragment();
    return fragment;
}
}

活动布局:

<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:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IndoorArena.IndoorArenaActivity">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    app:tabMode="scrollable"/>

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout" />

片段布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="7:00"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="Olga (Gatsby)"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="8:00"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="9:00"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

应用程序没有显示任何错误,只有片段如下所示: 在此处输入图像描述

我想像这样出现(每个片段):

在此处输入图像描述

标签: androidandroid-fragmentsandroid-tablayout

解决方案


您实际上是在创建适配器时传入NumOfTabs0,这就是为什么您在 中得到一个空视图ViewPager,而getItem不是被调用。要解决此问题,您只需在添加选项卡后移动适配器创建行:

TabLayout tabLayout = findViewById(R.id.tabs);
ViewPager viewPager = findViewById(R.id.viewPager);
tabLayout.addTab(tabLayout.newTab().setText("today"));
tabLayout.addTab(tabLayout.newTab().setText("tomorrow"));
tabLayout.addTab(tabLayout.newTab().setText("13/11"));
tabLayout.addTab(tabLayout.newTab().setText("14/11"));
tabLayout.addTab(tabLayout.newTab().setText("15/11"));
tabLayout.addTab(tabLayout.newTab().setText("16/11"));
tabLayout.addTab(tabLayout.newTab().setText("17/11"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

// Create adapter after adding the tabs
PageAdapterArena adapter = new PageAdapterArena(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

推荐阅读