Dynamic Tab with Fragment - android

total novice,but bit familiar with static tabs addition to view pager.But here I stuck in dynamic tabs which I am able to show but not the fragments of those dynamically received tabs.How would I add fragments to those dynamically received tabs from server.Pardon me spelling and could be ambiguous question

Follow this link example and change the following method.
Visit https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
for(int i=0;i<NUM_OF_FRAGMENT;i++){
adapter.addFragment(new YourFragment(), "Title"+i);
}
viewPager.setAdapter(adapter);
}
You need only one fragment for creating dynamic tab.

Related

Setting View's atribute that is in child fragment

I want to access Views that are in Fragment's children that are put in ViewPager. The idea is to toggle (disable/enable) spinners when button is pressed. Spinners are placed in separate fragments that are displayed in ViewPager controlled by TabLayout:
// Snippet of code from Fragment A
final TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
Fragment childFragment = new Fragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.viewPager, childFragment).commit();
ViewPagerAdapter adapter = new ViewPagerAdapter(childFragment.getFragmentManager());
adapter.AddFragment(new fragment_unositelj(), "FragmentB");
adapter.AddFragment(new fragment_voditelj(), "FragmentC");
adapter.AddFragment(new fragment_pratitelj(), "FragmentD");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
FragmentB = viewPager.findViewById(R.id.FragmentB);
FragmentC = viewPager.findViewById(R.id.FragmentC);
FragmentD = viewPager.findViewById(R.id.FragmentD);
I don't know how to achieve to set Spinner disabled / enabled in all fragment children's spinner. I tried to set public variable in MainActivity and to control Spinners attribute based on that, but I am not sure in which lifecycle part to place it because fragment B is inflated initialy, and others are when TabItem clicked. I hope I could describe my problem properly. If anything more is needed please let me know. Thanks!
I found workaround for this problem, I am not sure if it is correct way, but for now it works for me. In FragmentB,FragmentC,FragmentD i added
#Nullable
#Override
public View getView() {
if (((MainActivity) getActivity()).getStatus() == 1) {
mySpinner.setEnabled( true );
} else
mySpinner.setEnabled( false );
return super.getView();
}
//(MainActivity) getActivity()).getStatus() - method that returns state if it shold be enabled

Replacing the Fragment in Tablayout Android

I used the tablayout in my project. I have to replace the fragment in the tablayout. The code is like below
Activity Class
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(Fragment1, "One");
adapter.addFragment(Fragment2, "Two ");
adapter.addFragment(Fragment3, "Three");
viewPager.setAdapter(adapter);
}
Interface in Activity
#Override
public void onInput(int input) {
Here how to replace fragment three
}
I think replacing the fragment is not a good idea. So I would like to show you another way to achieve what you want.
viewPager.setCurrentItem(position); you can use this method to change the page. Here position is page-number starting from 0 to total-pages - 1.
Use ViewModal to pass latest data (in your case "input"). So here you can set value from onInput(int input) and in each fragment of ViewPager you can subscribe/ unsubscribe ViewModal to listen changes. In this way you do not need to replace fragment and the page which is on screen will get latest data and can show.

How to Clear first tab view data in android Tab View [duplicate]

This question already has answers here:
Android ViewPager Refresh fragment
(2 answers)
Closed 4 years ago.
I implemented TabView with ViewPager for my Application. The issue is that when I swipe view from the first tab to the second tab the view which related to first tab remaining same. I need to clear all TextViews once I leaving that view.
What I found that when I click the first tab it loads two Fragments
I solve that?
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new VoiceFragment(), "Voice");
adapter.addFragment(new DataFragment(), "Data");
adapter.addFragment(new IddFragment(), "Idd");
viewPager.setAdapter(adapter);
switch (openedActivity) {
case "Voice":
pageId = 0;
break;
case "Data":
pageId = 1;
break;
case "Idd":
pageId = 2;
break;
}
viewPager.setCurrentItem(pageId);
}
You need to set offscreenpage limit of your viewpager to 1.
viewpager.setOffscreenPageLimit(1);
Hope this helps

ViewPager TabLayout issue: tab fragment content only appears after last tab has been swiped to

I have a ViewPager to which I am adding 3 tab fragments dynamically:
private void addTabs(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new QuotesFragment(), "Quotes");
adapter.addFragment(new BooksFragment(), "Books");
adapter.addFragment(new ExercisesFragment(), "Exercises");
viewPager.setAdapter(adapter);
}
Here's the code of the fragment that is not working as expected:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
recyclerView = (RecyclerView) getActivity().findViewById(R.id.quotes_recycler);
layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
MasonryAdapter adapter = new MasonryAdapter(context, quotesList);
recyclerView.setAdapter(adapter);
}
The RecyclerView view holders form QuotesFragment() will not show up at parent Activity's start. They only show up when I have scrolled to the last tab (ExercisesFragment() in this case) and returned to the first one. Checking in Hierarchy Viewer it seems that at the app start the RecyclerView is initialized (the container takes necessary space on the screen), but its children are not.
What's odd, when I change the order in which I am adding tabs like so (note my problematic tab is now in the middle):
private void addTabs(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ExercisesFragment(), "Exercises");
adapter.addFragment(new QuotesFragment(), "Quotes");
adapter.addFragment(new BookFragment(), "Books");
viewPager.setAdapter(adapter);
}
it does never load! I have tried various things, and there's even one question about exactly the same problem, but it does not help in my case. Could it have to do with Fragment/ViewPager lifecycle or implicit methods?
Any help appreciated.
I finally got it to work and it's dumb simple really. I was calling my method that loads quotes from JSON inside Fragment's onCreate() instead of onViewCreated(). When I moved it to onViewCreated() (after setting adapter and layout manager) it started working.

Create Multiple Fragments Dynamically for Dynamic Tabs

I have a requirement and I do not know how to accomplish this. Can you guys help me out to get this done. Below is what the flow is
I have a Main Category list, user can tap to get sub categories (fetch from server) and based on the result I am displaying a list of subcategories with their child category as well. Next I am creating tabs based on the child categories.
Now my problem is creating fragments for that number of tabs. Childs can be from 1 to n number, so I would like to know the best way to create fragments dynamically. Once each fragment I have to display a list of items that would be fetched from server.
Thank you
You can use the following code snap
You will need to create corresponding layout xml
ViewPager pager;
ViewPagerAdapter adapter;
private List<Fragment> fragments;
ArrayList<String> Titles;
#Override
protected void onCreate(Bundle savedInstanceState) {
fragments = new ArrayList<Fragment>();
Titles= new ArrayList<String>();
pager = (ViewPager) findViewById(R.id.pager);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, fragments);
pager.setAdapter(adapter);
for (int i=0;i<numoffragments;i++){
fragments.add(new DemoFragment());
}
adapter.notifyDataSetChanged();
}
You may need to do some more adjustments to get the numoffragments you will be using and also you have to make a fragment that can be used for all the childs
Hope this wil help.

Categories

Resources