when i move to another tab, every time its creating new.
am getting tabname and catid from server,
private void setTabTitle() {
Final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
mviewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
viewPager.setOffscreenPageLimit(jSonTab.size());
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(mviewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mviewPager.setCurrentItem(tab.getPosition()); // Get current clicked Tab Position
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
});
}
public Parcelable saveState() {
return null;
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return MainFragment.newInstance(CatId.get(position), tabName.get(position));
}
#Override
public int getCount() {
return tabName.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabName.get(position);
}
}
when i change anything in first tab, and go anyother tab, first tab is refreshing and creating new instance,
everytime am calling server to fetch the details for the particular tab
Try setting viewPager.setOfscreenPagelimit(noOfPagesInViewPager);
class ViewPagerAdapter extends FragmentStatePagerAdapter {
Mainfragment mainfragment;
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
mainfragment=new Mainfragment();
}
#Override
public Fragment getItem(int position) {
return mainfragment(CatId.get(position), tabName.get(position));
}
#Override
public int getCount() {
return tabName.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabName.get(position);
}
}
Related
Here's my tabLayoutCode
mTabLayoutBerita.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Log.i("onTabSelected", String.valueOf(tab.getPosition()));
mViewPagerBerita.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
And this is in my PagerAdapter
public class PagerBerita extends FragmentPagerAdapter {
private int mNumOfTabs;
private FragmentBeritaByKategori fragmentBeritaByKategori = null;
private List<KategoriItem> mDataListKategoriBerita;
public PagerBerita(FragmentManager fm, int NumOfTabs, List<KategoriItem> mDataListKategoriBerita) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.mDataListKategoriBerita = mDataListKategoriBerita;
}
#Override
public Fragment getItem(int position) {
fragmentBeritaByKategori = new FragmentBeritaByKategori(mDataListKategoriBerita.get(position).getId());
return fragmentBeritaByKategori;
}
#Override
public int getCount() {
return mDataListKategoriBerita.size();
}
}
I'm trying to create dynamic tab layout, but when the tab already showed up, i get wrong data because the position i use in PagerAdapter is wrong. I've already use setOffscreenPageLimit and not working, i'm so confuse. Any solution for this case ? Thanks a lot.
You are forgot to add ViewPager to tab layout.
TabLayout tabsLayout = (TabLayout) findViewById(R.id.tabsLayout);
ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
//Add ViewPager to tab layout.
tabsLayout.setupWithViewPager(viewpager);
viewpager.setOffscreenPageLimit(8);
tabsLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewpager.setCurrentItem(tab.getPosition());
Log.i("TabLayout Position: " + tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
It working fine.
I do like this, is it wrong ?
final CustomViewPager viewPager = findViewById(R.id.pager);
final PagerAdapterBottomNavigation pagerAdapterBottomNavigation = new PagerAdapterBottomNavigation(getSupportFragmentManager(), tab_layout.getTabCount());
viewPager.setAdapter(pagerAdapterBottomNavigation);
viewPager.setCurrentItem(2);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
tab_layout.getTabAt(2).getIcon().setColorFilter(getResources().getColor(R.color.secondaryTextColor), PorterDuff.Mode.SRC_IN);
tab.getIcon().setColorFilter(getResources().getColor(R.color.secondaryLightColor), PorterDuff.Mode.SRC_IN);
if(tab.getPosition() == 0) {
actionBar.setTitle("Menu");
} else if(tab.getPosition() == 1) {
actionBar.setTitle("News");
} else if(tab.getPosition() == 2) {
actionBar.setTitle("Event");
} else if(tab.getPosition() == 3) {
actionBar.setTitle("Promo");
} else if(tab.getPosition() == 4) {
actionBar.setTitle("Setting");
}
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getIcon().setColorFilter(getResources().getColor(R.color.secondaryTextColor), PorterDuff.Mode.SRC_IN);
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
My PagerAdapter
public class PagerAdapterBottomNavigation extends FragmentPagerAdapter {
int mNumOfTabs;
private FragmentAllMenu fragmentAllMenu;
private FragmentBerita fragmentBerita;
private FragmentEvent fragmentEvent;
private FragmentSetting fragmentSetting;
private FragmentPromo fragmentPromo;
public PagerAdapterBottomNavigation(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fragmentAllMenu = new FragmentAllMenu();
return fragmentAllMenu;
case 1:
fragmentBerita = new FragmentBerita();
return fragmentBerita;
case 2:
fragmentEvent = new FragmentEvent();
return fragmentEvent;
case 3:
fragmentPromo = new FragmentPromo();
return fragmentPromo;
case 4:
fragmentSetting = new FragmentSetting();
return fragmentSetting;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
But i check on PagerAdapter getItem(int position), the position wrong but show correct fragment.
I have a problem in my Application, I load a fragment in my activity and in the fragment i used the ViewPager(for TabLayout) so i want to handle the BackPressed of ViewPager.
Because there is no OnKeyDown override method is available in fragment so i want the refrence of my Fragment ViewPager in my Parent Activty so i can handle this situation in onBackPressed() method Activity.
Activity Code :
fragment= new TabFragment();
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.containerView, fragment);
ft.commitAllowingStateLoss();
// ft.commit();
}
#Override
public void onBackPressed() {
if (isMainShown) {
// want the refrence of view pager so i can handle ViewPager on backPress
}
else {
//display tab fragment
}
//super.onBackPressed();
}
TabFragment :
public class TabFragment extends Fragment {
public TabFragment() {
// Required empty public constructor
}
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2 ; View x;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
x=inflater.inflate(R.layout.content_main_menu,null);
tabLayout=(TabLayout) x.findViewById(R.id.tabs);
viewPager =(ViewPager) x.findViewById(R.id.viewpager);
setupViewPager(viewPager);
//viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
viewPager.setOffscreenPageLimit(1);
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private void setupViewPager(ViewPager viewPager) {
MyAdapter adapter = new MyAdapter(getChildFragmentManager());
adapter.addFragment(new TabOne(), "TAb1");
adapter.addFragment(new TabTwo(), "Tab2");
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
final InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(x.getWindowToken(), 0);
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private class MyAdapter extends FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
}
}......
add this code in your onBackPressed()
fragobjTab = (TabFragment) getSupportFragmentManager().findFragmentById(R.id.containerView);
try {
fragobjTab.onBackPressed();
}catch (Exception e){
e.printStackTrace();
}
And create a onBackPressed method in your tabFragment and setCurrentItem to the ViewPager:
public void onBackPressed()
{
// set page to your viewPager
// 0 is your first viewpager fragment
viewPager.setCurrentItem(0,true);
}
In my app i am using pager adapter to load 2 fragments, the fragments are not getting loaded and view pager remains null. Where i am going wrong? Any help will be appreciated
Here is my code
I am setting this adapter to my view pager from activity
public class ViewPagerActivityAdapter extends PagerAdapter {
ArrayList<Fragment> tablist;
public ViewPagerActivityAdapter(ArrayList<Fragment> tablist) {
this.tablist = tablist;
}
#Override
public int getCount() {
return tablist.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
return null;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
}
}
Activity Part
private ViewPagerActivityAdapter viewPagerAdapter;
private ArrayList<Fragment> fragments_faq;
fragments_faq = new ArrayList<>();
FragmentOne frag1 = new FragmentOne();
tablayout.addTab(tablayout.newTab().setText("fragone"));
fragments_faq.add(frag1);
FragmentTwo frag2 = new FragmentTwo();
tablayout.addTab(tablayout.newTab().setText("fragtwo"));
fragments_faq.add(frag2);
private void setPagerAdapter() {
viewPagerAdapter = new ViewPagerActivityAdapter(fragments_faq);
viewpager.setAdapter(viewPagerAdapter);
viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));
viewpager.setOffscreenPageLimit(2);
tablayout.addOnTabSelectedListener(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) {
}
});
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
for details check this gist
https://gist.github.com/vinay6kr/33359c3f3b0e9f28e33bd383b01e9d9f
if you want to show fragments in viewpager you should use FragmentStatePagerAdapter
public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[];
public MyViewPagerAdapter(FragmentManager fm, CharSequence mTitles[]) {
super(fm);
this.Titles = mTitles;
}
#Override
public Fragment getItem(int position) {
if (position == 0)
return new Fragment1().getInstance();
else if (position == 1)
return new Fragment2().getInstance();
}
#Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
#Override
public int getCount() {
return Titles.length;
}
}
and to Set them
void setUpTabs() {
String Titles[] = {"Frag1","Frag2"};
MyViewPagerAdapter adapter = new MyViewPagerAdapter(this.getSupportFragmentManager(), Titles);
viewPager.setAdapter(adapter);
}
You can try like adapter look like
class ViewPagerActivityAdapter extends FragmentPagerAdapter {
// fragments to instantiate in the viewpager
private List<Fragment> fragments;
// constructor
public ViewPagerActivityAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
// return access to fragment from position, required override
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
// number of fragments in list, required override
#Override
public int getCount() {
return this.fragments.size();
}
}
Add Fragment
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this,
FragmentOne.class.getName()));
fragments.add(Fragment.instantiate(this,
FragmentTwo.class.getName()));
Add to view Pager
ViewPagerActivityAdapter mPagerAdapter;
this.mPagerAdapter = new ViewPagerActivityAdapter(
super.getSupportFragmentManager(), fragments);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPager.setAdapter(this.mPagerAdapter);
You should use FragmentPagerAdapter or FragmentStatePagerAdapter for this.Here is sample code.
public class ViewPagerActivityAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> tablist;
public ViewPagerActivityAdapter(ArrayList<Fragment> tablist) {
this.tablist = tablist;
}
#Override
public int getCount() {
return tablist.size();
}
#Override
public Fragment getItem(int position) {
return tablist.get(position);
}
}
I have two fragments.. I want to refresh my listview adapter adapter.notifyDataSetChanged() in setUserVisibleHint method inside my first Fragment:
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
adapter.notifyDataSetChanged();
}
}
But I get this Exception:
java.lang.NullPointerException
at com.androidbelieve.drawerwithswipetabs.FirstFragment.setUserVisibleHint(FirstFragment.java:247)
at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1175)
I'm sorry I know a little about Fragments, what is this "setPrimaryItem" !?
This is my Tab Fragment :
public class TabFragment extends Fragment {
View x;
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2 ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(2);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
viewPager.getCurrentItem();
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new FirstFragment();
case 1 : return new ZZZFavorites();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Online";
case 1 :
return "Favorites";
}
return null;
}
}
}
Your adapter is null. Perform null-check before accessing it.
if (isVisibleToUser && adapter != null) {
adapter.notifyDataSetChanged();
}
Well I want to remove/hide FAB only on a particular fragment , HISTORY tab in my case .
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
.... (Overriding methods , I didnt do anything here
});
My methods for inflating View Pager
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HistoryFragment(), "HISTORY");
adapter.addFragment(new TransactionFragment(), "CURRENT");
adapter.addFragment(new FutureFragment(), "FUTURE");
viewPager.setAdapter(adapter);
}
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 android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(android.support.v4.app.Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Where and how should I make changes to my code to Hide my FAB from the HISTORY tab/fragment and show again on other Tabs
I'd think it's as simple as this:
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
if(position == 0) {
// hide FAB
} else {
// show FAB
}
}
}
You can do a lot of cleanup to improve maintenance like assign page numbers to each page like:
private static final int PAGE_HISTORY = 0;
private static final int PAGE_TRANSACTION = 1;
private static final int PAGE_FUTURE = 2;
So instead of if(position == 0) you say if(position == PAGE_HISTORY). That way you don't have to change a lot of code if you ever change your pages around.
You can try setting the OnTabSelectedListener to your TabLayout.
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
yourFAB.setVisibilty(View.GONE);
// or
yourFAB.setVisibilty(View.VISIBLE);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Just add this you your OnPageChangeListener:
#Override
public void onPageSelected(int position) {
if (adapter.getItem(position) instanceof HistoryFragment) fab.hide();
//else fab.show();
}