2016-03-15 4 views
0

Я создал два макета вкладки через TabLayout & ViewPager, но когда я перехожу между вкладками, кажется, что я всегда просматриваю макет TAB1 вместо перехода на TAB2. Я вижу это, поскольку TAB1 имеет Spinner в своем макете, а TAB2 - нет.Android - TabLayout нечеткое поведение

AS вы можете увидеть, что я добавил запись 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

Благодарим за быстрый ответ. –

Смежные вопросы