Fragment Page Adapter - Only call Get Item once - android

I have tabs setup in my app. I'm using list view fragments that each do a call to pull down data.
Something like the following:
public Fragment getItem(int position) {
switch (position) {
case 0:
return FirstFragment.newInstance();
case 1:
return FirstFragment.newInstance();
case 2:
return SecondFragment.newInstance();
default:
return null;
}
}
It's working great, however I don't want the call to happen every time the tab is visited, only the first time.
What is the proper way to do this?

You can always instantiate those fragments somewhere in your constructor and then just pass a reference to it in getView(...).
Here's an illustration:
public class YourPagerAdapter extends FragmentPagerAdapter {
YourFragment fragment1, fragment2, fragment3;
public YourPagerAdapter() {
fragment1 = YourFragment.newInstance();
fragment2 = YourFragment.newInstance();
fragment3 = YourFragment.newInstance();
...
}
public Fragment getItem(int position) {
switch (position) {
case 0:
return fragment1;
case 1:
return fragment2;
case 2:
return fragment3;
default:
return null;
}
}
...
}
That way, your fragments would only got instantiated once - when you're creating your adapter.
Better yet, wrap those fragments in a List. That way you could simplify your getItem(int position) to just return mFragments.get(position).

Related

How do I add a Fragment tag to a newly created instance?

How would I add a tag to a newly created instance?
case 0:
return FragmentNotifications.newInstance(notifications, mCurrentTime);
The new instance:
public static FragmentNotifications
newInstance(String notificationsArray, long currentTime){
FragmentNotifications fragment = new FragmentNotifications();
return fragment;
}
How would I assign a name to the fragment created with viewpager?
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return
FragmentNotifications.newInstance(notifications, mCurrentTime);
case 1:
return FragmentMessages.newInstance(messages,
mCurrentTime);
case 2:
return new FragmentProfile();
default:
return null;
}
}
Answer & Credits to the person who found this method:
How to get existing fragments when using FragmentPagerAdapter
I got the tag name that is automatically generated by android api
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// get the tags set by FragmentPagerAdapter
switch (position) {
case 0:
String firstTag = createdFragment.getTag();
break;
case 1:
String secondTag = createdFragment.getTag();
break;
}
// ... save the tags somewhere so you can reference them later
return createdFragment;
}
Make sure you use the getChildFragmentManager to reference it otherwise it returns null
I spent like 2 hours trying to find out why findFragmentByTag was returning null

How I can have different controls in different tabs and one Fragment using TabLayout in Android

I am trying to understand how tabs are working but I am confused. I created a sample app with Android Studio wizard where there is one fragment.
How I can have in this one fragment but in different tabs different elements I only need to do something in onCreateView or in FragmentPagerAdapter?
The part of the adapter is:
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 "CONTACTS";
case 1:
return "DETAILS";
case 2:
return "GENERAL";
}
return null;
}
}
An example on how to achieve the task.
//sending different arguments from the view pager adapter
public Fragment getItem(int position){
Fragment fragment=new PlaceHolderFragment();
//a bundle is used to wrap the data you send to the fragment
Bundle bundle=new Bundle();
//change whats inside the bundle based on your page
switch(position){
case 0:{
bundle.putInt("dataForThisPosition",12);
break;
}case 1:{
bundle.putInt("dataForThisPosition",13);
break;
}
//this can continue
}
//attach the bundle to the fragment
fragment.setArguments(bundle);
return fragment;
}
//then in your fragment class
//receiving the data sent from pager adapter
public View onCreateView(/*parameters*/){
Bundle receivedBundle=getArguments();
int datForThisPosition=recievedBundle.getInt("dataForThisPosition",12/*default value*/);
//create your view based on the info received.
}

Android Listfragment relace page name to icon

My app have android listfragment, 3 pages with 3 title i want replace page title to a icon how can i?
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new firstFragment();
break;
case 1:
fragment = new secondFragment();
break;
case 2:
fragment = new thirdFragment();
break;
}
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.fragment_one).toUpperCase();
case 1:
return getString(R.string.fragment_two).toUpperCase();
case 2:
return getString(R.string.fragment_three).toUpperCase();
}
return null;
}
}
I want each page have a icon not a text title. And also how can i aligh each icon in center
You can make custom ActionBar and set some Layout. When your fragment will start then you can set you icon there, update style or something else...
You can provide the tabs in action bar and you can set the Drawables for each tab u want.

Android getItem in FragmentPagerAdapter unable to return FragmentActivity

I'm trying to return a FragmentActivity from getItem(int index) in a FragmentPagerAdapter. I'm using the FragmentActivity to implement a listview in a tab. Is there any work around for this?
#Override
public android.support.v4.app.Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new TopRatedFragment();
case 1:
// Games fragment activity
return null; //new GamesFragment();
case 2:
// Movies fragment activity
return null;//new MoviesFragment();
}
return null;
}
As per the getitem() mth documentation on developer, it is not apssoble to retuen FragmentActivity.
abstract Fragment getItem(int position)
Return the Fragment associated with a specified position.
FragmentPagerAdapter's getItem() method does not and cannot return a FragmentActivity. FragmentPagerAdapter's main purpose in life is to manage the various pages in ViewPager. To show a ListView in a ViewPager tab, you can extend FragmentPagerAdapter as follows:
public class MyCustomAdapter extends FragmentPagerAdapter {
private ArrayList<String> mPages;
public MyCustomAdapter(FragmentManager fm, ArrayList<String> viewPagerPageItems) {
super(fm);
mPages = viewPagerPageItems;
}
#Override
public int getCount() {
return mPages.size();
}
#Override
public Fragment getItem(int viewPagerTabPosition) {
switch (viewPagerTabPosition) {
case 0:
// this fragment should extend ListFragment, where you could call
// getListView() in the onCreate() or onActivityCreated() method.
// to inflate your list
return TopRatedListFragment.newInstance(mPages.get(position));
case 1:
case 2:
break;
}
return fragment;
}
}
The ListFragment structure and the newInstance() factory method follows the approach described here.

FragmentTransaction with FragmentPagerAdapter

Im using a FragmentPagerAdapter to manage 3 Fragments.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new Latest();
case 1:
return fragment = new MySets();
case 2:
return fragment = new MyPictures();
default:
break;
}
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
//SET TITLE OF TABS
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1_latest).toUpperCase(l);
case 1:
return getString(R.string.title_section2_mysets).toUpperCase(l);
case 2:
return getString(R.string.title_section3_allpictures).toUpperCase(l);
}
return null;
}
}
All Fragements extend Fragment. To handle Click Events and a Custom Layout Im using a Adapter which extends BaseAdapter.
My Goal is to replace Fragment 2 through another Fragment. Is it correct that I have to add all Fragments with FragmentTransaction to replace them later?
But how can i dynamically add the fragments to use FragmentTransaction? Is there a way to add them at the getItem(int position) or is that the wrong way to go?
Edit
If you need the Fragment as soon as the app is launched then the approach you used above in getItem() is okay, you don't need to do the transaction from there.
If you want to replace the Fragment later on simply use the getSupportFragmentManger and a transaction in the Fragment.
NewFragment newFrag = new NewFragment();
FragmentTransaction transaction = getActivity()
.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.your_fragment_container_id, newFrag);
transaction.addToBackStack(null);
transaction.commit();

Categories

Resources