Center Logo Between Two Tabs - android

Here the vector of what i'm trying to do
There Will be 2 tabs and one imageview or something between those two tabs, and the logo should be unclickable.
I've tried almost every approach but failed.
Anyone who knows how to do it?
Code :
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs2);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(1).setIcon(R.drawable.logo);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new LeaderBoard();
case 1:
return new Logo();
case 2:
return new Recent_Post();
default:
return null;
}
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "LeaderBoard";
case 1:
return "";
case 2:
return "Recent Posts";
}
return null;
}
}

I've Solved my problem by switching the tabs between each other, Some Calculations.
int currentScreen = 0;
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 1) {
if(currentScreen == 0) {
mViewPager.setCurrentItem(2);
currentScreen = 2;
}else{
mViewPager.setCurrentItem(0);
currentScreen = 0;
}
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Related

ViewPager does not scroll fragments

ViewPager does not scroll fragments:
I include ViewPager inside activity.
And create two fragments, but when i scroll view pager (horizintal) only 1rst Framgent selects:
_pageAdapter=new PagerAdapter(getSupportFragmentManager());
_viewPager= (ViewPager) findViewById(R.id.viewPager);
_viewPager.setAdapter(_pageAdapter);
_viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.d(LOG_TAG,"!Page #"+position);
}
}
#Override
public void onPageScrollStateChanged(int state) {
if(state==0)
Log.d(LOG_TAG,"Page Iddle");
else if(state==1)
Log.d(LOG_TAG,"Page Dragging");
else if(state==2)
Log.d(LOG_TAG,"Page Settling");
}
});
Here code of PageAdapter:
public class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position) {
case 0: {
fragment = new FirstFramgent();
Bundle bundle = new Bundle();
bundle.putInt("num", 0);
fragment.setArguments(bundle);
break;
}
case 1: {
fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putInt("num", 1);
fragment.setArguments(bundle);
break;
}
}
return fragment;
}
#Override
public int getCount() {
return 1;
}
}
Tell me, how to make ViewPager scrolls two fragments.
Try this change:
#Override
public int getCount() {
return 2;
}

Changing ImageView depends on Tab

