2016-11-12 26 views
1

我正在學習如何做一個標籤佈局的教程。我不知道我在下面的代碼中做了什麼,它沒有輸出標籤。當應用程序運行時,我看不到選項卡項目。其他一切正常。Android - Tab項目未被創建

Acitvity

public class dashboard extends AppCompatActivity { 

    TabLayout tabs; 
    ViewPager container; 

    private SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    private ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_dashboard); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     tabs = (TabLayout) findViewById(R.id.tabs); 
     tabs.setupWithViewPager(container); 
     tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){ 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       container.setCurrentItem(tab.getPosition()); 
      } 
      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 
       container.setCurrentItem(tab.getPosition()); 
      } 
      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 
       container.setCurrentItem(tab.getPosition()); 
      } 
     }); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_dashboard, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     private static final String ARG_SECTION_NUMBER = "section_number"; 

     public PlaceholderFragment() { 
     } 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false); 
      TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
      textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 
      return rootView; 
     } 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 
     private String fragments [] = {"Completed", "In Progress"}; 
     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      switch(position){ 
       case 0: 
        return new dashboard_completed(); 
       case 1: 
        return new dashboard_in_progress(); 
       default: 
        return null; 
      } 
     } 

     @Override 
     public int getCount() { 
      return fragments.length; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return fragments[position]; 
     } 
    } 
} 

dashboard_in_progress

public class dashboard_in_progress extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.dashboard_in_progress, container, false); 
    } 
} 

dashboard_completed

public class dashboard_completed extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.dashboard_completed, container, false); 
    } 
} 

XML

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

    </android.support.design.widget.TabLayout> 

    <view 
     android:layout_height="match_parent" 
     class="android.support.v4.view.ViewPager" 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_marginTop="48dp"/> 
</android.support.design.widget.CoordinatorLayout> 
+0

該代碼看起來不錯。你可以發佈相應佈局的XML代碼,也許錯誤是在你的XML代碼中? –

+0

標籤有可能存在,但他們只是沒有文字? –

+0

請同時發佈'SectionsPagerAdapter'的代碼。 –

回答

1

這是你的問題。

ViewPager container; 
... 

private ViewPager mViewPager; 
... 

tabs.setupWithViewPager(container); 

你聲明兩個ViewPager S,但你只有一個初始化,並試圖建立的TabLayout與空的。

取出ViewPager container;聲明,以及建立呼叫更改爲:

tabs.setupWithViewPager(mViewPager); 

而且,你不需要自己設置的TabLayoutOnTabSelectedListenersetupWithViewPager()方法將爲您處理。

+0

哦,我的天啊,你是我的英雄,謝謝兄弟,我正在嘗試這個,因爲過去8小時。 –

+1

嘿,沒問題。直到我複製/粘貼自己的代碼才能運行它時,我纔看到它。如果您將'ViewPager'傳遞給它,但是'_c'est la vie_'將會很容易發現'TabLayout'會拋出異常。乾杯! –

0

沒有標籤顯示。使用這個:

tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); 
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); 
+0

'tabs.setupWithViewPager(容器)' - 這些標籤應該來自'PagerAdapter',而不是手動添加。 –