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;
}
}
Related
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) {
}
});
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 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
I have 2 tabs and 2 fragments.I did all for this guide
http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
but now I see tabs but dont see my fragments.What is the problem?
ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
Direct tab1 = new Direct();
return tab1;
case 1:
AdventuresFragment tab2 = new AdventuresFragment();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
Activity with using this adapter:
public class TabsActivity extends Activity {
Fragment directFragmentTab = new Direct();
Fragment adventuresFragmentTab = new AdventuresFragment();
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
() {
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
};
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) {
}
});
}
}
In your activity you are using the PagerAdapter class while your fragment handling class is named ViewPagerAdapter. You want to use the ViewPagerAdapter class instead.
hey guys i implemented a tab layout(an action bar with tabs) , basically it is made of using pagerAdapter and FragmentStatePagerAdapter below is my code :
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
case 3:
TabFragment4 tab4 = new TabFragment4();
return tab4;
case 4:
TabFragment5 tab5 = new TabFragment5();
return tab5;
default:
return null;
}
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
super.destroyItem(container, position, object);
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
}
#Override
public int getCount() {
return mNumOfTabs;
}}
my mainActivty:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab4"));
tabLayout.addTab(tabLayout.newTab().setText("Tab5"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter 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) {
}
});
now i want to remove the tabs(fragments) and for that am just removing them from here
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
tabLayout.addTab(tabLayout.newTab().setText("Tab4"));
if (condition == true){ // do nothing which means don't add the fragment }
else {
tabLayout.addTab(tabLayout.newTab().setText("Tab5")); }
and is working but not as it supposed to , even after removing the the tab from here tabLayout.addTab(tabLayout.newTab().setText("Tab5")); the fragments are still there and actively performing their task , i want to shut them completely , am new in android so have no idea about this that its a good approach or not guys is it enough or not please let me know any guidance will be so helpful for me