我的导航抽屉是使用片段构建的。我想在其中一个片段上显示标签。到目前为止,没有任何东西出现。主片段是在导航抽屉上单击主页图标时访问的片段。我试图在home片段下创建两个选项卡而没有任何成功。我已经为每个标签创建了一个类。这些类会扩展各自的布局。
Home Fragment:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState){
View view = inflater.inflate(R.layout.daily_data_fragment, null);
//Adding toolbar to the activity
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
//Initializing the tablayout
tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("ADD DATA"));
tabLayout.addTab(tabLayout.newTab().setText("VIEW ARCHIVE"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//Initializing viewPager
viewPager = (ViewPager) view.findViewById(R.id.pager);
//Creating our pager adapter
DailyDataPageAdapter adapter = new DailyDataPageAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
//Adding adapter to pager
viewPager.setAdapter(adapter);
//Adding onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return view;
}
适配器类:
public class DailyDataPageAdapter extends FragmentStatePagerAdapter {
int tabCount;
public DailyDataPageAdapter(FragmentManager fm, int tabCount){
super(fm);
this.tabCount = tabCount;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
AddData addData = new AddData();
return addData;
case 1:
ViewArchive viewArchive = new ViewArchive();
return viewArchive;
default:
return null;
}
}
@Override
public int getCount() {
return tabCount;
}
}
布局:
<LinearLayout
android:id="@+id/main_layout"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".home_activity">
<!-- our toolbar -->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:visibility="visible"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<!-- our tablayout to display tabs -->
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorgray"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:visibility="visible" />
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:visibility="visible" />
</LinearLayout>
答案 0 :(得分:0)
你可以这样做。
public class HomeFragment extends Fragment {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
// Fragment List
private final List<Fragment> mFragmentList = new ArrayList<>();
// Title List
private final List<String> mFragmentTitleList = new ArrayList<>();
private ViewPagerAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.daily_data_fragment,container,false);
// init view
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
viewPager = (ViewPager) view.findViewById(R.id.pager);
tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// add title to the list
mFragmentTitleList.add("one");
mFragmentTitleList.add("two");
mFragmentTitleList.add("three");
// add fragment to the list
mFragmentList.add(new OneFragment());
mFragmentList.add(new TwoFragment());
mFragmentList.add(new ThreeFragment());
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
// Tab ViewPager setting
viewPager.setOffscreenPageLimit(mFragmentList.size());
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabsFromPagerAdapter(adapter);
return view;
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getChildFragmentManager(), mFragmentList, mFragmentTitleList);
viewPager.setAdapter(adapter);
}
/**
* ViewPagerAdapter setting
*/
class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titleLists) {
super(fm);
this.mFragmentList = fragments;
this.mFragmentTitleList = titleLists;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList == null ? 0 : mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}