2016-07-22 82 views
-1

我創建3個片段使用ViewPager,這是ViewPagerAdapter:Android的碎片無法更新

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 
int mNumOfTabs; 

public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) { 
    super(fm); 
    this.mNumOfTabs = NumOfTabs; 
} 

@Override 
public Fragment getItem(int position) { 

    switch (position) { 
     case 0: 
      FragFile tab1 = new FragFile(); 
      return tab1; 
     case 1: 
      FragAll tab2 = new FragAll(); 
      return tab2; 
     case 2: 
      FragFav tab3 = new FragFav(); 
      return tab3; 
     default: 
      return null; 
    } 
} 

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

}

這是活性1容納3片段:

public class Acc1 extends AppCompatActivity { 

ViewPager view_pager; 

TabLayout tab_layout; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_acc1); 

    tab_layout = (TabLayout) findViewById(R.id.tab_layout); 
    tab_layout.addTab(tab_layout.newTab().setText("File")); 
    tab_layout.addTab(tab_layout.newTab().setText("All")); 
    tab_layout.addTab(tab_layout.newTab().setText("Fav")); 

    view_pager = (ViewPager) findViewById(R.id.viewpager); 

    final ViewPagerAdapter adapter = new ViewPagerAdapter 
      (getSupportFragmentManager(), tab_layout.getTabCount()); 

    view_pager.setAdapter(adapter); 

    view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout)); 

    tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 

     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      view_pager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 
     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 
     } 
    }); 

} 

}

在MainActivity中,轉到Activity1保存3 Fragment。
在Activity1中,Fragment2(FragAll)和Fragment3(FragFav)可以轉到Acctivity2。
在Activity2中可以編輯數據,這個數據會顯示在Fragment3(FragFav)上。

我的問題是:
當活性2編輯數據,然後回來活性1使用backpress,在Fragment3數據(FragFav)不會改變(我認爲應用程序保持碎片狀態)。 (如果從Activity2返回Activity1,選擇Fragment1(FragFile)並選擇Fragment3(FragFav),數據也是如此),但是當離開Acitivity1時使用backpress到MainActivity並再次轉到Activity1,Fragment3(FragFav)將改變)
我知道我需要刷新或重置Fragment3(FragFav)時,從Activity2復出到Activity1,但我不知道如何去。
任何人都可以幫助我嗎?

這是更新我跟着DRV:
我在java文件夾創建AppConstants類:

public class AppConstants { 
public static boolean refreshPage=false; 
public int fragment_position=0; 

}

在活動1,更改代碼:

public class Acc1 extends AppCompatActivity { 

ViewPager view_pager; 

TabLayout tab_layout; 

AppConstants ac; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_acc1); 

    tab_layout = (TabLayout) findViewById(R.id.tab_layout); 
    tab_layout.addTab(tab_layout.newTab().setText("File")); 
    tab_layout.addTab(tab_layout.newTab().setText("All")); 
    tab_layout.addTab(tab_layout.newTab().setText("Fav")); 

    view_pager = (ViewPager) findViewById(R.id.viewpager); 

    final ViewPagerAdapter adapter = new ViewPagerAdapter 
      (getSupportFragmentManager(), tab_layout.getTabCount()); 

    view_pager.setAdapter(adapter); 

    view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout)); 

    tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 

     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      view_pager.setCurrentItem(ac.fragment_position); 
      ac.fragment_position=tab.getPosition(); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 
     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 
     } 
    }); 

} 

@Override 
public void onResume() { 
    super.onResume(); 
    if (AppConstants.refreshPage) { 
     AppConstants.refreshPage = false; 
     finish(); 
     startActivity(getIntent()); 
    } 
} 

在Activity2中,我添加了以下代碼:

@Override 
public void onBackPressed() { 
    Intent setIntent = new Intent(Acc2.this, Acc1.class); 
    AppConstants.refreshPage=true; 
    startActivity(setIntent); 
} 

但是當選擇Fragment2(FragAll)和Fragment3(FragFile)時,我在「view_pager.setCurrentItem(ac.fragment_position);」

