返回时不会调用viewpager中的片段getItem()方法

时间:2015-09-14 10:03:51

标签: android android-fragments android-viewpager

您好我在使用viewpager在我的app中的片段内显示三个标签。第一次加载时它正常工作但是当从下一个片段返回时,没有调用getitem()方法,并且我以前的数据丢失了。帮帮我。

这是我的viewpager实现

import com.devpoint.rprtjobs.R;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.design.widget.TabLayout;


public class MainActivity extends Fragment {

   // Declaring Your View and Variables

   Toolbar toolbar;
   ViewPager pager;
   SwipeViewPagerAdapter adapter;
   SlidingTabLayout tabs;
   CharSequence Titles[]={"OnGoing","UpComing","Expired"};
   int Numboftabs =3;
private View rootView;

   @SuppressWarnings("deprecation")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        try {
            rootView = inflater.inflate(R.layout.activity_main,
                    container, false);


       adapter =  new SwipeViewPagerAdapter(getActivity().getSupportFragmentManager(),Titles,Numboftabs);

       // Assigning ViewPager View and setting the adapter
       pager = (ViewPager) rootView.findViewById(R.id.pager);
       pager.setAdapter(adapter);

       // Assiging the Sliding Tab Layout View
       tabs = (SlidingTabLayout) rootView.findViewById(R.id.tabs);
       tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

       // Setting Custom Color for the Scroll bar indicator of the Tab View
       tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
           @Override
           public int getIndicatorColor(int position) {
               return getResources().getColor(R.color.tabsScrollColor);
           }
       });

       // Setting the ViewPager For the SlidingTabsLayout
       tabs.setViewPager(pager);

       pager.setOffscreenPageLimit(3);

       pager.setOnPageChangeListener(new OnPageChangeListener() {
            public void onPageScrollStateChanged(int state) {}
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            public void onPageSelected(int position) {

                if(position ==0)
                {
                    SwipeViewPagerAdapter.Pagename = "ListOnGoing";
                }
                else if(position ==1) 
                {
                    SwipeViewPagerAdapter.Pagename = "ListUpComing"; 
                }
                else if(position ==2)
                {
                    SwipeViewPagerAdapter.Pagename = "ListExpired";
                }

                }
        });


   }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return rootView;

   }

}

这是我的addapter

package com.devpoint.tabsswipe;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

/**
 * Created by Edwin on 15/02/2015.
 */
public class SwipeViewPagerAdapter extends FragmentStatePagerAdapter {

    CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
    int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
    public static String Pagename="ListOnGoing";


    // Build a Constructor and assign the passed Values to appropriate values in the class
    public SwipeViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
        super(fm);

        this.Titles = mTitles;
        this.NumbOfTabs = mNumbOfTabsumb;

    }

    //This method return the fragment for the every position in the View Pager
    @Override
    public Fragment getItem(int position) {

        switch (position) {
        case 0:
            //Pagename = "ListOnGoing";
            ListOnGoing mListOnGoing = new ListOnGoing();  
            return mListOnGoing;
        case 1:
            //Pagename = "ListUpComing";
            ListUpComing mListUpComing = new ListUpComing(); 
            return mListUpComing;
        case 2:
            //Pagename = "ListExpired";
            ListExpaire mListExpaire = new ListExpaire();
            return mListExpaire;
        default:
            return null;
    }

    }

    // This method return the titles for the Tabs in the Tab Strip

    @Override
    public CharSequence getPageTitle(int position) {
        return Titles[position];
    }

    // This method return the Number of tabs for the tabs Strip

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

1 个答案:

答案 0 :(得分:0)

boolean isResumedFragment = false;

//in OncreateView Check this

if(rootView!=null){

 isResumedFragment = true;

}

if(isResumedFragment){

//remove all added fragment

getChildFragmentManager().beginTransaction().remove(FRAGMENTINSTANCE)
                    .commitAllowingStateLoss();

}

//after that set adapter in view pager and call 

adapter.notifydatasetchanged();