我試圖用另一個替換ViewPager中的片段之一。在ViewPager中替換片段
該尋呼機由兩個頁面組成:片段1,片段2 裏面的片段2,有一個按鈕。當我點擊按鈕時,它將用Fragment3替換當前片段Fragment2。對於Fragment2,我希望它被銷燬,不再存在。
實現代碼:
佈局activity_main:
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay"> </android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"/>
main_activity:
private SectionsPageAdapter mSectionsPageAdapter; private ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager()); mViewPager = (ViewPager) findViewById(R.id.container); setupViewPager(mViewPager); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(mViewPager); mSectionsPageAdapter.clearAll(); } private void setupViewPager(ViewPager viewPager) { SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager()); adapter.addFragment(new Tab1Fragment(), "Tab 1"); adapter.addFragment(new Tab2Fragment(), "Tab 2"); adapter.notifyDataSetChanged() viewPager.setAdapter(adapter); }
適配器:
public class SectionsPageAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } public SectionsPageAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } }
Fragment2:
public class Tab2Fragment extends Fragment { private static final String TAG = "Tab2Fragment"; private Button btnTEST; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab2_fragment,container,false); btnTEST = (Button) view.findViewById(R.id.btnTEST2); btnTEST.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getActivity(), "TESTING BUTTON CLICK 2", Toast.LENGTH_SHORT).show(); } }); return view; } }
更新:
我試圖在適配器中實現clearAll方法,我注意到了mFragmentList()返回0爲什麼?
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
Log.d("tmz",mFragmentList.size()+" From addFragment"); // return 1 & 2
}
@Override
public int getCount() {
return mFragmentList.size();
Log.d("tmz",mFragmentList.size()+"getCount "); // return 2
}
public void clearAll(){
Log.d("tmz",mFragmentList.size()+" From addFragment"); // return 0 why ?
for(int i=0; i<mFragmentList.size(); i++)
fragMan.beginTransaction().remove(mFragmentList.get(i)).commit();
mFragmentList.clear();
mFragmentList=new ArrayList<Fragment>();
notifyDataSetChanged();
}
你在哪裏調用clearAll標籤內片段()? –
此外,您的addFragment()方法需要在其末尾調用notifyDataSetChanged()以使添加生效。 –
我對代碼進行了修改,爲了測試mFragmentList的大小,我將clearAll()放在了oncreate()中,但是在安裝適配器之後將notifyDataSetChanged()放入之後,仍然給了我一個零。 –