Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSteve Hunter
57,712 PointsModifying tabs .... SectionsPagerAdapter
Evening!
When I implemented this class, it did not extends FragmentActivity
but just Activity
. I refactored that but this doesn't generate the methods that need to be copied to the SectionsPagerAdapter.java
class.
I appreciate that this is a pretty open-ended question, but I'm not sure what else to say!!
How do I get a copy of the methods that need to be copied given my class hadn't previously inherited them?
I know Ben Jakuben will have a quick fix! ;-)
Thanks!
Steve.
2 Answers
Ben Jakuben
Treehouse TeacherIf I understand correctly, one thing you can try is using Quick Fixes to get some of the code you need. First, have your new SectionsPagerAdapter class extend FragmentPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
You'll get a red line under SectionsPagerAdapter, and if you hover over it you can select the Quick Fix to add the constructor. You'll have another red line appear, and from there you can select the Quick Fix to "add unimplemented methods".
I'm afraid you'll still need to copy some code from the screen for the methods. Don't worry too much about the getItem()
method because we update it completely in the next video.
This code was all generated based on the project template that included a ViewPager. Do you happen to remember which template you selected? Things have changed in the tools, which makes it hard.
For reference, here's my SectionsPagerAdapter. You can also download the project files to get it that way:
package com.teamtreehouse.ribbit.adapters;
import java.util.Locale;
import com.teamtreehouse.ribbit.R;
import com.teamtreehouse.ribbit.R.drawable;
import com.teamtreehouse.ribbit.R.string;
import com.teamtreehouse.ribbit.ui.FriendsFragment;
import com.teamtreehouse.ribbit.ui.InboxFragment;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
protected Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch(position) {
case 0:
return new InboxFragment();
case 1:
return new FriendsFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return mContext.getString(R.string.title_section1).toUpperCase(l);
case 1:
return mContext.getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
Steve Hunter
57,712 PointsI'm getting errors all over the place now - I think I fundamentally did something wrong right at the beginning when MainActivity
was created.
Are the project files that are available under the video examples of the starting files for that video, or the finishing files. In other words, how do I backtrack to the starting position when we started talking about fragments?
I'll bin my project and start again with that!
Thanks.
Steve Hunter
57,712 PointsFigured that out - all sorted now!