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;
}
}
Related
I am trying to make this theme selection with viewpager. I created a static arraylist object and using it in this activity with viewpager. But could not manage the run it properly. It is setting toolbar and status color from the position + 1 value, for the rest of them working correctly. Background,text color and name is correct but toolbar and status bar has a value of next iteration.
/**
* 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 COLORSET_POS = "list_pos";
TextView nick,post,countdown;
FrameLayout frameLayout;
Toolbar toolbar;
Button select;
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(COLORSET_POS, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.colorset_preview_layout, container, false);
int pos = getArguments().getInt(COLORSET_POS);
nick = (TextView) rootView.findViewById(R.id.nick);
post = (TextView) rootView.findViewById(R.id.post);
countdown = (TextView) rootView.findViewById(R.id.countdown);
frameLayout = (FrameLayout) rootView.findViewById(R.id.frame);
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
select = (Button) rootView.findViewById(R.id.select);
if(HelperClass.colorSetArrayList != null)
setColors(HelperClass.colorSetArrayList.get(pos).colorSet());
Toast.makeText(getActivity(), "Position " + pos + " name " + HelperClassHelperClass.colorSetArrayList.get(pos).getName(), Toast.LENGTH_SHORT).show();
return rootView;
}
synchronized void setColors(ColorSet colorSet){
toolbar.setSubtitle(String.format(getString(R.string.online_count), new Random().nextInt(20)));
if (colorSet != null) {
nick.setTextColor(colorSet.textcolor());
post.setTextColor(colorSet.textcolor());
countdown.setTextColor(colorSet.textcolor());
frameLayout.setBackgroundColor(colorSet.backgroundcolor());
toolbar.setBackgroundColor(colorSet.primarycolor());
nick.setText(.getUsername());
select.setTextColor(colorSet.textcolor());
if(colorSet.getName()!=null) post.setText(colorSet.getName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getActivity().getWindow().setStatusBarColor(colorSet.primarydarkcolor());
}
}
}
}
/**
* A {#link FragmentPagerAdapter} that 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);
}
#Override
public int getCount() {
return HelperClass.colorSetArrayList.size();
}
#Override
public CharSequence getPageTitle(int position) {
if(HelperClass.colorSetArrayList==null)
return "null";
else return HelperClass.colorSetArrayList.get(position).getName();
}
}
I've created a tab view by Android Studio, this is the default code for MainActivity.java:
public class MainActivity extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
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);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 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";
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)));
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that 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() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
so I have three xmls:
one.xml
two.xml
fragment_main.xml
I want to change the layout of each tab to an unique xml that I mentioned.
I tried this way:
Added this to my MainActivity.java:
public class FrChange extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.one, container, false);
return rootView;
}
}
and then I changed this:
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)));
return rootView;
to this:
return new FrChange();
But it throws me bunch of errors.
What should I do?
My problem was with the adapter.
Check out link for further information : different layout for each tab
I have a tabbed activity with 1 fragment, this fragment has a button, i want when i click the button it create a new fragment and i can swipe between the two fragments
this is the main activity
public class ActivityBeamRec extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
public static CustomViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_beam_rec);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (CustomViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setPagingEnabled(false);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0 : return PlaceholderFragment.newInstance(position + 1);
// case 1 : return the new fragment ;
}
return null;
}
#Override
public int getCount() {
return 1 ;
}
}
}
this is the fragment i have.
public class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
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) {
final View rootView = inflater.inflate(R.layout.fragment_activity_beam_rec, container, false);
final EditText etxb;
etxb = (EditText)rootView.findViewById(R.id.editText);
final Button buDesign = (Button)rootView.findViewById(R.id.buDesign);
buDesign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double b;
b = Double.valueOf(etxb.getText().toString());
\\here i want the button to create the second fragment and pass the variable d to it
ActivityBeamRec.mViewPager.setPagingEnabled(true); // this is to enable the siwpe between the fragments
ActivityBeamRec.mViewPager.setCurrentItem(2); // ths is to set the new fragment as the current view
}
});
return rootView;
}
}
the second fragment should go through on create view stage after the button is pressed, and please tell where i put each code if there is a way to do this.
ActivityBeamRec:
public class ActivityBeamRec extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
// This list holds the fragments for the pages displayed by view pager.
private List < Fragment > fragments = new ArrayList < Fragment > ();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
// Add the first fragment:
fragments.add(PlaceholderFragment.newInstance(1));
}
private void addFragment(Fragment fragment) {
fragments.add(fragment);
// Notify view pager that the contents of the adapter has changed and that it needs to update
// its pages:
notifyDataSetChanged();
// Since content of view pager has changed, re-wire tab layout:
tabLayout.setupWithViewPager(mViewPager);
// Set the current page in the view pager to the last added fragment:
mViewPager.setCurrentItem(fragments.size() - 1);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
// Set the tab title as the number of the current section:
return "SECTION " + (position + 1);
}
}
/**
* Adds a new fragment (page) to the view pager.
* This method can either be public or package-private (without any modifier) depending on the package
* of 'PlaceholderFragment'. Since you're new to Java please refer the link to access modifiers below.
*
* #param fragment the fragment to be added to the view pager
**/
public void addFragment(Fragment fragment) {
mSectionsPagerAdapter.addFragment(fragment);
}
/**
* Returns the number of the next section (page) to be added.
*
* #return the next section number
*/
public int getNextSectionNumber() {
return mSectionsPagerAdapter.getCount() + 1;
}
}
PlaceholderFragment:
public class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
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) {
final View rootView = inflater.inflate(R.layout.fragment_activity_beam_rec, container, false);
final EditText etxb;
etxb = (EditText) rootView.findViewById(R.id.editText);
final Button buDesign = (Button) rootView.findViewById(R.id.buDesign);
buDesign.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double b;
b = Double.valueOf(etxb.getText().toString());
//here i want the button to create the second fragment and pass the variable d to it
int nextSectionNumber = ((ActivityBeamRec) getActivity()).getNextSectionNumber();
((ActivityBeamRec) getActivity()).addFragment(PlaceholderFragment.newInstance(nextSectionNumber));
}
});
return rootView;
}
}
When the button in the fragment is clicked the following things occur in sequence:
addFragment() method of parent activity is called by passing the fragment instance to be added. This public method is used to access the private member mSectionsPagerAdapter of parent activity. We can do away with this method if mSectionsPagerAdapter is made public or package-private.
SectionsPagerAdapter adds the passed-in fragment to its list of fragments and then notifies the view pager that its data set has changed.
TabLayout is refreshed to accommodate the new fragment (page).
Finally the view pager is made to scroll to the added fragment using the setCurrentItem() method.
Reference:
Java Access Control Modifiers
List Data Structure in Java
In your SectionsPagerAdapter
private ArrayList<Fragment> fragments = new ArrayList<>();
#Override
public int getCount() {
return fragments.size();
}
#Override
public Fragment getItem(int position) {
if(position<fragments.size())
return fragments.get(position);
throw new NullPointerException("No Fragment with this position found.");
}
public void addFragment(Fragment newFragment){
fragments.add(newFragment);
}
In the MainActivity before setting the adapter with mViewPager.setAdapter(mSectionsPagerAdapter); add your first fragment(the PlaceHolder in your case). (or do it in the SectionPagerAdapter's constructor).
And then in your OnClickListener call the SectionPagerAdapter's addFragment method.
EDIT: In MainActivity:
mSectionsPagerAdapter.addFragment(PlaceholderFragment.newInstance(0));
mSectionsPagerAdapter.setAdapter(mSectionsPagerAdapter);
I am new to android. I have a sliding tab layout and a viewpager with a fragment. I have setup a basic link between these to switch tabs for a count of 5(i.e 5 tabs) The problem is that, in each of these tabs, I want to populate a listview using simplecursoradapter, for which I need the position of each fragment so that I could populate it accordingly. I have got the position in the fragment adapter class, but I am unable to pass it to the main activity. Also, How can I access DBadapter inside my fragment class to populate listview from the database? or is there some other way to do this? Below is my code. Any help is greatly appreciated. Thanks in advance.
public class myFragment extends Fragment{
private ListView mainList;
public static myFragment getInstance(int position){
myFragment MyFragment = new myFragment();
Bundle args = new Bundle();
args.putInt("position", position);
MyFragment.setArguments(args);
return MyFragment;
}
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_main_list,container, false);
mainList = (ListView)layout.findViewById(R.id.mainListView);
Bundle bundle = getArguments();
if(bundle!=null){
if(bundle.getInt("position")==0){
mainList.setBackgroundColor(getResources().getColor(R.color.primary500));
}
else if(bundle.getInt("position")==1){
mainList.setBackgroundColor(getResources().getColor(R.color.primary100));
}
else if(bundle.getInt("position")==2){
mainList.setBackgroundColor(getResources().getColor(R.color.primary700));
}
else if(bundle.getInt("position")==3){
mainList.setBackgroundColor(getResources().getColor(R.color.accent));
}
else if(bundle.getInt("position")==4){
mainList.setBackgroundColor(getResources().getColor(R.color.black));
}
}
return layout;
}
}
The Main Activity is as follows:
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ViewPager mPage;
private SlidingTabLayout mTabs;
DBadapter myDb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDB();
toolbar = (Toolbar)findViewById(R.id.app_bar);
mPage = (ViewPager)findViewById(R.id.pager);
mTabs = (SlidingTabLayout)findViewById(R.id.tabs);
mFab = (FloatingActionButton)findViewById(R.id.fab);
setSupportActionBar(toolbar);
mPage.setAdapter(new myPagerAdapter(getSupportFragmentManager()));
mTabs.setViewPager(mPage);
}
public class myPagerAdapter extends FragmentPagerAdapter{
String[] tabs;
public myPagerAdapter(FragmentManager fm) {
super(fm);
tabs = getResources().getStringArray(R.array.tabs);
}
#Override
public Fragment getItem(int position) {
myFragment MyFragment = myFragment.getInstance(position);
return MyFragment;
}
#Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
#Override
public int getCount() {
return 5;
}
}
Use this method :
mPage.setOnPageChangeListener(new SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int pos) {
// pos is the position of fragment that app showing
});
UPDATE
in your view pager adapter :
public Fragment getItem(int position) {
return new myFragment(position);
}
And in your fragment class :
public class myFragment extends Fragment{
private int pos
public void myFragment(int position){
this.pos = position;
}
// other code of your class
I used the generated activity with swipe views and title strip. My code worked with two fragments per activity, however when I add three it force closes.
The error is:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. fragment
Here is my activity class MainMenu:
//imports
public class MainMenu extends FragmentActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
* sections. We use a {#link android.support.v4.app.FragmentPagerAdapter} derivative, which will
* keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
* to switch to a {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new Fragment();
Bundle args = new Bundle();
if(i == 0){
fragment = new InsertCity();
}else if(i == 1){
fragment = new AddFlight();
}else if(i == 2){
fragment = new AddFlight();
}
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.title_section1).toUpperCase();
case 1: return getString(R.string.title_section2).toUpperCase();
case 2: return getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
}
Each fragment is almost identicle so I will only include one of them.
Here is my InsertCity class:
public class InsertCity extends Fragment {
private LinearLayout ll;
public InsertCity() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
TextView usernameInstruction = new TextView(getActivity());
ll.addView(usernameInstruction);
Button btn = new Button(getActivity());
btn.setText("Create Backup");
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
}
});
ll.addView(btn);
// setContentView(ll);
}
// #Override
// public void onDestroyView() {
// super.onDestroyView();
// ViewGroup parentViewGroup = (ViewGroup) ll.getParent();
// if (null != parentViewGroup) {
// parentViewGroup.removeView(ll);
// }
// }
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(ll != null) { return ll; }
return ll;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
I'm pretty sure it has to do with my SectionsPagerAdapter class or removing views, but I can't figure out how to fix it.
Help would be much appreciated.
Thanks so much!