Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

android - Fragment + ViewPager fail when orientation change

I have one FragmentActivity, with one ViewPager and one FragmentStatePagerAdapter. When i change the screen orientation screen on viewPager, i can see the viewPager but not the fragments. I test all post on Stackoverflow.

FragmentStatePagerAdapter

public class PageAdapter extends FragmentStatePagerAdapter{
    private static final String[] CONTENT = new String[] { "All", "Installed"};

    List<Application> applicationList;
    public PageAdapter(FragmentManager fm,List<Application> applicationList) {
        super(fm);
        this.applicationList = applicationList;
    }

    @Override
    public Fragment getItem(int position) {
        return ListAppFragment.newInstance(CONTENT[position % CONTENT.length],applicationList);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return CONTENT[position % CONTENT.length].toUpperCase();
    }

    @Override
    public int getCount() {return CONTENT.length;}

}

Fragment

public class ListAppFragment extends Fragment {


    public static final String APPS_LIST = "APPS_LIST";
    public static final String VIEW_TYPE = "VIEW_TYPE";
    private GoogleCardAdapter mGoogleCardAdapter;
    private ListView listView;
    private Utils utils;
    String typeView;

    public static ListAppFragment newInstance(String typeView,List<Application> applicationList) {
        ListAppFragment listAppFragment = new ListAppFragment();
        Bundle bundle = new Bundle();
        bundle.putString(VIEW_TYPE, typeView);
        bundle.putParcelableArrayList(APPS_LIST, (ArrayList<Application>) applicationList);
        listAppFragment.setArguments(bundle);
        return listAppFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        utils = new Utils((Context)getActivity().getApplicationContext());
        typeView = getArguments().getString(VIEW_TYPE);
        View v = inflater.inflate(R.layout.list_app_fragment, container, false);
        listView = (ListView)v.findViewById(R.id.activity_googlecards_listview);
        ArrayList<Application> applicationList =  getArguments().getParcelableArrayList(APPS_LIST);
        setApplicationList(applicationList, typeView);
        return v;
    }

    private void setApplicationList(List<Application> applicationList,String typeView) {

        if(typeView=="Installed"){
           applicationList = utils.getAppsAlreadyInstalled(applicationList);
        }
        mGoogleCardAdapter = new GoogleCardAdapter(getActivity().getApplicationContext(),applicationList);

        AlphaInAnimationAdapter alphaInAnimationAdapter = new AlphaInAnimationAdapter(new AlphaInAnimationAdapter(mGoogleCardAdapter));
        alphaInAnimationAdapter.setInitialDelayMillis(300);
        alphaInAnimationAdapter.setAbsListView(listView);

        listView.setAdapter(alphaInAnimationAdapter);

    }
}

Call to adapter in FragmentActivity:

public void getApplicationListFromApi(List<Application> applicationList) {
        this.applicationList = applicationList;
        pageAdapter= new PageAdapter(getSupportFragmentManager(),this.getApplicationList());
        pager.setAdapter(pageAdapter);

        //Bind the title indicator to the adapter
        TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.titles);
        titleIndicator.setViewPager(pager);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is "android support v4" library, i am using version r7 (in Maven repository is outdated), and this not work. I changed with version r13 and work fine (refactor proyect to Gradle).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...