我正在学习如何进行标签布局的教程。我不知道我在下面的代码中做了什么,它没有输出选项卡。当应用程序运行时,我看不到标签项。其他一切都有效。
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 :(得分:1)
这是你的问题。
ViewPager container;
...
private ViewPager mViewPager;
...
tabs.setupWithViewPager(container);
您声明了两个ViewPager
,但您只是初始化一个,并尝试使用null设置TabLayout
。
删除ViewPager container;
声明,并将设置调用更改为:
tabs.setupWithViewPager(mViewPager);
此外,您无需自己在OnTabSelectedListener
上设置TabLayout
。 setupWithViewPager()
方法将为您处理。
答案 1 :(得分:0)
没有要显示的标签。用这个:
RewriteRule gallery/([^/]+)/? /gallery/?album=$1 [L]