I'm pretty new to android, learning a bit and I've encountered a problem above my googling skills.
I have this:
activity_main layout with toolbar and viewPager
fragment_main with button
Main activity is this, mainly stripped to the problem:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
private FragmentManager supportFragmentManager;
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
Button newFragmentBtn = (Button) rootView.findViewById(R.id.cameraBtn);
newFragmentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// create new fragment with same layout on right so I can swipe to it
}
});
return rootView;
}
public FragmentManager getSupportFragmentManager() {
return supportFragmentManager;
}
}
/**
* returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 3;
}
}
}
So I have 3 fragments now (might as well be any number), but I want to have this not be a fixed number, instead to increase when I click on the button that is inside a fragment, so that when I click on newFragmentBtn, it creates 4 fragments to swipe between, click again, 5 fragments etc...
Thank you for your help.
Add field fragCount to your SectionsPagerAdapter (should extend FragmentStatePagerAdapter), add a setter to change the number of fragments you need, and don't forgot to call notifyDataSetChanged() after you changed the counter:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private int fragCount = 3;//initial number
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position);
}
public void setCount(int fragCount){
this.fragCount = fragCount;
notifyDataSetChanged();
}
@Override
public int getCount() {
return fragCount;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.