سؤال

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top