替换ViewPager中的片段

时间:2017-06-01 16:09:40

标签: android android-fragments android-viewpager android-tablayout

我正在尝试用另一个片段替换ViewPager中的一个片段。

寻呼机由两个页面组成:Fragment1,Fragment2 在fragment2里面,有一个按钮。当我点击按钮时,它将用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.size()返回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();
}

2 个答案:

答案 0 :(得分:0)

从mFragmentList(和mFragmentTitleList)中删除并添加new,然后调用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);
}

答案 1 :(得分:0)

归零是因为

SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());

重复了两次,我在标签

中使用替换片段解决了我的问题