与FragmentPagerAdapter怪异dissappearing片段 [英] Weird dissappearing Fragments with FragmentPagerAdapter

查看:160
本文介绍了与FragmentPagerAdapter怪异dissappearing片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个有效,对于一个很好的用户界面的GridView片段。我已经使用了一些code样品,试图建立这种

我是否真的需要跟踪并保存我的片段的状态取向开关(显然我)还是我失去了一些东西?从它(它作为FragmentPagerAdapter文档)看起来像FragmentPagerAdapter声音保存所有的状态对我来说...

我只想说,这里是我的问题(我有​​4个片段,在他们一些textviews):

1 :当我在倾斜的Fragment0屏幕,基础数据消失,不再出现,直到我搬到Fragment2。

2 :当我倾斜的片段1,它的底层数据屏幕,Fragment0消失,片段1的数据不会再次出现,直到我去Fragment3但Fragment0的数据,当我去Fragment2(只是重新出现像上述情况1)。

3 :当我倾斜的Fragment2,它的底层数据的屏幕和片段1消失,如果我去Fragment3,如果我去Fragment0 Fragment2将重新出现片段1再次出现。

4。:当我倾斜Fragment3只Fragment2消失。

我明明做错事所以这里是我的code:

这是我的onCreate(或它的一些ATLEAST):

  mAdapter =新FragPagerAdapter(getSupportFragmentManager());
    mPager =(ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

这是我FragmentPagerAdapter 的:

 私有类FragPagerAdapter扩展FragmentPagerAdapter {
            公共FragPagerAdapter(FragmentManager FM){
                            超(FM);            }            @覆盖
            公众诠释的getCount(){
                  返回numFrags;
            }            @覆盖
            公共片段的getItem(INT位置){                返回ArrayFragment.newInstance(位置,mItem2.get(位置));            }}

下面是我的片段的(我ArrayAdapter):

 公共静态类ArrayFragment扩展片段{    INT MNUM;
    私人的ArrayList<项目> mItems;
    私人GridAdapter myGridAdapter;
    私有静态的GridView mGridView;    静态的newInstance ArrayFragment(INT NUM,ArrayList的<项目>项目){
        ArrayFragment F =新ArrayFragment();        捆绑ARGS =新包();
        args.putInt(民,NUM);
        args.putSerializable(项目,项目);
        f.setArguments(参数);        返回F;
    }
    @燮pressWarnings(未登记)
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        MNUM = getArguments()!= NULL? getArguments()调用getInt(民):1。
        mItems =(ArrayList的<项目>)(getArguments()= NULL getArguments()getSerializable(项目):!?NULL);
        myGridAdapter =新GridAdapter(getActivity()getApplicationContext(),R.layout.grid_item,mItems);    }
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        //视图V = inflater.inflate(R.layout.fragment_pager_list,集装箱,FALSE);
        视图V = inflater.inflate(R.layout.grid,集装箱,FALSE);
        观看电视= v.findViewById(R.id.text);
        ((的TextView)TV).setText(碎片#+ MNUM);        mGridView =(GridView控件)v.findViewById(R.id.gridview);
        返回伏;
    }    @覆盖
    公共无效onActivityCreated(捆绑savedInstanceState){
        super.onActivityCreated(savedInstanceState);    }    @覆盖
    公共无效调用onStart()
    {
        super.onStart();
        mGridView.setAdapter(myGridAdapter);
        mGridView.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
                    长ARG3){
                Toast.makeText(getActivity(),mItems.get(ARG2).getSecret(),Toast.LENGTH_LONG).show();
            }        });
    }
    公共类GridAdapter扩展ArrayAdapter<项目> {
        私人诠释计数;
        私人LayoutInflater吹气;
        私人上下文的背景下;
        私人的ArrayList<项目>项目;        公共GridAdapter(上下文的背景下,INT资源,
                ArrayList的<项目> items_){
            超(背景下,资源);
            数= items_.size();
            项目= items_;            吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // TODO自动生成构造函数存根
        }        @覆盖
        公众诠释getCount将()
        {
            返回计数;
        }        @覆盖
        公共项目的getItem(INT位置)
        {
             返回items.get(位置);
        }        @覆盖
        众长getItemId(INT位置)
        {
                返回的位置;
        }        公共无效setItems(ArrayList的<项目>的项目)
        {
            this.items =物品;
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup)
        {
                视图V = convertView;
                尝试
                {
                        V = inflater.inflate(R.layout.grid_item,NULL);
                }
                赶上(NullPointerException异常NPE)
                {
                        npe.printStackTrace();                }                项目项目= items.get(位置);                尝试
                {
                        TextView的名字=(TextView中)v.findViewById(R.id.grid_att_name);
                        TextView中值=(TextView中)v.findViewById(R.id.grid_att_value);
                        name.setText(item.getName());
                        value.setText(item.getValue());
                }
                赶上(NullPointerException异常NPE)
                {
                        npe.printStackTrace();                }                返回伏;
        }
    }
}