I am having 2 Tab fragment in an activity.An activity has a circle imageview at the center. While I swipe 1st Fragment,I need to show 1st image & same as for 2nd tab swipe with different image.
UPDATE:
static class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle bundle = new Bundle();
bundle.putInt("position", position);
switch (position) {
case 0:
imgProfile.setImageResource(R.drawable.login_logo);
return new LogInFragment();
case 1:
imgProfile.setImageResource(R.drawable.add_image);
return new SignUpFragment();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "LOGIN";
case 1:
return "SIGNUP";
}
return null;
}
}
ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (i == 0) {
ImageView.setBackgroundResource(R.drawable.image1);
}
else if (i ==1) {
ImageView.setBackgroundResource(R.drawable.image2);
}
}
});
Ok now I understand what you want to do:
First delete all the code where you change the image and try this:
static class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle bundle = new Bundle();
switch (position) {
case 0:
yourImageView.setImageResource(R.id.yourResource1);
fragment = new LogInFragment();
break;
case 1:
yourImageView.setImageResource(R.id.yourResource2);
fragment = new SignUpFragment();
break;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
String str = "";
switch (position) {
case 0:
str = "LOGIN";
break;
case 1:
str = "SIGNUP";
break;
}
return str;
}
}
Thats all you need
Finally Got it by using the following code by made some modification in #sasikumar answer.
Instead of put onPageScrollStateChanged I used to get position in onPageSelected
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
int i = 0;
if (position == 0) {
Log.e("Tab Position", String.valueOf(position));
imgProfile.setImageResource(R.drawable.login_logo);
} else if (position == 1) {
Log.e("Tab Position", String.valueOf(position));
imgProfile.setImageResource(R.drawable.add_image);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Dynamically add and remove tabs in TabLayout(material design) android

I have a TabLayout and inside that I have ViewPager. I need to dynamically add and remove tab in tablayout(material design). I can able to add the tabs dynamically but while removing the tab, tab is removing correctly. But viewpager last item is deleted. So tab is not showing specific fragment. FYI i have used FragmentPagerAdapter.
I have followed this tutorial to implement this
https://androidbelieve.com/navigation-drawer-with-swipe-tabs-using-design-support-library
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2;
private MyNewsFragment mMyNewsFragment;
private ArrayList<TabFragmentModelClass> mFragmentArrayList;
private TabLayoutAdapter mTabLayoutAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mMyNewsFragment = new MyNewsFragment();
/**
*Inflate tab_layout and setup Views.i;;a
*/
View x = inflater.inflate(R.layout.tab_layout, null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); // scorllable tab
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); // tab name fill parent
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
mFragmentArrayList = new ArrayList<>();
mFragmentArrayList.add(new TabFragmentModelClass(new MyNewsSelectionFragment(), "", ""));
mFragmentArrayList.add(new TabFragmentModelClass(mMyNewsFragment, "", getResources().getString(R.string.mynews)));
mFragmentArrayList.add(new TabFragmentModelClass(new BreakingNewsFragment(), "", getResources().getString(R.string.breakingnews)));
// Selected news from shared preference
ArrayList<MyNewsSharedPreferenceModelClass> tempSharedPreferenceModelClass = new MyNewsSharedPreferenceClass().loadFavorites(getActivity());
for (int i = 0; i < tempSharedPreferenceModelClass.size(); i++) {
mFragmentArrayList.add(new TabFragmentModelClass(new CategoreyNewsFragment(tempSharedPreferenceModelClass.get(i).getmCatID()), tempSharedPreferenceModelClass.get(i).getmCatID(),
tempSharedPreferenceModelClass.get(i).getmCatName()));
}
}
/**
*Set an Apater for the View Pager
*/
mTabLayoutAdapter = new TabLayoutAdapter(getChildFragmentManager(), mFragmentArrayList);
viewPager.setAdapter(mTabLayoutAdapter);
viewPager.setOffscreenPageLimit(mFragmentArrayList.size());
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
tabLayout.getTabAt(position);
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == 1) {
updateMyNewsFragment();
}
}
});
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.getTabAt(0).setIcon(R.mipmap.mynewselection);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 1:
System.out.println("000000000000000000000 ");
updateMyNewsFragment();
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
});
return x;
}
/**
* update the mynews selection
* this method trigger from the MainNewsActivity
*/
public void updateMyNewsFragment() {
if (ApplicationController.getInstance().getBoolPref(CommonVariableInterFace.isNewsSelected)) {
mMyNewsFragment.sendMyNewsRequest();
ApplicationController.getInstance().setBoolPref(CommonVariableInterFace.isNewsSelected, false);
}
}
/**
* update the tab values
* this method trigger from the MainNewsActivity
* if value is zero need to add in tab
* if value is one need to remove in tab
*/
public void updateTabvalue(String catId, String catName, int value) {
if (value == 0) { // add the value in tab
mFragmentArrayList.add(new TabFragmentModelClass(new CategoreyNewsFragment(catId), catId,
catName));
} else { // Removing the tab
for (int i = 0; i < mFragmentArrayList.size(); i++) {
if (mFragmentArrayList.get(i).getmCatID().equalsIgnoreCase(catId)) {
Log.i("-----same id ", catId);
mFragmentArrayList.remove(i);
mTabLayoutAdapter = new TabLayoutAdapter(getChildFragmentManager(), mFragmentArrayList);
viewPager.setAdapter(mTabLayoutAdapter);
}
}
}
mTabLayoutAdapter.notifyDataSetChanged();
viewPager.setOffscreenPageLimit(mFragmentArrayList.size());
tabLayout.getTabAt(0).setIcon(R.mipmap.mynewselection);
tabLayout.setupWithViewPager(viewPager);
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.getTabAt(0).setIcon(R.mipmap.mynewselection);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 1:
updateMyNewsFragment();
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
});
}
public void openSpecificTab() {
// tabLayout.settab
// tabLayout.se
}
}
Adapter code:
public class TabLayoutAdapter extends FragmentPagerAdapter {
private long baseId = 0;
ArrayList<TabFragmentModelClass> fragmentAdapterArrayList;
public TabLayoutAdapter(FragmentManager fm, ArrayList<TabFragmentModelClass> fragmentArrayList) {
super(fm);
fragmentAdapterArrayList = fragmentArrayList;
}
#Override
public Fragment getItem(int position) {
if (position == 0)
return fragmentAdapterArrayList.get(position).getmFragment();
if (position == 1)
return fragmentAdapterArrayList.get(position).getmFragment();
if (position == 2)
return fragmentAdapterArrayList.get(position).getmFragment();
return new CategoreyNewsFragment().newInstance(fragmentAdapterArrayList.get(position).getmCatID());
}
#Override
public int getCount() {
return fragmentAdapterArrayList.size();
}
//this is called when notifyDataSetChanged() is called
#Override
public int getItemPosition(Object object) {
// refresh all fragments when data set changed
return TabLayoutAdapter.POSITION_NONE;
}
#Override
public long getItemId(int position) {
// give an ID different from position when position has been changed
return baseId + position;
}
/**
* Notify that the position of a fragment has been changed.
* Create a new ID for each position to force recreation of the fragment
*
* #param n number of items which have been changed
*/
public void notifyChangeInPosition(int n) {
// shift the ID returned by getItemId outside the range of all previous fragments
baseId += getCount() + n;
}
/**
* // * This method returns the title of the tab according to the position.
* //
*/
#Override
public CharSequence getPageTitle(int position) {
return fragmentAdapterArrayList.get(position).getmCatName();
}
}
Remove tab from TabLayout
...
public void removeTab(int position) {
if (mTabLayout.getTabCount() >= 1 && position<mTabLayout.getTabCount()) {
mTabLayout.removeTabAt(position);
mPagerAdapter.removeTabPage(position);
}
}
...
Add a removeTabPage method to your PagerAdapter
...
public void removeTabPage(int position) {
if (!tabItems.isEmpty() && position<tabItems.size()) {
tabItems.remove(position);
notifyDataSetChanged();
}
}
...
Add a Tab
...
private void addTab(String title) {
mTabLayout.addTab(mTabLayout.newTab().setText(title));
mPagerAdapter.addTabPage(title);
}
...
Add a addTabPage method to your PagerAdapter
...
public void addTabPage(String title) {
tabItems.add(title);
notifyDataSetChanged();
}
...
Check out this sample code for a full example: ...samples/SupportDesignDemos/src/com/example/android/support/design/widget/TabLayoutUsage.java
With the new support library (I'm using 23.2.1) you should only add to the Viewpager and not the TabLayout (otherwise you end up with double tabs and other funky behavior). So to update TouchBoarder's answer:
Add a removeTabPage method to your PagerAdapter
public void removeTabPage(int position) {
if (!tabItems.isEmpty() && position<tabItems.size()) {
tabItems.remove(position);
notifyDataSetChanged();
}
}
Add a addTabPage method to your PagerAdapter
public void addTabPage(String title) {
tabItems.add(title);
notifyDataSetChanged();
}
In addition to existing answers, to remove all tabs from tabLayout.
tabLayout.removeAllTabs();
You can add dynamic tabs like this :
public void addTabs(){
for(YourClassModel tabName: tabsName){
Log.i("ActivityClass","tabName"+tabName);
tabLayout.addTab(tabLayout.newTab().setText(tabName.getName()));
}
Removetab from tablayout
TabActivity and ViewPager code :
`public class ScrollableTabsActivity extends AppCompatActivity {
int NumberOfTab = 5;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
ViewPagerAdapter adapter ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollable_tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
int length = tabLayout.getTabCount();
for (int i = 0; i < length; i++) {
tabLayout.getTabAt(i).setCustomView(getTabView(i));
}
}
public View getTabView(final int position) {
View view = LayoutInflater.from(this).inflate(R.layout.close_tablayout, null);
TextView title = (TextView) view.findViewById(R.id.title);
ImageView icon = (ImageView) view.findViewById(R.id.close);
title.setText(adapter.getPageTitle(position));
icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NumberOfTab = NumberOfTab - 1;
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
if (tabLayout.getTabCount()==3)
tabLayout.setTabMode(TabLayout.MODE_FIXED);
int length = tabLayout.getTabCount();
for (int i = 0; i < length; i++) {
tabLayout.getTabAt(i).setCustomView(getTabView(i));
}
}
});
return view;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
final Fragment result;
switch (position) {
case 0:
// First Fragment of First Tab
result = new OneFragment();
break;
case 1:
// First Fragment of Second Tab
result = new TwoFragment();
break;
case 2:
// First Fragment of Second Tab
result = new ThreeFragment();
break;
case 3:
// First Fragment of Second Tab
result = new FourFragment();
break;
case 4:
// First Fragment of Second Tab
result = new FiveFragment();
break;
default:
result = null;
break;
}
// Log.d("RESULT",result.getTag());
return result;
}
#Override
public int getCount() {
return NumberOfTab;
}
#Override
public CharSequence getPageTitle(final int position) {
switch (position) {
case 0:
return "One";
case 1:
return "Two";
case 2:
return "Three";
case 3:
return "Four";
case 4:
return "Five";
default:
return null;
}
}
}
}`
Check out this Links :
https://stackoverflow.com/a/58480151/9223264
I am new in stackoverflow i try my best if you think my answer is useful then please appreciate

how not to show some tabs on tabbed activity?

I have the following activity wich show different tabs.
public class BagContent extends FragmentActivity implements ActionBar.TabListener, CosmeListener {
String[] profileTypes;
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_bag_content);
mViewPager = (ViewPager) findViewById(R.id.pager);
final ActionBar actionBar = getActionBar();
actionBar.setTitle(getString(R.string.title_activity_bag_content) + " [" + bagName + "]");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
}
#Override
protected void onResume() {
super.onResume();
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FormatShortFragment();
case 1:
return new FormatLongFragment();
case 2:
return new FormatPlantFragment();
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_bag_content_format_short).toUpperCase(l);
case 1:
return getString(R.string.title_bag_content_format_long).toUpperCase(l);
case 2:
return getString(R.string.title_bag_content_format_plant).toUpperCase(l);
}
return null;
}
}
I have and array wiht different profile types, depending on each type i have to show some tabs or others.
Actually all tabs are been showing for every profile type. I tried to insert manually the tabs on the actionbar, but then the tabs are not showing the fragments correctly. Other problem is that navigation breaks when i go to an inexistent tab on the right edge.
I need help with this, how can I select just 1 tab for a specific profile type?
for example, how can I show just the title_bag_content_format_long and his fragment for profileTypes[1]?
Any walk aroun can be good, for example remove some tabs from some profile types or any other thing.
Thanks a lot guys, I really need help with this.
EDITED
I have made some modifications:
Added a profile type parameter to SectionsPagerAdapter constructor:
private String profileType;
public SectionsPagerAdapter(FragmentManager fm, String profileType) {
super(fm);
this.profileType = profileType;
}
Then i have modified this other methods to control the profile type:
#Override
public Fragment getItem(int position) {
if (this.profileType.equals(profileTypes[1])){
switch (position) {
case 0:
return new FormatPlantFragment();
}
} else {
switch (position) {
case 0:
return new FormatShortFragment();
case 1:
return new FormatLongFragment();
case 2:
return new FormatPlantFragment();
}
}
return null;
}
#Override
public int getCount() {
if (this.profileType.equals(profileTypes[1])){
return 1;
}
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
if (this.profileType.equals(profileTypes[1])){
switch (position) {
case 0:
return getString(R.string.title_bag_content_format_plant).toUpperCase(l);
}
} else {
switch (position) {
case 0:
return getString(R.string.title_bag_content_format_short).toUpperCase(l);
case 1:
return getString(R.string.title_bag_content_format_long).toUpperCase(l);
case 2:
return getString(R.string.title_bag_content_format_plant).toUpperCase(l);
}
}
return null;
}
This is working correctly and do the filter by profile type more versatile.
Thanks to Filpe.costa01 that gave me the push that I needed.
You're returning all the tabs because your method
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
return 3 tabs. Maybe you could use the SectionsPagerAdapter constructor to include the number of tabs you'd like, according to each profile wanted.
Something like this:
private mNumOfTabs;
public SectionsPagerAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
mNumOfTabs = numberOfTabs;
}
and you should replace your getCount() method for this one:
#Override
public int getCount() {
// Show 3 total pages.
return mNumOfTabs;
}
Organizing your profiles in an ascendant way (low profile = small number of tabs), you can use this method like it's now
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FormatShortFragment();
case 1:
return new FormatLongFragment();
case 2:
return new FormatPlantFragment();
}
return null;
}
Otherwise, you should think about the profiles and implement a new logic to retrieve the correct tabs for each profile.

How can I set fragment Id in some of the override method of FragmentPagerAdapter?

I have a FragmentPagerAdapter, and in the fragment class I have a public method that I need to use from main activity.
In this point I don't know how to access the fragment that contains that method. Can I modify something on the class below to set id or something to fragments?
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment_1();
case 1:
return new Fragment_2();
case 2:
return new Fragment_3();
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.fg_1).toUpperCase(l);
case 1:
return getString(R.string.fg_2).toUpperCase(l);
case 2:
return getString(R.string.fg_3).toUpperCase(l);
}
}
return null;
}
}
How can I set a Fragment id, or Fragment tag or something to acces the fragment later?
Any other way to access fragment methods could be good.
EDITED WITH MORE INFORMATION:
I'm really lost with this. But, I have a ViewPager too, these fragments are associated to actionBar Tabs.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
Is there any way to communicate with the fragments?
Like so :
switch (position) {
case 0:
Fragment f = new Fragment_1();
f.setId(...);
return f;
case 1:
...
}
This assumes you have a setId method in your fragment.
What I do however is define a static string TAG per fragment, I can then access it with Fragment_1.TAG.

Categories

Resources