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.
Related
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.
}
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).
i am Suffering In Little problem .My Second Tab Of Fragment Is Not Performing The Task
my first tab is working
i m using View Page Adapter for Tab
Here Is Tutorial same way of this tutorial i used
but my second tab showing nothing
please help me ..
i will appreciate your suggestion
thanks in adavance
here is my view page adapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 2;
private String titles[] = new String[] { "Friends Request", "FriendsList" };
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
MyFriendsList fragmenttab1 = new MyFriendsList();
return fragmenttab1;
// Open Friendlist.java
case 1:
FriendsList fragmenttab2 = new FriendsList();
return fragmenttab2;
}
return null;
}
public CharSequence getPageTitle(int position) {
return titles[position];
}
#Override
public int getCount() {
return PAGE_COUNT;
}
}
in this my first tab of myfriendlist is working but Why Second tab Is not Calling It show blank page in frienlist
please help me
i want to set different pages for ViewPager from class. for example:
i have 4 activity A + B + C and D.
A include my ViewPager.
B +C + D are different pages that must be shown in ViewPager.(each of them have different contex)
my question is that, how can i recognize these pages into ViewPager?
thanks
You can add but better way is that you will create a activity and your four activity should be child fragment of the same .
In short your all activity should be fragments jst load the fregment when you swipe viewpage indicater
sample code for adapter
//User adapter according condition
final FragmentPagerAdapter adapter = new FragmentChildPageAdapter(getChildFragmentManager());
//final FragmentPagerAdapter adapter = new FragmentChildPageAdapter(getActivity().getSupportFragmentManager()));
final ViewPager pager = (ViewPager) v.findViewById(R.id.pager);
pager.setAdapter(adapter);
final TabPageIndicator indicator = (TabPageIndicator) v.findViewById(R.id.indicator);
indicator.setViewPager(pager);
//Adapter
class FragmentChildPageAdapter extends FragmentPagerAdapter implements
IconPagerAdapter {
public FragmentChildPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
//first fregment
break;
case 1:
//Second fregment
break;
case 2:
//third fragment
break;
case 3:
//Forth fragment
break;
}
return fragment;
}
#SuppressLint("DefaultLocale")
/*#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}*/
#Override
public int getCount() {
//return CONTENT.length;
return CONTENT.size();
}
#Override
public int getIconResId(int position) {
return (Integer) Icon.get(position % CONTENT.size());
}
}
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();