Android 为什么不能在 Fragment 中使用 ViewPager?它实际上是
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12088983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why it is not possible to use ViewPager within a Fragment? It actually is
提问by Eugene
There are information that it is impossible to use ViewPagerwithin a Fragmentin many sources like "The Busy Coders Guide for Android Developers" by Mark Murphy, or posts like thison SO. I'm confused because I don't have such a problem and I successfully use ViewPagerwithin my Fragment. The only distinction is that I instantiate a ViewPagernot in onCreateView()method but in onActivityCreated(). And everything works perfectly fine.
有信息,这是不可能使用ViewPager内Fragment像“许多来源的忙打码机指南Android开发”由马克·墨菲,或职位像这样的SO。我很困惑,因为我没有这样的问题,而且我ViewPager在我的Fragment. 唯一的区别是我ViewPager不是在onCreateView()方法中而是在onActivityCreated(). 一切正常。
So the question is - may be I just don't know something and this is not recommended for some reason to make UI instantiations in onActivityCreated()? But again - everything works just fine.
所以问题是 - 可能我只是不知道一些事情,并且出于某种原因不建议在 中进行 UI 实例化onActivityCreated()?但同样 - 一切正常。
Here is the listing of the class and xml:
这是类和 xml 的列表:
Class:
班级:
public class ViewPagerFragment extends Fragment {
static final int NUM_ITEMS = 2;
private ViewPagerAdapter mAdapter;
private ViewPager mPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.view_pager_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = new ViewPagerAdapter(getFragmentManager());
mPager = (ViewPager) getView().findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int num) {
if (num == 0) {
return new ItemsListFragment();
} else {
return new FavsListFragment();
}
}
@Override
public int getCount() {
return NUM_ITEMS;
}
}
}
Layout:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
回答by Rajkumar
To implement the View pager within a fragment use getChildFragmentManager()instead of getFragmentManager(). You can call setAdapter()for the ViewPager from onCreateView()or onActivityCreated(), that is not a matter.
要在片段中实现视图分页器,请使用getChildFragmentManager()而不是getFragmentManager()。您可以从onCreateView()或onActivityCreated ()为 ViewPager调用setAdapter(),这不是问题。
I hope this will help you guys.
我希望这会帮助你们。
回答by CommonsWare
UPDATE: Since this answer was originally written, you now can have nested fragments, which means it is possible to have a ViewPageruse fragments for pages andbe in a fragment itself. This sample projectdemonstrates the technique.
更新:由于这个答案最初是写的,你现在可以有嵌套的片段,这意味着可以有一个ViewPager用于页面的片段并在片段本身中。此示例项目演示了该技术。
I now return you to your regularly-scheduled answer, presented in its entirety...
我现在让你回到你定期安排的答案,完整地呈现......
Quoting myself from the book:
引用书中自己的话:
The simplest way to use a ViewPager is to have it page fragments in and out of the screen based on user swipes. This only works if the ViewPager itself is not contained within a fragment, as you cannot have fragments nested inside of other fragments.
使用 ViewPager 的最简单方法是根据用户的滑动让页面片段进出屏幕。这仅在 ViewPager 本身不包含在片段中时才有效,因为您不能将片段嵌套在其他片段中。
Quoting Dianne Hackborn:
引用黛安·哈克伯恩的话:
Nested fragments are not currently supported. Trying to put a fragment within the UI of another fragment will result in undefined and likely broken behavior.
当前不支持嵌套片段。尝试将一个片段放在另一个片段的 UI 中将导致未定义和可能损坏的行为。
It is perfectly possible to put a ViewPagerinside a Fragment, so long as the contentsof the ViewPagerdo not themselves contain fragments. Since the concrete implementations of PagerAdaptersupplied by the Android Support package use fragments, you have to roll your own fragment-less PagerAdapterto put the ViewPagerin a fragment.
这是完全可能把一个ViewPager内部的Fragment,只要内容的ViewPager文件本身并不包含片段。由于PagerAdapterAndroid 支持包提供的具体实现使用片段,因此您必须滚动自己的无片段PagerAdapter以将其ViewPager放入片段中。
I will endeavor to make this point clearer in the next edition of the book (unless you're British, in which case I'll endeavour to make this point clearer :-).
我将努力在本书的下一版中更清楚地说明这一点(除非您是英国人,在这种情况下,我将努力使这一点更清楚:-)。
回答by hims_3009
While Initializing the SetionPageAdapter for ViewPager use "getChildFragmentManager()".
在为 ViewPager 初始化 SetionPageAdapter 时,请使用“getChildFragmentManager()”。
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
回答by mukesh mali
it is possible try to do this code and save view_pager_fragment.xml
可以尝试执行此代码并保存 view_pager_fragment.xml
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
ViewPagerFragment
ViewPagerFragment
ViewPager viewPager;
TabLayout tabLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tracks, container, false);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.tab);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());``
viewPagerAdapter.addFragment(new FirstFragment(), "First");
viewPagerAdapter.addFragment(new SecoundFragment(), "Secound");
viewPagerAdapter.addFragment(new LoginFragment(), "Login");
viewPager.setAdapter(viewPagerAdapter);
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragmentList = new ArrayList<>();
List<String> fragmentTitles = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitles.get(position);
}
public void addFragment(Fragment fragment, String name) {
fragmentList.add(fragment);
fragmentTitles.add(name);
}
}

