So far I have only implemented ViewPager with one type of fragment.
Now I want to add navigation tabs and be able to slide sideways from fragment of type A to fragment of type B . Do I need to contain both types of fragments in one activity ? If so , does it matter which fragment will have the view pager?
thank you.
Neither of the fragments contain the ViewPager. That would be contained in this case in your activity. The navigation tabs sit above the Viewager. Look at this tutorial: https://github.com/codepath/android_guides/wiki/Sliding-Tabs-with-PagerSlidingTabStrip . You can specify what fragments go in the ViewPager in the getItem method of a FragmentPagerAdater.
#Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0:
fragment = fragment1.newInstance();
break;
case 1:
fragment = fragment2.newInstance();
break;
case 2:
fragment = fragment3.newInstance();
break; }
return fragment;
}
Related
In this image I have a list.
What I want is to change the view of this Fragment to the image given below.
On the header, I want to add a search button. By clicking on the button the second image layout will appear and and by clicking the button again, it will fire a web service that returns with the response.
First of all add the search button.Initialize it in your main activity.In your project I suggest you to do with a single onclicklistner and a longclicklistner.For double tap you have to initialize a gesture.
I can provide you a code for any approaches but I strongly suggest you go with the first one.Reply mi accordingly I will provide you a code.
I think that what you are actually asking is "How to get a reference of the ViewPager's Fragment and do something with that".
If this is the case, you can have a look here, you see, its a very common and not that easy problem.
One way to solve ot correctly (providing that you are already using a FragmentStateAdapter) is doing something like the following:
In your activity:
private CustomFragment mHomeFragment;
private CustomFragment mOfferFragment;
private CustomFragment mFavoriteFragment;
private CustomFragment mInfoFragment;
and then, in your adapter
#Override
public Fragment getItem(int position) {
return CustomFragment.newInstance(position, null);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// save the appropriate reference depending on position
switch (position) {
case 0:
mHomeFragment = (CustomFragment) createdFragment;
break;
case 1:
mOfferFragment = (CustomFragment) createdFragment;
break;
case 2:
mFavoriteFragment = (CustomFragment) createdFragment;
break;
case 3:
mInfoFragment = (CustomFragment) createdFragment;
break;
}
return createdFragment;
}
Doing that, it will allow you to do:
mViewPager.setCurrentItem(1);
mOfferFragment.doSomething();
in your parrent Activity
I have a PagerAdapter which creates 3 fragments.
In the MainActivity I set the ViewPager like this:
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setOffscreenPageLimit(2);
pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
the pager.setOffScreenPageLimit(2) is from here https://stackoverflow.com/a/11852707/1662033, to make sure OnViewCreated is called once for each fragment.
Here is my PagerAdapter class:
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Home";
case 1:
return "Live";
case 2:
return "Gallery";
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new HomeFragment();
case 1:
return new LiveFragment();
case 2:
return new GalleryFragment();
default:
return null;
}
}
}
In the current code: all of the fragments's onCreateView, onActivityCreated etc are called once, at the beginning and that's it.
The issue I am having is - in one of the fragments (LiveFragment) I have a custom view which connects to a camera and shows the live stream.
What I want is - to inflate the view of LiveFragment only when the user navigates to the fragment, instead of how its now - its inflated at the beginning with the other fragments.
Is there a way to call onCreateView only when fragment is chosen?
FragmentPagerAdapter creates all the Fragments and has all of them in memory at all time. i.e. All your Fragments are created only once and you can navigate around them.
FragmentStatePagerAdapter creates and has only 3 Fragments (the current Fragment and the left and right Fragments to the current one) in memory at any given time, by default. You cannot reduce that number. However, you can increase the number Fragments in memory by using the viewpager.setOffScreenPageLimit().
Since you have only 3 Fragments, all your 3 fragments are created when the Viewpager is initialised. You can track which Fragment is currently visible on the screen using viewpager.addOnPageChangeListener(). Using this you can change the View of your LiveFragment from dummy one to actual View only when the Fragment is currently visible.
I am using swipe view with ViewPager to display tabs in project.
I am adding three tabs(fragments) to AlertTabsPagerAdapter using
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new FragmentA();
case 1:
// Games fragment activity
return new FragmentB();
case 2:
// Movies fragment activity
return new FragmentC();
}
return null;
}
I am having two problems:
By default OffscreenPageLimit of ViewPager is two, so in my case for first time FragmentA and FragmentB will get created. I want to reload or recreate FragmentB on swipe from FragmentA. How can I achieve this?
In the first tab (i.e. FragmentA) I am having multiple fragments like FragmentA through FragmentD. For this I am replacing FragmentA with FragmentD using FragmentTransaction.
After the fragment transaction onback key press from FragmentD i am getting back to FragmentA, but now I want to reload or recreate FragmentA.
So I'm trying to create a swipe tab view in my app. But the problem I'm having is that my fragments need to be FragmentActivity becuase they have listview inside them.
So I think this is where I'm getting my error:
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new TopRatedFragment();
case 1:
// Games fragment activity
return new GamesFragment();
case 2:
// Movies fragment activity
return new MoviesFragment();
case 3:
//Other fragment activity
return new OtherFragment();
}
return null;
}
This needs to be:
public FragmentActivity getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new TopRatedFragment();
case 1:
// Games fragment activity
return new GamesFragment();
case 2:
// Movies fragment activity
return new MoviesFragment();
case 3:
//Other fragment activity
return new OtherFragment();
}
return null;
}
But I keep getting errors on this line:
public FragmentActivity getItem(int index) {
Error:
The return type is incompatible with FragmentPagerAdapter.getItem(int)
Does anyone know what I'm doing wrong here?
My class is extending FragmentPagerAdapter
A Fragment can also have a ListView in it.
FragmentActivity is some support Class, to support Fragments on older devices, i think its pre api 11.
To use a ListView in a Fragment, just create a ListView in the Fragment, or inflate an xml file with a ListView in it.
FragmentPagerAdapter getItem() method needs to return a Fragment. FragmentActivity is not a Fragment, it's an Activity containing Fragments. See here: http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html
Use ListFragment for Fragments having a ListView
http://developer.android.com/reference/android/support/v4/app/ListFragment.html
Your fragment don't need to be FragmentActivity to hold a ListView, actually the Activity should extend FragmentActivity, and your fragment should extend ListFragment, which is:
a fragment that displays a list of items by binding to a data source
such as an array or Cursor, and exposes event handlers when the user
selects an item.
I made a post about it that explains how fragments work, how to implement a ListView inside a fragment and how to integrate Fragments with ViewPager. Here is the link, hope it helps.
I want to implement a navigation drawer with multiple listfragments, how can i do this? I spent time searching online but couldnt find anything related to it. Any help would be appreciated!
When implementing the NavigationDrawer, use its onDrawerItemSelected method to switch Fragments:
#Override
public void onDrawerItemSelected(final int pos) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new ListFragmentOne();
break;
case 1:
fragment = new ListFragmentTwo();
break;
case 2:
fragment = new ListFragmentThree();
break;
}
// content_frame is a FrameLayout inside the layout of your activity - this is where the fragment will be put
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(pos, true);
// do stuff like closing the drawer...
}
I am not sure though if the NavigationDrawer even supports ListFragments. If not, simply use normal Fragments containing a ListView.