我有一个应用程序,需要提供一套快速入门的使用配套硬件产品的说明。这些指令是带有ViewPager的简单片段,它可以滚动四个片段并在其中包含图像。我第一次用viewpager实例化片段时,我看到它被调用实例化,然后两次调用获取第1页和第2页的片段,一切都按预期工作。然后,在按下后退按钮返回主应用程序,并再次显示指令后,我看到调用使用ViewPager实例化片段,但没有调用获取第1页和第2页,因此屏幕为空
如果我滚动到第2页(也是空白),我会看到第3页的调用(Android显然正在排队)。然后我滚动到显示的第3页。然后我滑回到第2页,它仍然是空白的,但接着是第1页的调用,所以当我回到第1页时,它最终显示出来。
我无法弄清楚为什么带有ViewPager的新片段在第二次实例化时不会调用获取第1页和第2页。它似乎与它有关联的状态没有得到恢复,但它应该是一个新的实例化。
非常感谢任何帮助!
以下是使用ViewPager的片段代码:
public class InstructionsFragment extends Fragment { private static final String TAG =“InstructionsFragment”;
public InstructionsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreate: " + savedInstanceState);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: " + savedInstanceState);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_instructions, container, false);
// use support.v4 ViewPager, but support.v13 FragmentPagerAdapter
// ViewPager is only in v4, but v13 FragmentPagerAdapter doesn't require support fragments
android.support.v4.view.ViewPager viewPager = (android.support.v4.view.ViewPager) view.findViewById(R.id.viewPager);
viewPager.setAdapter(new android.support.v13.app.FragmentPagerAdapter(getFragmentManager()) {
@Override
public Fragment getItem(int position) {
Log.d(TAG, "getItem: " + position);
switch(position) {
case 0: return ImageFragment.newInstance(R.drawable.instructions0);
case 1: return ImageFragment.newInstance(R.drawable.instructions1);
case 2: return ImageFragment.newInstance(R.drawable.instructions2);
default: return ImageFragment.newInstance(R.drawable.instructions3);
}
}
@Override
public int getCount() {
return 4;
}
});
return view;
}
}
以下是显示图像的片段代码:
public class ImageFragment extends Fragment {
private static final String TAG = "ImageFragment";
private static final String ARG_IMAGE_RESOURCE_ID = "imageResourceId";
private int mImageResourceID;
public ImageFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*/
// TODO: Rename and change types and number of parameters
public static ImageFragment newInstance(int imageResourceID) {
ImageFragment fragment = new ImageFragment();
Bundle args = new Bundle();
args.putInt(ARG_IMAGE_RESOURCE_ID, imageResourceID);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mImageResourceID = getArguments().getInt(ARG_IMAGE_RESOURCE_ID);
}
Log.d(TAG, "onCreate, imageResourceID = " + mImageResourceID);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView, imageResourceID = " + mImageResourceID);
View view = inflater.inflate(R.layout.fragment_image, container, false);
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
imageView.setImageResource(mImageResourceID);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState, imageResourceID = " + mImageResourceID);
outState.putInt(ARG_IMAGE_RESOURCE_ID, mImageResourceID);
}
}
答案 0 :(得分:1)
在您使用
getFragmentManager()
的以下代码上,但在此处 应该使用getChildFragmentManager()
,因为您使用的是{ 片段中的ViewPager
。
viewPager.setAdapter(newandroid.support.v13.app.FragmentPagerAdapter(getFragmentManager()) {
@Override
public Fragment getItem(int position) {
Log.d(TAG, "getItem: " + position);
switch(position) {
case 0: return ImageFragment.newInstance(R.drawable.instructions0);
case 1: return ImageFragment.newInstance(R.drawable.instructions1);
case 2: return ImageFragment.newInstance(R.drawable.instructions2);
default: return ImageFragment.newInstance(R.drawable.instructions3);
}
}
答案 1 :(得分:1)
您正在片段内使用片段,因此必须使用getChildFragmentManager()方法而不是getFragmentManager()或getSupportFragmentManager()。
试试这个:
viewPager.setAdapter(new android.support.v13.app.FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int position) {
Log.d(TAG, "getItem: " + position);
switch(position) {
case 0: return ImageFragment.newInstance(R.drawable.instructions0);
case 1: return ImageFragment.newInstance(R.drawable.instructions1);
case 2: return ImageFragment.newInstance(R.drawable.instructions2);
default: return ImageFragment.newInstance(R.drawable.instructions3);
}
}
@Override
public int getCount() {
return 4;
}
});
如果您需要有关此行为的更多信息,可以阅读官方片段文档https://developer.android.com/reference/android/app/Fragment.html和https://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()。您也可以查看此答案以获得解释:https://stackoverflow.com/a/14775322/5658915