当包含ViewPager的片段可见性设置为GONE或INVISIBLE时,ViewPager中的片段不会被初始化。如果父片段可见,它们似乎只是初始化。我怎样才能解决这个问题?我想要做的就是让ViewPager加载它的片段(不超过两个),即使父片段对于用户来说还不可见。
演示问题的示例项目:
MainActivity:
public class PagerFragment extends Fragment {
ViewPager pager;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
Log.d("PageFragment", "onCreateView");
View result=inflater.inflate(R.layout.pager, container, false);
pager=(ViewPager)result.findViewById(R.id.pager);
pager.setAdapter(buildAdapter());
return(result);
}
private PagerAdapter buildAdapter() {
return(new SampleAdapter(getActivity(), getChildFragmentManager()));
}
}
PagerFragment:
public class SampleAdapter extends FragmentPagerAdapter {
Context ctxt=null;
public SampleAdapter(Context ctxt, FragmentManager mgr) {
super(mgr);
this.ctxt=ctxt;
}
@Override
public int getCount() {
return(10);
}
@Override
public Fragment getItem(int position) {
Log.d("SampleAdapter","getItem");
return(EditorFragment.newInstance(position));
}
@Override
public String getPageTitle(int position) {
return(EditorFragment.getTitle(ctxt, position));
}
}
SampleAdapter:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/tabLayout">
<FrameLayout
android:id="@+id/startTab"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="@+id/editTab"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tabLayout"
android:text="switch"
android:id="@+id/switchButton"/>
</RelativeLayout>
main_layout:
public class StartFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("StartFragment","onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.start, container, false);
Log.d("StartFragment","onCreateView");
return(result);
}
}
StartFragment:
public class EditorFragment extends Fragment {
private static final String KEY_POSITION="position";
static EditorFragment newInstance(int position) {
EditorFragment frag=new EditorFragment();
Bundle args=new Bundle();
args.putInt(KEY_POSITION, position);
frag.setArguments(args);
return(frag);
}
static String getTitle(Context ctxt, int position) {
return(String.format(ctxt.getString(R.string.hint), position + 1));
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("EditorFragment","onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.editor, container, false);
EditText editor=(EditText)result.findViewById(R.id.editor);
int position=getArguments().getInt(KEY_POSITION, -1);
editor.setHint(getTitle(getActivity(), position));
Log.d("EditorFragment","onCreateView");
return(result);
}
@Override
public void onResume() {
super.onResume();
}
}
EditorFragment:
VK_FORMAT_R16_UINT
答案 0 :(得分:1)
想出了解决问题的方法。我向viewpager添加了一个OnAttachStateChangeListener,然后调用setOffscreenPageLimit。这会在ViewPager中触发populate(),即使PagerFragment的可见性设置为GONE,它也会将所有片段添加到适配器。 setOffscreenPageLimit只能在视图附加到窗口后调用,否则populate()将在适配器填充片段之前返回。
PagerFragment:
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View result=inflater.inflate(R.layout.pager, container, false);
final ViewPager pager=(ViewPager)result.findViewById(R.id.pager);
pager.setAdapter(buildAdapter());
pager.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
pager.setOffscreenPageLimit(2);
}
@Override
public void onViewDetachedFromWindow(View v) {
}
});
return(result);
}