我正在使用navigationDrawer,它有3个片段。
在其中一个片段中,我想使用滑动标签,它有2个片段。
问题是navDrawer的片段扩展了Fragment
public class Verses extends Fragment {
}
但它滑动标签它需要扩展FragmentActivity
public class Verses extends FragmentActivity {
}
所以我不知道如何解决它。
答案 0 :(得分:0)
At their I/O 2015 conference, Google announced a new design support library, which can resolve your problem.
you just need to add the following dependency to your app.gradle file
compile 'com.android.support:design:22.2.0'
**below is the complete code:**
swipe_tab.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
**fragment_page.xml** file
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
**ViewPagerFragment.java**
public class ViewPagerFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.swipe_tabs,container,false);
ViewPager viewPager = (ViewPager)view. findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getChildFragmentManager(),getActivity()));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
public static ViewPagerFragment newInstance(int sectionNumber) {
ViewPagerFragment fragment = new ViewPagerFragment();
/*Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);*/
return fragment;
}
}
**PageFragment.java**
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
**SampleFragmentPagerAdapter.java**
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 10;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" ,"Tab4","Tab5","Tab6","Tab7" ,"Tab8","Tab9","Tab10"};
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
**At last you need to make following changes in your navigation drawer's MainActivity.java class**
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position) {
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, ViewPagerFragment.newInstance(position + 1))
.commit();
break;
default: fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
}
**Don't forgot to add dependency in your build.gradle:
compile 'com.android.support:design:22.2.0'**