how to use a ViewPager in a fragment
-
I download a project about ViewPager and CirclePageIndicator. It works well on my tablet and its code is : Test1Activity.java: TestFragmentAdapter mAdapter; ViewPager mPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //The look of this sample is set via a style in the manifest setContentView(R.layout.simple_circles); mAdapter = new TestFragmentAdapter(getSupportFragmentManager()); mPager = (ViewPager)findViewById(R.id.pager); mPager.setAdapter(mAdapter); CirclePageIndicator indicator = (CirclePageIndicator)findViewById(R.id.indicator); indicator.setViewPager(mPager); } TestFragmentAdapter.java: class TestFragmentAdapter extends FragmentPagerAdapter { protected static final String[] CONTENT = new String[] { "Page1", "Page2", "Page3", "Page4", }; private int mCount = CONTENT.length; public TestFragmentAdapter(FragmentManager fragmentManager) { super(fragmentManager); } @Override public Fragment getItem(int position) { return TestFragment.newInstance(CONTENT[position % CONTENT.length]); } @Override public int getCount() { return mCount; } public void setCount(int count) { if (count > 0 && count <= 10) { mCount = count; notifyDataSetChanged(); } }} TestFragment: public final class TestFragment extends Fragment { private static final String KEY_CONTENT = "TestFragment:Content"; public static TestFragment newInstance(String content) { TestFragment fragment = new TestFragment(); fragment.mContent = content; return fragment; } private String mContent = "???"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) { mContent = savedInstanceState.getString(KEY_CONTENT); } TextView text = new TextView(getActivity()); text.setGravity(Gravity.CENTER); text.setText(mContent); text.setTextSize(20 * getResources().getDisplayMetrics().density); text.setPadding(20, 20, 20, 20); LinearLayout layout = new LinearLayout(getActivity()); layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); layout.setGravity(Gravity.CENTER); layout.addView(text); return layout; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString(KEY_CONTENT, mContent); }} now I want to use this ViewPager in a Fragment instead of Activity, but I am puzzled. The constructor of TestFragmentAdapter accept a parameter with type android.support.v4.app.FragmentManager, but in a fragment I can only use getFragmentManager() to get an android.app.FragmentManager type, thus the mAdapter = new TestFragmentAdapter(getSupportFragmentManager()); is always wrong. I must finish my project within days, so can u tell me how to modify my code to achieve same function in a fragment? Thx!!
-
Answer:
Have you try to replace extends Fragment by extends android.support.v4.app.Fragment ? When you do this, getFragmentManager() refers to android.support.v4.app.FragmentManager.
Eiffel Zhu at Stack Overflow Visit the source
Other answers
Android does not support fragments inside of fragments. Either switch your ViewPager to use a PagerAdapter that does not use fragments, or do not put the ViewPager in a fragment.
CommonsWare
Related Q & A:
- How to open a fragment from a notification?Best solution by Stack Overflow
- How to replace a fragment with another fragment in Android?Best solution by Stack Overflow
- How to add a button in a Fragment?Best solution by Stack Overflow
- How to refresh a listview within a Fragment?Best solution by Stack Overflow
- How to use a camcorder as a webcam?Best solution by Ask Ubuntu
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.