I'm searching solution of my problem. I have to make app using 3 fragments - just like on the picture. Picture of layout
I'm using ViewPager to have swipeable tabs which are required. But there is a problem - i can't set two fragments into one page of ViewPager.
public class ProgrammingLanguageInfo extends FragmentActivity{
private String languageName;
private int languageImageID;
private String languageDescription;
private String SHARED_PREFERENCES;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
languageName = getIntent().getStringExtra("LANGUAGE_NAME");
languageImageID = getIntent().getIntExtra("LANGUAGE_IMAGE", 0);
languageDescription = getIntent().getStringExtra("LANGUAGE_DESCRIPTION");
SHARED_PREFERENCES = getIntent().getStringExtra("LANGUAGE_SHARED_PREFERENCES");
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
if(pos==0){
return FirstFragment.newInstance(languageName, languageImageID, languageDescription, SHARED_PREFERENCES);
}
else{
return SecondFragment.newInstance("aaa");
}
}
#Override
public int getCount() {
return 2;
}
}
The problem comes in function getItem(int pos). On page number 0 everything is ok - there is one fragment to display. But how to display two fragments on next page of ViewPager? I can't return two Fragments from this function.
Check this tutorial. Hoe this will help!
https://tausiq.wordpress.com/2014/06/06/android-multiple-fragments-stack-in-each-viewpager-tab/
as a sollution you need to make a fragment that contains the 2 fragments as a child fragment instead of getSupportedFragment call
getChildFragmentManager()
and the other parts of creating and showing a fragment
make a xml that should use two containers for the fragments to use
and finaly return the above fragment as 2secon frag
Related
How to add a listview inside a viewpager (without using fragments)? Could you please help me? Thanks in advance.
It will be easy if you use Fragment in ViewPager, BUT if you dont want to use ViewPager with Fragment than ViewPager Without Fragments is example where you add Views and layouts to ViewPager and is more complex.
A good example is an image gallery, where the user can swipe between different pictures. On these types of pages, all you really want to display is a view of static content (in this case, an image), how to utilize the ViewPager with just plain-old Views and layouts.
It will be best to use fragment for each pages of viewPager. In this way you will be able to maintain individual pages easily.
In your fragment class(a java class which extends Fragment) you can work with your listview like initializing,setting adapter to listview,setting onClickListner for listview items same as you do in Activity
You can't use ViewPager to swipe between Activities. You need to convert each of you Activities into Fragments, then combine everything in one FragmentActivity with the Adapter you use with ViewPager.
This is not how ViewPager works. You feed the pages to ViewPager with a PagerAdapter. Your ListView will be contained within a Fragment created by the PagerAdapter.
In the layout:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/home_pannels_pager" />
In the FragmentActivity with this layout:
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
Example of simle PagerAdapter:
public class PagerAdapter extends FragmentPagerAdapter {
public FrontPageAdapter(FragmentManager Fm) {
super(Fm);
}
#Override
public Fragment getItem(int position) {
Bundle arguments = new Bundle();
arguments.putInt("position", position);
FragmentPage fragment = new FragmentPage();
fragment.setArguments(arguments);
return fragment;
}
#Override
public int getCount() {
// return count of pages
return 3;
}
}
Example of FragmentPage:
public class FragmentPage extends Fragment {
public FragmentPage() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_frontpage, container, false);
// probably cast to ViewGroup and find your ListView
Bundle arguments = getArguments();
int position = b.getInt("position");
return view;
}
}
Answered here - https://stackoverflow.com/a/28798795/7764015
So I have a section_home layout that shows a picture. Then I made another layout called section_home_two that shows a different picture. In my code I have a class called HomeFragment for the section_home layout. The second java class is called PreviewsFragment for the section_home_two layout.
Is there anyway you can make HomeFragment swipe horizontally to PreviewsFragment? Is there anyway to merge them?
I would suggest that you use a ViewPager to swipe between the fragments.
Follow the instructions at http://developer.android.com/training/animation/screen-slide.html to get the ViewPager working, and then change the number of pages to 2:
private static final int NUM_PAGES = 2;
And use this PagerAdapter instead of the one in the sample:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0)
return new HomeFragment(); // The ViewPager will show this fragment first
else
return new PreviewsFragment; // and will allow you to swipe to this one and back.
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
This will allow you to swipe horizontally back and forth between the two fragments. if you're using a factory method to create your fragments, use that instead of the return new HomeFragment(); and return new PreviewsFragment(); lines.
I'm very new to Android development and I have been trying make an application that allows users to flip either to the left or right to change fragments. I'm using a FragmentStatePagerAdapter for this.
I first, in a for loop, created each fragment separately in the main activity's OnCreate() method and added them to the FragmentManager fManager.
for(Day day: days){
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
this.fManager.beginTransaction().add(R.id.pager, fragment).commit();
}
fManager.executePendingTransactions();
I then set up UI elements and set the ViewPager's adapter like so
this.setUpUI();
mPager.setAdapter(mPagerAdapter);
The problem is is that I keep encountering this exception java.lang.IllegalStateException: Fragment Already Added...
I am very confused as to what is going on because the only place I add fragments to the FragmentManager is in the for-loop above. Do FragmentStatePagerAdapter's do something in the background that might cause this issue or am I not implementing my ScreenSlidePagerAdapter correctly?
Here is my code for the ScreenSlidePagerAdapter class:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public FragmentManager fm;
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
}
#Override
public Fragment getItem(int position) {
Fragment f = fm.getFragments().get(position);
return f;
}
#Override
public int getCount() {
return fm.getFragments().size();
}
}
Input would be greatly appreciated.
You show create the fragments inside getItem(pos) method of Adapter. The adapter internally use the FragmentManager that you provide in constructor.
Some what like this:
#Override
public Fragment getItem(int position) {
Fragment f = new ScreenSlidePageFragment();
return f;
}
I think the FragmentStatePagerAdapter already adds the fragments to your Activity (in the getItem() method). Try to remove that for loop.
You are getting the Fragment that is already in the FragmentManager and when you return it, the PagerAdapter tries to add it, but it is already there.
You don't have to add the fragments to the FragmentManager yourself. That's the FragmentStatePagerAdapter's job, that's why it receives a FragmentManager reference. You only have to create and return the fragments in getItem() and the PagerAdapter will add the fragments to the FragmentManager.
I usually do what you are trying like this:
public class FragmentSliderAdapter extends FragmentStatePagerAdapter {
private List<Fragment> list = new ArrayList<Fragment>();
public FragmentSliderAdapter(FragmentManager fm) {
super(fm);
list.add(new LoginFragment());
list.add(new SignupFragment());
}
#Override
public Fragment getItem(int arg0) {
return list.get(arg0);
}
#Override
public int getCount() {
return list.size();
}
}
Using list is cleaner and a good approach, since you can control what fragments are added to your adapter.
On the activity that uses the adapter, I just do this:
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new FragmentSliderAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
And the activity xml I do:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
See if it helps !
I'm using android.support.v4.view.ViewPager and noticed, that after setting my android.support.v4.app.FragmentStatePagerAdapter on the ViewPager in the onStart()-method of my activity I can't retrieve a view of the currently displayed ViewPager page, because it doesn't seem to be initialized/attached/drawn to the activity yet. I always get a NPE.
Is there an event which is fired, when my ViewPager is fully initialized including its fragment pages and their views?
My code in the onStart()-method of the main activity looks something like this:
ViewPager vp = findViewById(R.id.viewPager);
vp.setAdapter(new FlashCardPagerAdapter(getSupportFragmentManager()));
vp.getChildCount(); //this is still 0 because the child fragments have not been added yet, why? When are they added?
My Adapter looks like this:
public class FlashCardPagerAdapter extends FragmentStatePagerAdapter {
private List<FlashCard> flashCards;
private FlashCardFragment[] flashCardFragments;
public FlashCardPagerAdapter(FragmentManager fm) {
super(fm);
flashCards = FlashCardApp.getInstance().getFlashCards();
flashCardFragments = new FlashCardFragment[flashCards.size()];
}
#Override
public Fragment getItem(int i) {
if (flashCardFragments[i] != null) {
return flashCardFragments[i];
} else {
flashCardFragments[i] = new FlashCardFragment(flashCards.get(i));
return flashCardFragments[i];
}
}
public FlashCardView getFlashCardViewAt(int i) {
return ((FlashCardFragment) getItem(i)).getFlashCardView();
}
#Override
public int getCount() {
return 25;
}
}
Thanks for any help.
EDIT
Thing is, all this works when I call it after the onStart() method, for example in one of my custom button views.
I'm guessing vp.setAdapter(new MyAdapter()); is just a typo and you meant to say vp.setAdapter(new FlashCardPagerAdapter(getSupportFragmentManager()));
If that doesn't work, then it's returning 0 pages for some reason. I would check the adapter's getCount() method to make sure it's returning something more than 0, because that's how the viewpager decides how many pages to show.
Need some help with my problem of updating pages while using viewpager. I am using a simple viewpager with FragmentStatePagerAdapter. All I want to do is get access to the current fragment/view so as I can update some textviews in my fragment/view. I searched around in the forum and came to know few things
- One of the ways to handle this is by setting tag in instantiateItem() call back of the adapter and retreive the view by findViewbyTag. I could not understand how to implement this and I am also not sure if that will work for FragmentStatePagerAdapter.
- I explored other options as suggested in various forums, but cannot make them work.
My code is very much same as in a android http://developer.android.com/training/animation/screen-slide.html. Basic components are the same (Fragment activity xml with some display components including a textview, viewpager xml with just a view pager in it, a Fragment class and the main FragmentActivity class). in my FragmentActivity class I have added a pageChangelistener to my viewpager so as I can do my textview changes during onPageSelected().
Any help is is appreciated.
Adding the code for reference.
Public class myActivity extends FragmentActivity
//Variable declarations
protected void onCreate(Bundle savedInstanceState) {
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
View CurrView;
OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
#Override
public void onPageSelected(int pageSelected) {
doTextViewChnges();//access the text view and update it based on pageSelected
---THIS IS WHERE I AM STUCK IN TRYING TO GET THE TEXTVIEW IN MY CURRENT FRAGMWNT/VIEW-------
}
mPager.setOnPageChangeListener(pageChangelistener);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position, <other parameters I want to pass>);
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
in your OnPageChangeListener:
OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
#Override
public void onPageSelected(int pageSelected) {
ScreenSlidePageFragment currentFragment = (ScreenSlidePageFragment) mPagerAdapter.getItem(pageSelected)
doTextViewChnges();//access the text view and update it based on pageSelected
---THIS IS WHERE I AM STUCK IN TRYING TO GET THE TEXTVIEW IN MY CURRENT FRAGMWNT/VIEW-------
}
i assume you want to access your fragments from your activity and update the views.Fragments are parts of activities and their views can be accessed by them. Fragments are created dynamically in viewadapters and there is no straight forward and easy way to Tag them. You may simply access a fragment by its index number.first one is 0 second one is 1 and so on...
//access your fragment(in your case your first fragment)
//this part should be inside your doTextViewChnges()
Fragment fragment = (Fragment ) adapterViewPager
.getFragment(0);
if (fragment != null) {
//call a public method inside your fragment to update your text view
fragment .updateYourTextView();
}
Edit: inside your fragment create the following method and update your textview from there.
void updateYourTextView() {
yourTextView.setText("yourtext");
}