在ViewPager显示第一片段是第二个我创建 [英] First fragment shown in ViewPager is the second one I created

查看:153
本文介绍了在ViewPager显示第一片段是第二个我创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的列表,并提供与他们的数据我尝试填充片段(每个片段一个对象)。它工作在方式,它示出了它们,但所显示的第一个是真的在第二和之后我滑动两次到左和回来到第一然后它的存在(第一个)

I have list of objects Offer and with their data I try filling the fragments (one object per fragment). It works in the way that it shows them but the first one that is shown is really the second and after I swipe two times to the left and come back to the first then it is there (the first one)

我知道,这个问题涉及到这一块。

I know that this question relates to this one

<一个href=\"http://stackoverflow.com/questions/27672170/viewpager-first-fragment-shown-is-always-wrong-with-fragmentstatepager\">ViewPager显示第一个片段总是错FragmentStatePager

但问题是自answerd并不能很好理解的,所以我真的需要帮助。

But the question is self answerd and not well understandable so I really need help

这是我的code:
内部片段活动

This is me code: Inside Fragment 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 类是独立的,看起来像这样

And my ImageFragment class is seperate and looks like this

    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 ;
    }

}

推荐答案

我已经修复了这个问题。为什么它被打破的原因是,由于某种原因(我不清楚),你不能(在如ImageFRagment)初始化的片段所需的值,在它的init或工厂方法,并期望稍后使用它,而创建视图保持在发生的顺序。你必须通过参数(包)提供的值作为参数传递给片段。因此,如果适配器有4页内容与A,B,C,D,当你开始的寻呼机,该onCreateView和的onCreate将呼吁元素[0],即。答:在我的情况下,我总是得到B,B,C,D和刷卡回D,C,B,A。

I have fixed this issue. The reason why it was breaking is that for some reason (not clear to me) you cannot initialize values required by the Fragment (ImageFRagment in your eg.) , in it's init or factory method and expect to use it later while creating views to keep the order in place. You MUST supply the values as arguments to the fragment via arguments(bundle). Hence, if adapter has 4 pages with content A, B, C, D when you start the pager, the onCreateView and onCreate will be called for element[0] ie. A. In my case i'd always get B, B, C, D and swiping back D, C, B , A.

所以解决您的code
 `公共静态ImageFragment的init(发售的发售){

so to fix your code `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(),你必须初始化,你需要使用的参数你的看法的值:

then on your onCreate() you must initialize the values that you need for your views using the arguments:

 mName = getArguments().getInt(ARG_NAME);
 content = getArguments().getString(ARG_CONTENT);

在你的onCreateView(),您可以使用辅助函数来检索他们访问这些值。

In your onCreateView(), you can access these values using helper functions to retrieve them.

((的TextView)rootView.findViewById(android.R.id.text1))的setText(的getContent());
        ((的TextView)rootView.findViewById(R.id.article_content))的setText(getPageNumber());

这篇关于在ViewPager显示第一片段是第二个我创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