I'm currently trying to create 3 different tabs with 3 different fragments for the purpose of having different content for each tab. I'm stuck with some error in my code that says :
ARG_SECTION_NUMBER cannot be resolved
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Featured", "Browse", "Following" };
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
//(original coding) return PageFragment.newInstance(position + 1);
//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:
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(fragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
case 1:
Fragment fragment2 = new DummySectionFragment2();
Bundle args2 = new Bundle();
//args2.putInt(fragment2.ARG_SECTION_NUMBER, position + 2);
fragment2.setArguments(args2);
return fragment2;
case 2:
Fragment fragment3 = new DummySectionFragment3();
Bundle args3 = new Bundle();
//args3.putInt(fragment3.ARG_SECTION_NUMBER, position + 3);
fragment3.setArguments(args3);
return fragment3;
default:
return null;
}
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
//(original coding) return tabTitles[position];
Locale l = Locale.getDefault();
switch (position) {
case 0:
return App.getContext().getString(R.string.title_section1).toUpperCase(l);
case 1:
return App.getContext().getString(R.string.title_section2).toUpperCase(l);
case 2:
return App.getContext().getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
Anyone have any idea why ARGS_SECTION_NUMBER cannot be resolved for
fragment.ARG_SECTION_NUMBER
fragment2.ARG_SECTION_NUMBER
fragment3.ARG_SECTION_NUMBER
The DummySectionFragment.java is as follows:
public class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number_1";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
This is because your fragment variable has type Fragment. But constant is defined in DummySectionFragment. So what you need to do is:
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
Remember that when you use constants defined in some class file, there's no need to access them via instance of the class.
Related
First let me try to reproduce my Problem/Question.
My Activity is a TabbedActivity with 3 Tabs, done with a SectionPageAdapter and a FragmentPageAdapter.
On these 3 Tabs the user is input Data. With just swiping the user can change the tab. After the User has filled in all data, he must click on a FloatingActionButton to save the data.
Now there is my Problem i donĀ“t know if i can access these 3 Fragments with the filled in data ?
Can i load all 3 Fragments with the filled in Data, read the data and save it or must i handle the PageChangeListenEvent and saved the Data before i change the Page?
Code:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TravelInfoActivity.PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
return maxTabs;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_FRAGMENT_ID = "fragment_id";
private static int REQUEST_CODE_PLACE = 1;
private View currentRootView;
private View currentClickedView;
#SuppressLint("UseSparseArrays")
private HashMap<Integer, View> hMap = new HashMap<>();
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static TravelInfoActivity.PlaceholderFragment newInstance(int sectionNumber) {
TravelInfoActivity.PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
int fragmentLayout;
switch (sectionNumber) {
case 1:
fragmentLayout = R.layout.fragment_accommodation;
break;
case 2:
fragmentLayout = R.layout.fragment_visited_places;
break;
case 3:
fragmentLayout = R.layout.fragment_additional_info;
break;
default:
fragmentLayout = R.layout.fragment_accommodation;
}
args.putInt(ARG_FRAGMENT_ID, fragmentLayout);
fragment.setArguments(args);
return fragment;
}
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(getArguments().getInt(ARG_FRAGMENT_ID), container, false);
currentRootView = rootView;
switch (getArguments().getInt(ARG_FRAGMENT_ID)) {
// switch handling here, not relevant
}
return rootView;
}
Guys i have done it refreshed my brain ;)
Just save the instance from the Fragment to an array and you can use it later.
I made this in the SectionPageAdapter and ovverided the mehtod instantiateItem().
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private SparseArray<Fragment> registeredFragments = new SparseArray<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
Integer fragmentId = fragment.getArguments().getInt("fragment_id");
registeredFragments.put(fragmentToPageMap.get(fragmentId), fragment);
return fragment;
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
public boolean registeredFragmentExists(int position) {
if(registeredFragments.size() >= position) {
if(registeredFragments.get(position) != null) {
return true;
}
}
return false;
}
#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 TravelInfoActivity.PlaceholderFragment.newInstance(position);
}
#Override
public int getCount() {
return maxTabs;
}
}
Later you can access the fragments rootView with
mSectionsPagerAdapter.getRegisteredFragment(position);
fragment.getActivity().findViewById(id);
I have a weird problem.
In the main activity I use this code:
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = {"Laatste Nieuws", "Uitrukken", "Voertuigen", "Contact", "Top Grossing"};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = NaviContentFragment.newInstance(position);
break;
case 1:
fragment = NaviContentFragmentTwo.newInstance(position);
break;
case 2:
fragment = NaviContentFragment.newInstance(position);
break;
case 3:
fragment = NaviContentFragment.newInstance(position);
break;
case 4:
fragment = NaviContentFragment.newInstance(position);
break;
default:
break;
}
return fragment;
}
}
So when I open a tab it will open a new fragment. And when I open tab 1 labeled "Uitrukken" it should open a other fragment than the other ones. This fragment:
public class NaviContentFragmentTwo extends Fragment {
private static final String ARG_POSITION = "position";
public static NaviContentFragment newInstance(int position) {
NaviContentFragment fragment = new NaviContentFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = rootView = inflater.inflate(R.layout.navi_content_two, container, false);
TextView textViewNaviConTwo = (TextView) rootView.findViewById(R.id.textViewNaviConTwo);
textViewNaviConTwo.setText("HALLLOOOTJES :)");
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
With the Text "HALLLOOOTJES :)". And only in that fragment. But the other way around happens. This text is show on all the tabs except for tab number 1.
I'm certainly missing something.
Screenshots:
It should say "HALLLOOOTJES :)"
In your NaviContentFragmentTwo class, you are returning a NaviContentFragment from your newInstance method.
My guess is you should be returning a NaviContentFragmentTwo instance.
So it should be something like..
public class NaviContentFragmentTwo extends Fragment {
private static final String ARG_POSITION = "position";
public static NaviContentFragmentTwo newInstance(int position) {
NaviContentFragmentTwo fragment = new NaviContentFragmentTwo ();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);
return fragment;
}
This code could be the possible cause:
public static NaviContentFragment newInstance(int position) {
NaviContentFragment fragment = new NaviContentFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);
return fragment;
}
Try replacing NaviContentFragment to NaviContentFragmentTwo.
If I ave an activity with a viewPAger on it and two sets of data that I want to display, is it best to have one fragment and load information into textViews based on position passed from activity or better to create more than one fragment and load the one needed?
Any help is greatly appreciated.
public class testFragment extends Fragment {
private static final String ARG_PAGE = "pageNumber";
private static final int DEFAULT_INT = 0;
TextView header, sd1;
int h1, page = 0;
public testFragment (){
}
public testFragment newInstance(int pageNumber) {
testFragment fragment = new testFragment ();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page = getArguments().getInt(ARG_PAGE);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_high_scores_page, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
header = (TextView) getActivity().findViewById(R.id.modes_header);
sd1 = (TextView) getActivity().findViewById(R.id.stat_det_1);
switch (page) {
case 1:
header.setText(R.string.easy_mode);
h1 = DEFAULT_INT;
sh1 = DEFAULT_INT;
sd1.setText(String.valueOf(h1));
break;
case 2:
header.setText(R.string.medium_mode);
h1 = DEFAULT_INT + 1;
sh1 = DEFAULT_INT + 1;
sd1.setText(String.valueOf(h1));
break;
default:
header.setText(R.string.action_restart);
h1 = DEFAULT_INT;
sh1 = DEFAULT_INT;
sd1.setText(String.valueOf(h1));
break;
}
}
public void setTextViewInfo(){
sd1.setText(String.valueOf(h1));
}
}
Or would it be better to send the information from my activity and load one or multiple fragments instead?
public class testActivity extends FragmentActivity {
private static final int NUM_PAGES = 8;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testActivity);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.high_scores_pager);
mPagerAdapter = new testActivity (getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class testActivityAdapter extends FragmentStatePagerAdapter {
public testActivityAdapter (FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
testFragment tf1 = new testFragment ();
notifyDataSetChanged();
return easy.newInstance(position);
case 1:
testFragment2 tf2 = new testFragment ();
notifyDataSetChanged();
return medium.newInstance(position);
default:
testFragment d = new testFragment ();
return d.newInstance();
}
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
edit:
In my adapter, I just tried
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return HighScoresEasyFragment.newInstance(position);
case 1:
return HighScoresMediumFragment.newInstance(position);
case 2:
return HighScoresExpertFragment.newInstance(position);
case 3:
return HighScoresBrainFartFragment.newInstance(position);
case 4:
return HighScoresClassicFragment.newInstance(position);
case 5:
return HighScoresPuzzleFragment.newInstance(position);
case 6:
return HighScoresPerfectFragment.newInstance(position);
case 7:
return HighScoresPowerFragment.newInstance(position);
default:
return HighScoresEasyFragment.newInstance(position);
}
ugh, but it didn't load anything... my constructor is now...:
public static HighScoresEasyFragment newInstance(int pageNumber) {
HighScoresEasyFragment fragment = new HighScoresEasyFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
It loaded the first page's header but no data and it didn't load anything for second to 8th fragments...
You should call notifyDataChanged() from your activity using your adapter reference. Basically notifyDataChanged just tells all your fragments in your adapter that data has changed and they should refresh themselves. Remove notifyDataChanged from your getItem() method and put it when you change data in your activity.
i used this turtorial: http://www.paulusworld.com/technical/android-navigationdrawer-sliding-tabs#comment-2793
to get a swipe view alongside my navigation drawer, but i can't quite figure out how to change the view
in the onCreateView i'm trying to do it with a switch statement but it says "method is not overwriting method from it's superclass
code for fragment:
public class StepFragment extends Fragment {
public static final String TAG = StepFragment.class.getSimpleName();
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public static StepFragment newInstance() {
return new StepFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.step_fragment, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getChildFragmentManager());
mViewPager = (ViewPager) v.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return v;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new TabbedContentFragment();
Bundle args = new Bundle();
args.putInt(TabbedContentFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.step_title1).toUpperCase(l);
case 1:
return getString(R.string.step_title2).toUpperCase(l);
case 2:
return getString(R.string.step_title3).toUpperCase(l);
}
return null;
}
}
public static class TabbedContentFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public TabbedContentFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState,int position) {
View view1 = inflater.inflate(R.layout.fragment_step,
container, false);
View view2 = inflater.inflate(R.layout.fragment_step1,
container, false);
switch (position) {
case 0:
return view1;
case 1:
return view2;
}
return null;
}
}
}
Your onCreateView is passing four arguments, the superclass method only takes 3. You need to remove the position arg.
Use the args Bundle that you're setting in getItem() to pass the position. In onCreateView(), get those args and pull the position from that Bundle.
EDIT:
Here's your getItem():
#Override
public Fragment getItem(int position) {
Fragment fragment = new TabbedContentFragment();
Bundle args = new Bundle();
args.putInt(TabbedContentFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
Since your switch statement in onCreateView is looking for a 0 or 1, go ahead and remove the "+ 1" from args.putInt():
#Override
public Fragment getItem(int position) {
Fragment fragment = new TabbedContentFragment();
Bundle args = new Bundle();
args.putInt(TabbedContentFragment.ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
return fragment;
}
Now, in your fragment, you need to get the args and get the position from that:
public static class TabbedContentFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public TabbedContentFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
int position = args.getInt(ARG_SECTION_NUMBER, 0);
View view1 = inflater.inflate(R.layout.fragment_step,
container, false);
View view2 = inflater.inflate(R.layout.fragment_step1,
container, false);
switch (position) {
case 0:
return view1;
case 1:
return view2;
}
return null;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
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;
}
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return inflater.inflate(R.layout.fragment_content1, container, false);
}
}
}
The code above is the template from the SDK for ViewPager, it only put text 1 to 3 when I swipe from one page to one page. The question is how can I replace that text and put my own layout xml when I swipe it, and is it possible to use WebView with this feature. For example: when the activity loads it goes to google.com then when i swipe to other page it change the url of the WebView each time I swipe? Hope someone can help me...
You have to create classes that extend from android.support.v4.app.Fragment just like the class DummySectionFragment. Then, into the method getItem(int i) you should instantiate each class you want for each index and the method getCount() must return the number of Fragments you are instantiating.
viewpager.setCurrentItem(index) where index is the desired fragment index.