在viewpager中保存和恢复片段状态

时间:2017-07-24 10:44:57

标签: android android-fragments fragmentpageradapter

这可能是一个重复的问题,但似乎没有提供的答案可以解决我的问题。所以我有一个extend s ViewPager的课程,在水平导航中,我有3页。在其中一个Fragment onActivityCreated()方法中,我正在调用服务器,获取一些数据并在TableRow中插入TableLayout。现在我想保存这个确切的状态,并在导航回相同的Fragment时恢复它。

我注意到的一个奇怪的事情是,当我从一个Tab滑动到下一个然后再回到第一个时,第一个Tab的状态没有变化。然而,在从第一个导航到第二个到第三个然后再回到第一个时,我失去了状态。是否有任何方法可以保存Fragment的状态,然后在使用onSaveInstanceState()之后将其恢复?因为这需要我手动存储Bundle中的所有数据。这是我的Fragment

public static class ProfileFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        View fragmentView = inflater.inflate(R.layout.fragment_profile, container, false);
        return fragmentView;
    }

    private final int EMPLOYER_TABLE_ROWS = 3;
    private final int STUDENT_TABLE_ROWS = 7;
    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        HasuraClient client = Hasura.getClient();

        // make network calls to fetch data from server

        HasuraUser user = client.getUser();
        String[] role = user.getRoles();

        if(role[1].equals(getString(R.string.student_role).toLowerCase())){
            StudentProfileQuery query = new StudentProfileQuery(user.getId());
            client.asRole(getString(R.string.student_role).toLowerCase()).useDataService()
                    .setRequestBody(query)
                    .expectResponseType(ProfileResponse.class)
                    .enqueue(new Callback<ProfileResponse, HasuraException>() {
                        @Override
                        public void onSuccess(ProfileResponse response) {
                            // Handle response

                            // get headingContainer
                            LinearLayout headingContainer = (LinearLayout) getView().findViewById(R.id.name_and_institute);
                            // get inflater object
                            LayoutInflater inflater = LayoutInflater.from(getContext());

                            // place name
                            TextView headingName = (TextView) inflater.inflate(R.layout.profile_page_title, headingContainer, false);
                            headingName.setText(response.getName());
                            headingContainer.addView(headingName, 0);

                            // place instituteName
                            TextView headingInstitution = (TextView) inflater.inflate(R.layout.profile_page_title, headingContainer, false);
                            headingInstitution.setText("Student" + " at " + response.getInstitution());
                            headingContainer.addView(headingInstitution, 1);

                            String[] field = {"Email", "Date Of Birth", "Gender", "Year of Admission", "Year of Passing", "Percentage",
                                    "Resume URL"};
                            String[] detail = {response.getEmail(), response.getDob(), response.getGender(),
                                    response.getYear_of_admission().toString(), response.getYear_of_passing().toString(),
                                    response.getPercentage().toString(), response.getPath_to_cv()};

                            // insert TableRows in TableLayout
                            // get TableLayout container
                            TableLayout tableContainer = (TableLayout) getView().findViewById(R.id.profile_table);

                            for(int i = 0; i < STUDENT_TABLE_ROWS; i++){
                                // inflate TableRow
                                TableRow row = (TableRow) inflater.inflate(R.layout.profile_page_table, tableContainer, false);

                                // inflate element of col0 and set field, width
                                TextView col0Element = (TextView)inflater.inflate(R.layout.profile_table_row_element, row, false);
                                col0Element.setLayoutParams(new ViewGroup.LayoutParams(R.dimen.profile_col_0, ViewGroup.LayoutParams.WRAP_CONTENT));
                                col0Element.setText(field[i]);

                                // inflate element of col1 and set detail, width
                                TextView col1Element = (TextView)inflater.inflate(R.layout.profile_table_row_element, row, false);
                                col1Element.setLayoutParams(new ViewGroup.LayoutParams(R.dimen.profile_col_1, ViewGroup.LayoutParams.WRAP_CONTENT));
                                col1Element.setText(detail[i]);

                                // add col0 element to TableRow
                                row.addView(col0Element, 0);

                                // add col1 element to TableRow
                                row.addView(col1Element, 1);

                                // add TableRow to TableLayout
                                tableContainer.addView(row, i);
                            }
                        }

                        @Override
                        public void onFailure(HasuraException e) {
                            //Handle error
                            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
        }

        else if(role[1].equals(getString(R.string.employer_role).toLowerCase())){
            EmployerProfileQuery query = new EmployerProfileQuery(user.getId());
            client.asRole(getString(R.string.employer_role).toLowerCase()).useDataService()
                    .setRequestBody(query)
                    .expectResponseType(ProfileResponse.class)
                    .enqueue(new Callback<ProfileResponse, HasuraException>() {
                        @Override
                        public void onSuccess(ProfileResponse response) {
                            // Handle response

                            // get headingContainer
                            LinearLayout headingContainer = (LinearLayout) getView().findViewById(R.id.name_and_institute);
                            // get inflater object
                            LayoutInflater inflater = LayoutInflater.from(getContext());

                            // place name
                            TextView headingName = (TextView) inflater.inflate(R.layout.profile_page_title, headingContainer, false);
                            headingName.setText(response.getName());
                            headingContainer.addView(headingName, 0);

                            // place company name and designation
                            TextView headingCompany = (TextView) inflater.inflate(R.layout.profile_page_title, headingContainer, false);
                            headingCompany.setText(response.getDesignation() + " at " + response.getCompany());
                            headingContainer.addView(headingCompany, 1);

                            String[] field = {"Name", "Email", "Date Of Birth", "Gender"};
                            String[] detail = {response.getName(), response.getEmail(), response.getDob(), response.getGender()};

                            // insert TableRows in TableLayout
                            // get TableLayout container
                            TableLayout tableContainer = (TableLayout) getView().findViewById(R.id.profile_table);

                            for(int i = 0; i < EMPLOYER_TABLE_ROWS; i++){
                                // inflate TableRow
                                TableRow row = (TableRow) inflater.inflate(R.layout.profile_page_table, tableContainer, false);

                                // inflate element of col0 and set field, width
                                TextView col0Element = (TextView)inflater.inflate(R.layout.profile_table_row_element, row, false);
                                col0Element.setLayoutParams(new ViewGroup.LayoutParams(R.dimen.profile_col_0, ViewGroup.LayoutParams.WRAP_CONTENT));
                                col0Element.setText(field[i]);

                                // inflate element of col1 and set detail, width
                                TextView col1Element = (TextView)inflater.inflate(R.layout.profile_table_row_element, row, false);
                                col1Element.setLayoutParams(new ViewGroup.LayoutParams(R.dimen.profile_col_1, ViewGroup.LayoutParams.WRAP_CONTENT));
                                col1Element.setText(detail[i]);

                                // add col0 element to TableRow
                                row.addView(col0Element, 0);

                                // add col1 element to TableRow
                                row.addView(col1Element, 1);

                                // add TableRow to TableLayout
                                tableContainer.addView(row, i);
                            }
                        }

                        @Override
                        public void onFailure(HasuraException e) {
                            //Handle error
                            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }
}

这是FragmentPagerAdapter

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a UI (defined as a static inner class below).
        switch(position){
            case 0:
                return new ProfileFragment();

            case 1:
                return new MessagesFragment();

            case 2:
                return new HomeFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.profile_tab);
            case 1:
                return getString(R.string.messages_tab);
            case 2:
                return getString(R.string.home_tab);
        }
        return null;
    }
}

除此之外,还有另外两个Fragment的代码,我认为无需发布。 提前谢谢!

1 个答案:

答案 0 :(得分:2)

使用mViewPager.setOffscreenPageLimit(noOfPageYouWantToKeepInMemory);