0

我試圖用另一個替換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(); 
} 
+0

你在哪裏調用clearAll標籤內片段()? –

+0

此外,您的addFragment()方法需要在其末尾調用notifyDataSetChanged()以使添加生效。 –

+0

我對代碼進行了修改,爲了測試mFragmentList的大小,我將clearAll()放在了oncreate()中,但是在安裝適配器之後將notifyDataSetChanged()放入之後,仍然給了我一個零。 –

回答

0

從mFragmentList(和mFragmentTitleList)中刪除並添加新的,然後調用notifyDataSetChanged?也會在通知時添加到您的SectionsPageAdapter強制重繪ViewPager

public int getItemPosition(Object object) { 
    return POSITION_NONE; 
} 

不是最有效的,但足以滿足您的需要。對於適配器:

public void removeLastFragment() { 
    if(mFragmentList.size()==0) 
     return; 
    mFragmentList.remove(mFragmentList.size()-1); 
    mFragmentTitleList.remove(mFragmentTitleList.size()-1); 
} 
+0

感謝您的快速響應,但對我而言仍然不清楚,我可以將removeLastFragment放入onClick監聽器中,並將getItemPosition放入適配器中。以及如果我有4個選項卡,並且我想用新的 –

+0

替換第三個片段,並且當我將removeLastFragment放入適配器時出現錯誤:「列表中的add(android.support.v4.app.Fragment)無法應用(int) –

+0

我的錯誤,應該[[刪除]](https://developer.android.com/reference/java/util/ArrayList.html#remove(int)),而不是添加,明顯地...如果你將有更多的選項卡,然後通過索引刪除和添加片段的方法[add(int index,E元素)](https://developer.android.com/reference/java/util/ArrayList.html#add( int,%20E))。使用你的大腦,創建你的邏輯,我已經爲你提供了所有需要的方法 – snachmsm

0

對於零,這是因爲

SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager()); 

重複兩次,我解決我的問題,使用替代