ViewPager 中显示的第一个片段是我创建的第二个片段
First fragment shown in ViewPager is the second one I created
我有对象列表,我尝试用它们的数据填充片段(每个片段一个对象)。它以显示它们的方式工作,但显示的第一个实际上是第二个,在我向左滑动两次并返回到第一个之后它就在那里(第一个)
我知道这个问题与这个问题有关
ViewPager first fragment shown is always wrong with FragmentStatePager
但是这个问题是自己回答的,不太好理解,所以我真的需要帮助
这是我的代码:
内部片段 activity
public class OffersDisplay extends FragmentActivity {
/*some inicializations for layout and stuff*/
listOfOffers = (ArrayList<Offer>) getIntent().getSerializableExtra("listOfOffers");
...
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return ITEMS;
}
@Override
public Fragment getItem(int position) {
return ImageFragment.init(listOfOffers.get(position));
}
}
}
我的 ImageFragment class 是独立的,看起来像这样
public class ImageFragment extends Fragment {
//some inits again
public static ImageFragment init(Offer offer) {
// Supply val input as an argument.
name = offer.getName();
description = offer.getDescription();
moreInfoURL = offer.getMoreInfoURL();
return new ImageFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
layoutView = inflater.inflate(R.layout.fragment_image, container,
false);
tv = layoutView.findViewById(R.id.text);
((TextView) tv).setText(name + "\n"+ description);
iview = layoutView.findViewById(R.id.image);
//...some inits
((ImageView) iview).setImageBitmap(pic);
return layoutView ;
}
}
我已经解决了这个问题。它被破坏的原因是由于某种原因(我不清楚)你不能初始化 Fragment 所需的值(例如 ImageFRagment 在你的例子中),在它的 init 或工厂方法中并期望稍后在创建视图时使用它以保持订单到位。您必须通过 arguments(bundle) 将值作为参数提供给片段。因此,如果在启动寻呼机时适配器有 4 个内容为 A、B、C、D 的页面,则将为元素 [0] 调用 onCreateView 和 onCreate,即。 A. 在我的情况下,我总是得到 B、B、C、D 并向后滑动 D、C、B、A。
所以修复你的代码
`public static ImageFragment init(Offer offer) {
ImageFragment fragment = new ImageFragment();
Bundle args = new Bundle();
String name = offer.getName();
String description = offer.getDescription();
args.putString(ARG_NAME, name);
args.putString(ARG_CONTENT, description);
fragment.setArguments(args);
return fragment; }
然后在 onCreate() 上,您必须使用以下参数初始化视图所需的值:
mName = getArguments().getInt(ARG_NAME);
content = getArguments().getString(ARG_CONTENT);
在您的 onCreateView() 中,您可以使用辅助函数访问这些值以检索它们。
((TextView) rootView.findViewById(android.R.id.text1)).setText(getContent());
((TextView) rootView.findViewById(R.id.article_content)).setText(getPageNumber());
我有对象列表,我尝试用它们的数据填充片段(每个片段一个对象)。它以显示它们的方式工作,但显示的第一个实际上是第二个,在我向左滑动两次并返回到第一个之后它就在那里(第一个)
我知道这个问题与这个问题有关
ViewPager first fragment shown is always wrong with FragmentStatePager
但是这个问题是自己回答的,不太好理解,所以我真的需要帮助
这是我的代码: 内部片段 activity
public class OffersDisplay extends FragmentActivity {
/*some inicializations for layout and stuff*/
listOfOffers = (ArrayList<Offer>) getIntent().getSerializableExtra("listOfOffers");
...
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public int getCount() {
return ITEMS;
}
@Override
public Fragment getItem(int position) {
return ImageFragment.init(listOfOffers.get(position));
}
}
}
我的 ImageFragment class 是独立的,看起来像这样
public class ImageFragment extends Fragment {
//some inits again
public static ImageFragment init(Offer offer) {
// Supply val input as an argument.
name = offer.getName();
description = offer.getDescription();
moreInfoURL = offer.getMoreInfoURL();
return new ImageFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
layoutView = inflater.inflate(R.layout.fragment_image, container,
false);
tv = layoutView.findViewById(R.id.text);
((TextView) tv).setText(name + "\n"+ description);
iview = layoutView.findViewById(R.id.image);
//...some inits
((ImageView) iview).setImageBitmap(pic);
return layoutView ;
}
}
我已经解决了这个问题。它被破坏的原因是由于某种原因(我不清楚)你不能初始化 Fragment 所需的值(例如 ImageFRagment 在你的例子中),在它的 init 或工厂方法中并期望稍后在创建视图时使用它以保持订单到位。您必须通过 arguments(bundle) 将值作为参数提供给片段。因此,如果在启动寻呼机时适配器有 4 个内容为 A、B、C、D 的页面,则将为元素 [0] 调用 onCreateView 和 onCreate,即。 A. 在我的情况下,我总是得到 B、B、C、D 并向后滑动 D、C、B、A。
所以修复你的代码 `public static ImageFragment init(Offer offer) {
ImageFragment fragment = new ImageFragment();
Bundle args = new Bundle();
String name = offer.getName();
String description = offer.getDescription();
args.putString(ARG_NAME, name);
args.putString(ARG_CONTENT, description);
fragment.setArguments(args);
return fragment; }
然后在 onCreate() 上,您必须使用以下参数初始化视图所需的值:
mName = getArguments().getInt(ARG_NAME);
content = getArguments().getString(ARG_CONTENT);
在您的 onCreateView() 中,您可以使用辅助函数访问这些值以检索它们。
((TextView) rootView.findViewById(android.R.id.text1)).setText(getContent());
((TextView) rootView.findViewById(R.id.article_content)).setText(getPageNumber());