解决方案

我解决了这个问题,通过使用一个片段的状态适配器,我不得不添加的android:configChanges =方向来活动的活动标签,里面的片段。

I'm trying to build a Fragment that holds a gridview for a nice UI. I have used some of the sample code to try and build this.

Do I really need to keep track and save the state of my fragments for orientation switches (apparently I do) or am I missing something? From the sound of it (it being the FragmentPagerAdapter docs) it seems like the FragmentPagerAdapter saves all states for me...

Suffice to say, here is my problem(I have 4 fragments with some textviews in them):

1. When I tilt the screen on Fragment0, the underlying data disappears and will not reappear until I move to Fragment2.

2. When I tilt the screen on Fragment1, the underlying data of it AND Fragment0 disappears and Fragment1's data wont reappear until I go to Fragment3 BUT Fragment0's data will reappear when I go to Fragment2 (just like case 1 above).

3. When I tilt the screen on Fragment2, the underlying data of it AND Fragment1 disappears and Fragment1 reappears if I go to Fragment3 and Fragment2 will reappear if I go to Fragment0.

4. When I tilt Fragment3 only Fragment2 disappears.

I'm obviously doing something wrong so here is my code:

Here is my onCreate(or some of it atleast):

mAdapter = new FragPagerAdapter(getSupportFragmentManager());
    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

Here is my FragmentPagerAdapter:

private class FragPagerAdapter extends FragmentPagerAdapter {


            public FragPagerAdapter(FragmentManager fm) {
                            super(fm);

            }

            @Override
            public int getCount() {
                  return numFrags;
            }

            @Override
            public Fragment getItem(int position) {

                return ArrayFragment.newInstance(position, mItem2.get(position));

            }

}

Here is my Fragment (with my ArrayAdapter):

public static class ArrayFragment extends Fragment {

    int mNum;
    private ArrayList<Item> mItems;
    private GridAdapter myGridAdapter;
    private static GridView mGridView;

    static ArrayFragment newInstance(int num, ArrayList<Item> items) {
        ArrayFragment f = new ArrayFragment();

        Bundle args = new Bundle();
        args.putInt("num", num);
        args.putSerializable("items", items);
        f.setArguments(args);

        return f;
    }


    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        mItems = (ArrayList<Item>) (getArguments() != null ? getArguments().getSerializable("items") : null);
        myGridAdapter = new GridAdapter(getActivity().getApplicationContext(), R.layout.grid_item, mItems);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
        View v = inflater.inflate(R.layout.grid, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Fragment #" + mNum);

        mGridView = (GridView) v.findViewById(R.id.gridview);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onStart()
    {
        super.onStart();
        mGridView.setAdapter(myGridAdapter); 
        mGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getActivity(), mItems.get(arg2).getSecret(), Toast.LENGTH_LONG).show();                  
            }

        });
    }


    public class GridAdapter extends ArrayAdapter<Item> {
        private int count;
        private LayoutInflater inflater;
        private Context context;
        private ArrayList<Item> items;

        public GridAdapter(Context context, int resource,
                ArrayList<Item> items_) {
            super(context, resource);
            count = items_.size();
            items = items_;

            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount()
        {
            return count;
        }

        @Override
        public Item getItem(int position)
        {
             return items.get(position);
        }

        @Override
        public long getItemId(int position)
        {
                return position;
        }

        public void setItems(ArrayList<Item> items)
        {
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
                View v = convertView;
                try
                {
                        v = inflater.inflate(R.layout.grid_item, null);
                }
                catch (NullPointerException npe)
                {
                        npe.printStackTrace();

                }                    

                Item item = items.get(position);

                try
                {
                        TextView name = (TextView) v.findViewById(R.id.grid_att_name);
                        TextView value = (TextView) v.findViewById(R.id.grid_att_value);


                        name.setText(item.getName());   
                        value.setText(item.getValue());
                }
                catch (NullPointerException npe)
                {
                        npe.printStackTrace();

                }

                return v;
        }
    }
}

解决方案

I fixed this by using a Fragment State Adapter and I had to add android:configChanges="orientation" to the activity tag of the activity that housed the fragments.

这篇关于与FragmentPagerAdapter怪异dissappearing片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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