我想在横向和纵向模式下运行我的应用程序。我已经使用ViewPager
并将FragmentPagerAdapter
在您滑动列表时分离每个片段,但将它们保留在内存中,以便在用户向后滑动时可以简单地重新连接它们。它在纵向模式下正常工作。但是当它进入横向模式时,当我交换屏幕时它只显示第一个片段视图。请给我解决方案。
片段适配器:
public class FragmentAdapter extends FragmentStatePagerAdapter
{
private final int three = 3 ;
public FragmentAdapter(FragmentManager fm)
{
super(fm);
Log.e("adapter", "const");
}
@Override
public Fragment getItem(int position)
{
Log.e("adapter", "getitem");
return ViewFragment.newInstance(position);
}
@Override
public int getCount()
{
Log.v("adpt", "getcount");
return three;
}
}
视图片段:
public final class ViewFragment extends Fragment
{
private int position;
public static ViewFragment newInstance(int position)
{
ViewFragment viewFragment = new ViewFragment();
viewFragment.position = position;
Log.e("newinstance", "pos="+position);
return viewFragment;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
Log.d("oncreateview", ":)");
View view = null ;
if (position == 0)
{
Log.e("oncreateview", "0/"+position);
View v = inflater.inflate(R.layout.first_view_fragment, null) ;
view = v.findViewById(R.id.firstViewFragmentLayout);
}
else if (position == 1 )
{
Log.e("oncreateview", "1/"+position);
View v = inflater.inflate(R.layout.second_view_fragment, null) ;
view = v.findViewById(R.id.secondViewFragmentLayout);
}
else if (position == 2)
{
Log.e("oncreateviewr", "2/"+position);
View v = inflater.inflate(R.layout.third_view_fragment, null) ;
view = v.findViewById(R.id.thirdViewFragmentLayout);
}
return view;
}
}