2016-11-12 152 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',而不是手动添加。 –