When creating a template project with holo fragments and tabs and sectionsPagerAdapter you get something like this
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;
}
The definition of DummySectionFragment:
/**
* 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 textView;
}
}
Now I replace this code with my own extension of Fragment:
#Override
public Fragment getItem(int i) {
Fragment fragment = new TargetsFragment();
Bundle args = new Bundle();
I defined this in its own file:
public class TargetsFragment extends Fragment {
boolean mDualPane;
int mCurCheckPosition = 0;
private CheckedExpandableListAdapter expandableListAdapter;
private ExpandableListView expandableListView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//TODO: serve with correct data instead of this!
final ArrayList<ListNode> dummyList = buildDummyData();
loadHosts(dummyList);
}
[...]
The error I get is that it is not possible to convert TargetsFragment to Fragment in getItem. I feel i'm missing something simple here. But what? If you need more code please ask.
Solution: take a close look at the imports. Make sure you import and extend the same Fragment in both classes. eg android.app.fragment or import android.support.v4.app.Fragment;
Related
I'm using swipeable tabs as part of an activity with a total of 3 sections. I'm providing the right and left arrows to navigate to the other sections of the swipe-view.
I want my left arrow to disappear when I'm at the leftmost section and my right-arrow to disappear when I'm at the rightmost section.
Here's what I'm trying rightnow, but not getting the desired result:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
static Button rightButton;
static Button leftButton;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rightButton = (Button)findViewById(R.id.buttonRight);
leftButton = (Button)findViewById(R.id.buttonLeft);
// 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);
}
/**
* 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;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
// 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;
}
public PlaceholderFragment() {
}
#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);
int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
String text = instructions(sectionNumber);
textView.setText(text);
return rootView;
}
private String instructions(int sectionNumber) {
if (sectionNumber == 1) {
leftButton.setVisibility(View.INVISIBLE);
rightButton.setVisibility(View.VISIBLE);
Log.i("Welcome"," to section 1");
return "All questions are mandatory. Each question carries 1 mark. There is no negative marking";
}
else if(sectionNumber == 2) {
leftButton.setVisibility(View.VISIBLE);
rightButton.setVisibility(View.VISIBLE);
Log.i("Welcome", " to section 2");
return "Color the bubble besides the option you think is best for the answer.";
}
else {
leftButton.setVisibility(View.VISIBLE);
rightButton.setVisibility(View.INVISIBLE);
Log.i("Welcome", " to section 3");
return "Click on the skip button above to start the test. Timer will start as soon as you'll click that button!";
}
}
}
}
As you can see in the method instructions, I tried using logs to see what's happening only to find that I never get the "Welcome to section 2" part even after swiping through the tabs multiple times. Although, the texts related to a given section are returned correctly. What am I missing here?
The problem is that you returned a new fragment in getItem
Do it like this:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<Fragment>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
mFragments.add(PlaceholderFragment.newInstance(1));
mFragments.add(PlaceholderFragment.newInstance(2));
mFragments.add(PlaceholderFragment.newInstance(3));
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
Also change the Buttons to the fragment_main layout "not" in activity_main and use it like this.
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private Button rightButton;
private Button leftButton;
// 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;
}
public PlaceholderFragment() {
}
#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);
rightButton = (Button)rootView.findViewById(R.id.buttonRight);
leftButton = (Button)rootView.findViewById(R.id.buttonLeft);
int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
String text = instructions(sectionNumber);
textView.setText(text);
return rootView;
}
EDIT if you want to know the cuurent page you have to implement this.
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.i("Welcome", "page "+ position+1);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Also add the following line so that the system will create your fragment only once:
mViewPager.setOffscreenPageLimit(3);
The issue you had is that you're getting the position that the system created now not the current page.
I can't seem to find a way to give different text to a dynamically created fragment for my pageviewer-supoorted application. I've came to a point of my codding where I got stuck. For my application I want to have 400-500 dynamically created fragments, where you can horizontally slide thru them and every content of the fragment to be the same (repeating the same fragment) and the only different thing to be the text on them.
Here's where I got stuck at my codding :
package com.example.testarearg;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity 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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
}
});
}
/**
* 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 DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/ private String mText; // display this text in your fragment
public static Fragment getInstance(String text) {
Fragment f = new Fragment();
Bundle args = new Bundle();
args.putString("text", text);
f.setArguments(args);
return f;
}
public void onCreate(Bundle state) {
super.onCreate(state);
setmText(getArguments().getString("text"));
// rest of your code
}
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
public String getmText() {
return mText;
}
public void setmText(String mText) {
this.mText = mText;
}
}
}
You need to create your own custom Fragment:
import android.support.v4.app.Fragment;
public class YourFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, container, false);
TextView tv = (TextView) v.findViewById(R.id.yourtextview);
tv.setText(getArguments().getString("text"));
return v;
}
public static YourFragment newInstance(String text) {
YourFragment f = new YourFragment();
Bundle b = new Bundle();
b.putString("text", text);
f.setArguments(b);
return f;
}
}
And then modify your adapter. The basic idea is that the adapter
contains the Strings that you want to display and the getItem(...)
method will choose the text.
public class MyPagerAdapter extends FragmentPagerAdapter {
private String [] strings;
public MyPagerAdapter(FragmentManager fm, String [] stringstodisplay) {
super(fm);
this.strings = stringstodisplay;
}
#Override
public Fragment getItem(int pos) {
return YourFragment.newInstance(strings[pos]);
}
#Override
public int getCount() {
return 500; // or strings.length to be save
}
}
From your Activity, you hand over the String array containing the different strings to the adapter.
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the content that your fragments will display
String [] strings = new String[500];
for(int i = 0; i < 500; i++) {
strings[i] = "This is Fragment " + i;
}
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), strings));
}
}
My app has a Gridview. Clicking on the Gridview cell used to open an activity which was themed as a dialog in the manifest. This worked well.
To improvise the app further, I wanted to add a viewpager in the activity. So I changed SomeClass extends Activity to SomeClass extends Fragment. I created a container (which extends FragmentActivity) to hold this Fragment and I themed this container as a Dialog in the manifest:
<activity android:name="com.packagename.SomeContainer" android:theme="#style/Theme.Sherlock.Light.Dialog" />
This doesn't show the Fragment as a dialog. It shows the fragment full screen which I don't want and it doesn't resize based on the content, which it did when it was an Activity.
How should I display the container (fragmentactivity) or the fragment as a dialog which should resize according to the content and not display full-screen?
SomeContainer.java
public class SomeContainer extends SherlockFragmentActivity {
private SomeAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
overridePendingTransition(R.anim.pull_in_from_left, R.anim.hold);
setContentView(R.layout.layout_events);
getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Intent intent = getIntent();
String date_string = intent.getStringExtra("date_string");
mAdapter = new SomeAdapter(getSupportFragmentManager(), date_string);
mPager = (ViewPager) findViewById(R.id.eventspager);
mPager.setAdapter(mAdapter);
}
#Override
protected void onPause() {
overridePendingTransition(R.anim.hold, R.anim.pull_out_to_left);
super.onPause();
}
public static class SomeAdapter extends FragmentPagerAdapter {
String date;
FragmentManager fm;
public SomeAdapter(FragmentManager fm, String date) {
super(fm);
this.date = date;
this.fm = fm;
}
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return SomeFragment.newInstance(date);
case 1:
return SomeFragment1.newInstance(date);
case 2:
return SomeFragment2.newInstance(date);
default:
return null;
}
}
}
}
SomeFragment.java
public class SomeFragment extends DialogFragment {
String date_string;
public static SomeFragment newInstance(String date) {
SomeFragment someFragment = new SomeFragment();
Bundle args = new Bundle();
args.putString("date", date);
someFragment.setArguments(args);
returnsomeFragment;
}
/** Called when the activity is first created. */
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.events, container, false);
//getActivity().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
date_string = getArguments().getString("date");
//do something
return v;
}
}
I am trying to implement Swipe view using fragment.
But its very important for me to extend simple activity rather than fragment activity.
I am getting error on getSupportFragmentManager() in the code written below:
public class CollectionDemoActivity extends Activity {
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection_demo);
mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
}
public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
public DemoCollectionPagerAdapter(FragmentManager fm) {
super(fm);
//int x=10;
}
#Override
public Fragment getItem(int i) {
Fragment fragment = new DemoObjectFragment();
Bundle args = new Bundle();
args.putInt(DemoObjectFragment.ARG_OBJECT, i + 10);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// For this contrived example, we have a 100-object collection.
return 100;
}
#Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
public static class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
Integer.toString(args.getInt(ARG_OBJECT)));
return rootView;
}
}
}
The error is pretty self-explanatory. getSupportFragmentManager() is only defined for the FragmentActivity class. No matter how much you want or need to call that method, it simply doesn't exist for plain Activities.
See also: Difference between Activity and FragmentActivity and Difference between Fragment and FragmentActivity
I have question list in my database. Questions appears on my page one by one with prev/next buttons.
I want to add viewpager for swiping between questions. But getting content to FragmentAdapter confused me.
FragmentAdapter:
class TestFragmentAdapter extends FragmentPagerAdapter {
protected static final String[] CONTENT = new String[] { "1", "2", "3", };
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return TestFragment.newInstance(CONTENT[position]);
}
#Override
public int getCount() {
return CONTENT.length;
}
}
As you get content from database, you dont need to use string content;
You need to use fragments. Create fragment with a textview then populate it for every next position. Then update your content with position
public class TestFragment extends Fragment {
public static TestFragment newInstance(int index ) {
TestFragment TestFragment = new TestFragment();
Bundle bundle = new Bundle();
bundle.putInt("index", index);
TestFragment.setArguments(bundle);
return TestFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.yourlayout, container, false);
text = (TextView) view.findViewById(R.id.yourtext);
text.setText("your position: " + getArguments().getInt("index")) // where you get index from your newinstance
}
}