为什么不能在 Fragment 中使用 ViewPager?它实际上是 [英] Why it is not possible to use ViewPager within a Fragment? It actually is
问题描述
在诸如Fragment 中使用 ViewPager
="noreferrer">面向 Android 开发人员的繁忙编码人员指南"作者:Mark Murphy,或诸如this之类的帖子.我很困惑,因为我没有这样的问题,而且我在我的 Fragment
中成功地使用了 ViewPager
.唯一的区别是我不是在 onCreateView()
方法中而是在 onActivityCreated()
中实例化了 ViewPager
.一切正常.
There are information that it is impossible to use ViewPager
within a Fragment
in many sources like "The Busy Coders Guide for Android Developers" by Mark Murphy, or posts like this on SO. I'm confused because I don't have such a problem and I successfully use ViewPager
within my Fragment
. The only distinction is that I instantiate a ViewPager
not in onCreateView()
method but in onActivityCreated()
. And everything works perfectly fine.
所以问题是 - 可能我只是不知道某些事情,并且出于某种原因不建议在 onActivityCreated()
中进行 UI 实例化?但同样 - 一切正常.
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.
这里是类和xml的列表:
Here is the listing of the class and xml:
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;
}
}
}
布局:
<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>
推荐答案
UPDATE: 由于这个答案最初是写的,你现在可以有嵌套的片段,这意味着可以有一个 ViewPager
为页面使用片段并且位于片段本身中.这个示例项目演示了该技术.
UPDATE: Since this answer was originally written, you now can have nested fragments, which means it is possible to have a ViewPager
use fragments for pages and be in a fragment itself. This sample project demonstrates the technique.
我现在返回给您定期安排的答案,完整呈现...
I now return you to your regularly-scheduled answer, presented in its entirety...
引用书中的自己的话:
使用 ViewPager 的最简单方法是让它页面片段进出基于用户滑动的屏幕.这只适用于不包含 ViewPager 本身的情况在一个片段内,因为你不能将片段嵌套在其他片段内.
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.
引用 Dianne Hackborn:
目前不支持嵌套片段.尝试将一个片段放在另一个片段的 UI 中将导致未定义和可能损坏的行为.
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.
完全有可能将ViewPager
放在Fragment
中,只要ViewPager
的contents> 本身不包含片段.由于Android Support 包提供的PagerAdapter
的具体实现使用了fragments,所以你必须滚动自己的fragment-less PagerAdapter
来放置ViewPager
在一个片段中.
It is perfectly possible to put a ViewPager
inside a Fragment
, so long as the contents of the ViewPager
do not themselves contain fragments. Since the concrete implementations of PagerAdapter
supplied by the Android Support package use fragments, you have to roll your own fragment-less PagerAdapter
to put the ViewPager
in a fragment.
我将努力在本书的下一版中更清楚地说明这一点(除非你是英国人,在这种情况下我会努力使这一点更清楚:-).
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 :-).
这篇关于为什么不能在 Fragment 中使用 ViewPager?它实际上是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!