There is three tab fragment in my app when tab at index 0 onCreate called when app launch, tab ta index 1 onCreateView method not called. I need to update UI in tab 1 onCreate method. How do i resolve this kind of issue.
code:
private void setupViewPager(ViewPager viewPager) {
adapter.addFragment(new Users());
adapter.addFragment(new FavouriteUsers());
adapter.addFragment(new Recent());
viewPager.setAdapter(adapter);
}
page Adapter code:
public class UserPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public UserPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
}
Try to Use getChildFragmentManager() to initilize your UserPagerAdapter
UserPagerAdapter adapter = new UserPagerAdapter(getChildFragmentManager())
if this doesn't work try changing FragmentStatePagerAdapter to FragmentPagerAdapter.
implement your getItem(int position) like this.
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Users tab1 = new Users();
return tab1;
case 1:
FavouriteUsers tab2 = new FavouriteUsers();
return tab2;
case 2:
Recent tab3 = new Recent();
return tab3;
default:
return null;
}
}
and in your tab activity call adapter like this
final UserPagerAdapter adapter = new UserPagerAdapter
(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Related
i have a viewPager that contains 3 tabs fragments.
the first and the second fragments works fine but when i sweep to the third tab
i do not see him.
the third tab has shown only when the activity with the viewPager has reload(move to another activity and return).
my code:
private void setupViewPager(ViewPager viewPager) {
adapter = new PagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment1(), Constants.TAB_0);
adapter.addFragment(new Fragment2(), Constants.TAB_1);
adapter.addFragment(new Fragment3(), Constants.TAB_2);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
}
pager adapter:
public class PagerAdapter extends FragmentPagerAdapter {
/*
* Arraylist used to contain the fragments
* and contain fragments title's
*/
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
//Adds the fragment and it's title. Called in MainActivity
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
//Retrieves the title of the tab
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Here is my adapter, on getItem I set the fragment object to the Fragment activity.
public class SectionPagerAdapter extends FragmentPagerAdapter {
Fragment fragment;
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0){
fragment = new FragmentOne();
}
else if (position == 1){
fragment = new FragmentTwo();
}
else if (position == 2){
fragment = new FragmentThree();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "name one";
case 1:
return "name two";
case 2:
return "name three";
}
return null;
}
}
Then in my main activity I call it like so:
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
SectionPagerAdapter sectionPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(sectionPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
Try replacing your code with above and let me know if it helps!
USE new PagerAdapter(getChildSupportFragmentManager());instead of getSupportFragmentManager()
Try this one !!
I have a tablayout (tab1, tab2, tab3) with a ViewPager. It seems that on app startup the tab2 is preloaded which is fine.
However, I need to input some data on tab1 and I want the tab2 to update its fragment and hide some buttons based on the data input from tab1.
How to do that? Is there a way to not preload the tab so that when I swipe to tab2, it will be getting the latest data instead of the old data?
Here's my code
private ViewPager viewPager;
private PagerAdapter adapter;
private TabLayout tabLayout;
in my onCreate method within an activity.
tabLayout.addTab(tabLayout.newTab().setText("Input"));
tabLayout.addTab(tabLayout.newTab().setText("Detail"));
tabLayout.addTab(tabLayout.newTab().setText("Chart"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Here's my pageadapater class
public class PagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
private InputFragment inputFragment;
private DetailFragment detailFragment;
private ChartFragment chartFragment;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
inputFragment = new InputFragment();
return inputFragment;
case 1:
detailFragment = new detailFragment();
return detailFragment;
case 2:
chartFragment = new ChartFragment();
return chartFragment;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
on your detailfragment fragment
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//check values on tab 1 and try to update your tab 2 views accordingly
}
}
I have a tablayout (tab1, tab2, tab3) with a ViewPager. I want to input something in tab1, and depends on the input, tab2 and tab3 will show data accordingly.
It seems every time the app start, tab1 (fragment) and tab2 (fragment) are loaded with correct data. I'm using static variable to pass data from tab1 to tab2. However, after I change some data in tab1, tab2 doesn't update itself base on the data from tab1, it shows the old data. Here are my codes
private ViewPager viewPager;
private PagerAdapter adapter;
private TabLayout tabLayout;
in my onCreate method within an activity.
tabLayout.addTab(tabLayout.newTab().setText("Input"));
tabLayout.addTab(tabLayout.newTab().setText("Detail"));
tabLayout.addTab(tabLayout.newTab().setText("Chart"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Here's my pageadapater class
public class PagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
private InputFragment inputFragment;
private DetailFragment detailFragment;
private ChartFragment chartFragment;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
inputFragment = new InputFragment();
return inputFragment;
case 1:
detailFragment = new detailFragment();
return detailFragment;
case 2:
chartFragment = new ChartFragment();
return chartFragment;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
I want to implement Tablayout addOnTabSelectedListener to open a new Activity on a certain tab click. There are 3 tabs and at present they are connected with fragments. I want to start an activity when I click position 1 tab. I can do that in onTabSelected but before opening activity it shows the fragment attached to that same position. How to remove that fragment?
public class MainActivity extends AppCompatActivity {
private TabLayout tabs;
private ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (TabLayout)findViewById(R.id.tabs);
viewpager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewpager);
tabs.addTab(tabs.newTab());
tabs.addTab(tabs.newTab());
tabs.addTab(tabs.newTab());
tabs.setupWithViewPager(viewpager);
tabs.addOnTabSelectedListener(onTabSelectedListener(viewpager));
}
private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager viewPager) {
return new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Frag1(), "ONE");
adapter.addFragment(new Frag2(), "TWO");
adapter.addFragment(new Frag3(), "THREE");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs));
onTabSelectedListener(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager viewPager) {
return new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Frag1(), "ONE");
adapter.addFragment(new Frag2(), "TWO");
adapter.addFragment(new Frag3(), "THREE");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs));
onTabSelectedListener(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
try this:
use a FragmentStatePagerAdapter instead the reason for this is that the FragmentPagerAdapter will keep all the views that it loads into memory forever. Where the FragmentStatePagerAdapter disposes of views that fall outside the current
Also Override the adapter method getItemPosition
For example:
private class ViewPagerAdapter extends FragmentStatePagerAdapter {
//... your existing code
#Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
}
source
it will recreate your fragment each time instead of loading them from memory first..
To start activity do it in your particular fragment's onCreate()
Hope this helps..
I had same requirement, what i did is overlap textview on center of TabLayout using RelativeLayout & apply click event on textview. I can not post my layout please refer demo image
I am using ViewPager to create 3 Fragment on Activity1,
this is my viewPager adapter :
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment1 tab1 = new Fragment1();
return tab1;
case 1:
Fragment2 tab2 = new Fragment2();
return tab2;
case 2:
Fragment3 tab3 = new Fragment3();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
this is Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
TabLayout tab_layout = (TabLayout) findViewById(R.id.tab_layout);
tab_layout.addTab(tab_layout.newTab().setText("Frag1"));
tab_layout.addTab(tab_layout.newTab().setText("Frag2"));
tab_layout.addTab(tab_layout.newTab().setText("Frag3"));
final ViewPager view_pager = (ViewPager) findViewById(R.id.viewpager);
final ViewPagerAdapter adapter = new ViewPagerAdapter
(getSupportFragmentManager(), tab_layout.getTabCount());
view_pager.setAdapter(adapter);
view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
view_pager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
When Activity1 is open it show Fragment1 . But I want to show Fragment2 or Fragment3 instead .
How I can to do that?
Add this line after viewpager.setAdapter(adapter); :
viewPager.setCurrentItem(index);
Index is depending on you , how many fragments you have. tabLayout.getTabCount(); will return you tab count. But index is starting from 0.
If you have 3 Fragment and want to show third Fragment you must use viewPager.setCurrentItem(2);
Set current position of viewpager to your desired possition which is 2 or 3 in your case.
new Handler().post(new Runnable() {
#Override
public void run() {
mViewPager.setCurrentItem(desiredPos);
}
});
seee more
Android ViewPager setCurrentItem not working after onResume