回答

0

您應該使用如下一些全局標誌:

public class AppConstants{ 

public static boolean refreshPage=false; 
public static int fragment_position=0; 

} 

在您的活性2的backpress,下面寫:

AppConstants.refreshPage=true; 

在你的活動1中的onResume,下面寫:

if (AppConstants.refreshPage) { 
     AppConstants.refreshPage = false; 
     finish(); 
     startActivity(getIntent()); 
} 

然後在您的Activity1中,保存您選擇的標籤位置如下:

YOURTABLAYOUT.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      AppConstants.fragment_position=tab.getPosition(); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 

,並在其的onCreate如下寫:

YOURVIEWPAGER.setCurrentItem(AppConstants.fragment_position); 
+0

我跟着你,但是當從活動1翻盤使用backpress的活性2,它不顯示舊片段,它顯示片段1( FragFile) – 1234abcd

+0

@ 1234abcd檢查我的答案,你會得到你想要的。 – Drv

+0

什麼是fragment_position? – 1234abcd

0

你可以通過調用片段的方法更新您的數據。

首先在你的碎片如創建一個方法,

public void onUpdateData(Boolean isUpdated) { 
    if (isUpdated) { 
     Toast.makeText(context, "Updated", Toast.LENGTH_SHORT).show(); 
     //update your data 

    } 
} 

然後在活動註冊您的片段,並通過上下文(這)

FragFav mFragFav = new FragFav(this); 

只是不要忘了在片段創建構造函數避免空對象異常。

public FragFav(Context context) { 
    this.context = context; 
} 

無論數據更新和方法將在觸發片段從活動現在調用此方法..

mFragFav.onUpdateData(true); 
0

您可以使用下面的代碼片段: 在使用onPageSelected通知FragmentStatePagerAdapter。

if (mSectionsPagerAdapter != null) { 
          mSectionsPagerAdapter.notifyDataSetChanged(); 
         } 

主要代碼:

SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity(),getChildFragmentManager()); 

      // Set up the ViewPager with the sections adapter. 
      mViewPager = (ViewPager)view.findViewById(R.id.container); 
      mViewPager.setAdapter(mSectionsPagerAdapter); 
      mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
       @Override 
       public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

       } 

       @Override 
       public void onPageSelected(int position) { 
        Log.e("changed","mViewPager "+position); 
        try { 
         if (mSectionsPagerAdapter != null) { 
          mSectionsPagerAdapter.notifyDataSetChanged(); 
         } 
        }catch (Exception e){ 
         e.printStackTrace(); 
        } 
       } 

       @Override 
       public void onPageScrollStateChanged(int state) { 

       } 
      }); 

SectionsPagerAdapter

public class SectionsPagerAdapter extends FragmentStatePagerAdapter { 

     Context mContext; 
     FragmentManager fm1; 
     public SectionsPagerAdapter(Context context, FragmentManager fm) { 
      super(fm); 
      fm1=fm; 
//   if(multiple_actions.isExpanded()){ 
//    multiple_actions.collapse(); 
//   } 
      mContext = context; 
     } 

     @Override 
     public Fragment getItem(int position) { 

      if(mViewPager.getCurrentItem()==0){ 
       return ActivityListing.newInstance(0,"preview"); 
      }else if(mViewPager.getCurrentItem()==1){ 
       return ActivityListing.newInstance(1,"current"); 
      }else if(mViewPager.getCurrentItem()==2){ 
       return ActivityListing.newInstance(2,"next"); 
      }else { 
       return null; 
      } 
     } 
     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 

      switch (position) { 
       case 0: 
        return getActivity().getResources().getString(R.string.preview); 
       case 1: 
        return getActivity().getResources().getString(R.string.current); 
       case 2: 
        return getActivity().getResources().getString(R.string.next); 
      } 
      return null; 
     } 
     @Override 
     public int getItemPosition(Object object) { 
      if(fm1.getFragments().contains(object)) 
       return POSITION_NONE; 
      else 
       return POSITION_UNCHANGED; 
     } 

    }