Android - TabLayout 不明确的行为

Android - TabLayout unclear behavior

我已经通过 TabLayout 和 ViewPager 创建了两个选项卡布局,但是当我在选项卡之间切换时,我似乎总是在查看 TAB1 的布局,而不是切换到 TAB2。我可以看到这是因为 TAB1 的布局中有一个 Spinner,而 TAB2 没有。

如您所见,我在 onTabSelected 中添加了 Log.w 条目,但我从未看到输出,这强化了我并没有真正在两者之间转换的假设选项卡,布局永远不会改变。

我的配置有什么不正确的地方?

代码如下: 主要activityonCreate

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    dbM = DBManager.getInstance(context);
    TaskHashList.Initialize();

    itemListAllTasks = dbM.getAllTasks();
    TaskHashList.addTaskList(itemListAllTasks);

    itemListWaitingTasks = dbM.getSortedTasks(Sorting.fromInteger(Sorting.WAITING.ordinal()));
    TaskHashList.addTaskList(itemListWaitingTasks);

    tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("All Tasks"));
    tabLayout.addTab(tabLayout.newTab().setText("Waiting"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PageAdapter adapter = new PageAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            Log.w("changed tab", "");
            viewPager.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });
    adapter.notifyDataSetChanged();
}

我的 PageAdapter

public class PageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

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

@Override
public Fragment getItem(int position) {
    Fragment fragment;
   switch (position) {
        case 0:
            fragment = new AllTasksTabFragment();
        case 1:
            fragment = new WaitingTasksTabFragment();
        default:
            fragment = new AllTasksTabFragment();
    }
    Bundle bundle = new Bundle();
    bundle.putInt("position",position);
    fragment.setArguments(bundle);
    return fragment;
}

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

TAB1 布局 - 所有任务

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_marginTop="8dp"
    android:id="@+id/Main2ActivitylinearLayout3">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="right"
        android:paddingRight="10dp"
        android:text=""
        android:id="@+id/allt_tab_totalTask"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:id="@+id/Main2ActivitylinearLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:paddingLeft="5dp"
        android:paddingRight="8dp"
        android:text="Sort:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/sort_array"
        android:id="@+id/all_tasks_sortSpinner"
        android:gravity="center"
        android:textAlignment="center"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:id="@+id/Main2ActivitylinearLayout2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="No Tasks to Display"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="60dp"
        android:id="@+id/alltab_emptylist"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:textStyle="bold" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/alltasks_listView"
        android:layout_centerHorizontal="true" />
</LinearLayout>

TAB2 布局 - 等待任务

    <?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:layout_marginTop="8dp"
    android:id="@+id/Main2ActivitylinearLayout1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="right"
        android:paddingRight="10dp"
        android:text=""
        android:id="@+id/waitt_tab_totalTask"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:id="@+id/Main2ActivitylinearLayout2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="No Tasks to Display"
        android:gravity="center"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="60dp"
        android:id="@+id/wait_tabemptylist"
        android:layout_centerHorizontal="true"
        android:textStyle="bold" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/waitingtasks_listView"
        android:layout_centerHorizontal="true" />
</LinearLayout>

你的 switch case 没有中断。

@Override
public Fragment getItem(int position) {
    Fragment fragment;
   switch (position) {
        case 0:
            fragment = new AllTasksTabFragment();
            break;   // Add this
        case 1:
            fragment = new WaitingTasksTabFragment();
            break;   // Add this
        default:
            fragment = new AllTasksTabFragment();
    }
    Bundle bundle = new Bundle();
    bundle.putInt("position",position);
    fragment.setArguments(bundle);
    return fragment;
}


但我不能假设为什么你的 Log.w 不起作用。
也许您将日志过滤器设置为仅显示错误...