2016-03-15 50 views
0

我已經通過TabLayout & ViewPager創建了兩個標籤佈局,但是當我在標籤間切換時,似乎總是查看TAB1的佈局而不是轉換到TAB2。我可以看到,因爲TAB1在其佈局中有一個微調,而TAB2沒有。Android - TabLayout不清楚的行爲

,你可以看到我已經添加Log.w條目onTabSelected但我從來沒有看到輸出,它的強項,我不是真正的選項卡之間轉換的前提,佈局不會改變。

什麼是不正確的我的配置?

下面是代碼: 主要活動的onCreate

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

回答

1

你的開關情況下沒有休息。

@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不起作用。
也許你正在設置日誌過濾器只顯示錯誤...

+0

感謝您的及時響應。 –