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.
Related
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.
HI i am trying to add image to the new TabLayout introduced in android support design. But it doesn't seems to be working i took some code github and tried to do some little change except for text nothing is workin no icon is showing on tha tab instead of text.
Any help is appreciated.
List<String> titles = new ArrayList<>();
titles.add("Page One");
titles.add("Page Two");
mTabLayout.addTab(mTabLayout.newTab().setIcon(getResources().getDrawable(R.drawable.ic_launcher)));
mTabLayout.addTab(mTabLayout.newTab().setIcon(getResources().getDrawable(R.drawable.ic_launcher)));
List<Fragment> fragments = new ArrayList<>();
fragments.add(new AnimatedFragment());
fragments.add(new AnimatedFragment());
FragmentAdapter adapter =
new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(0);
mTabLayout.setupWithViewPager(viewPager);
mTabLayout.setTabsFromPagerAdapter(adapter);
This is the code where i setup tablayout and viewpager.
You can add icons to tablayout from viewPager's getPageTitle method. Here is a good tutorial tutorial to do so that I tried.
I have a ViewPager, defined in an Activity, and many Fragments sequentially shown in the ViewPager. In these fragments there are dynamically constructed checkboxes and radiobuttons, which the user is supposed to manipulate. On the very moment that the user swipes to the next page I need the user data to be retrieved and stored in the Application object. I can't figure out what the standard way of doing this is. Since there are many Fragments I opted for using the FragmentStatePagerAdapter. Any help would be welcome, thanks in advance!
Update-1:
I do have this:
pageAdapter = new MyPageAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
// detects viewpager page change
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
Log.i("TAG", "onPageSelected");
int index = pager.getCurrentItem();
MyPageAdapter adapter = ((MyPageAdapter) pager.getAdapter());
QuestionFragment fragment = (QuestionFragment) adapter.getItem(position);
if (fragment.rdbtn != null) {
for (int i = 0; i < fragment.rdbtn.length; i++) {
if (fragment.rdbtn[i].isChecked())
Log.i("TAG", "checked");
else
Log.i("TAG", "not checked");
}
}
// fragment.refresh();
}
});
When checking the debugger, after starting up, the ViewPager instantiates Fragments 0 and 1 (standard behavior). When the user manipulates fragment-0 and swipes, the handler is indeed called but with position=1, not 0. And the public elements I want to read are null!
UPDATE-2
I notice in the debugger that the data I need is stored in adapter.mCurrentPrimaryItem.
How to retrieve CurrentPrimaryItem in the code?!
You can implement a pageChangeListener in your activity and set this to the viewPager.
Then you can have a class abstract BaseFragment extends Fragment and declare an abstract method, say, getData() that every fragment in the ViewPager extends and overrides the method.
And in onPageSelected() of the activity you can access those data.
I have an app with a ViewPager which contains two fragments: a main fragment and a list fragment. If something in the list is selected, an event is raised which is handled in the activity. The activity switches to the main fragment and calls the "setSelectedItem" method. In this method,
I use getView() to find a specific view to change the text according to the selected item.
Most of the time it works, but sometimes getView returns null and I don't know why.
The pagerAdapter is created in OnCreate of the activity:
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager());
ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
The pager adapter creates instances of both fragments in its constructor:
public PagerAdapter(FragmentManager fm) {
super(fm);
this.main = MainFragment.newInstance();
this.list = ListFragment.newInstance();
}
This is the method in the activity which is called when an item from the list is selected:
ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
pager.setCurrentItem(0, true);
((MainFragment)mPagerAdapter.getItem(0)).setSelectedItem(id);
And finally this is the line in the setSelectedItem method in the MainFragment which causes the problem:
TextView s = (TextView)getView().findViewById(R.id.CurrentSelection);
I tried several things I read about but the problem still occurs irregularly.
To start, I am not 100% sure, but I have a couple of points I need to mention. First, I don't like how you are using this and super like that, they seem to be vague, I believe a better way is to be more accurate considering pre-compile you are aware of their values. I think if you change your code to as following, it would help (just a hunch).
// No need for 'this' and 'super' here, you are within the activity
mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
public PagerAdapter(FragmentManager fm) {
super(fm);
// No need for 'this' here, you are within the class
main = MainFragment.newInstance();
list = ListFragment.newInstance();
}
// No need for 'super' here, you are within the activity
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setCurrentItem(0, true);
((MainFragment)mPagerAdapter.getItem(0)).setSelectedItem(id);
// Here use 'getActivity()' instead of 'getView()'
TextView s = (TextView)getActivity().findViewById(R.id.CurrentSelection);
Let me know what happens, cheers.
I have a working PagerActivity (extends FragmentActivity) with a bunch of Fragment objects in it. Everything works fine but what I want to do now is to display the number of the Fragment in the list (let's say there are 25 Fragment objects and I want to display the current number in that list of Fragments).
How can I do that? Is there some method I can call in the Fragment object?
I'm filling the PagerAdapter like this (snippet):
fragments = new Vector<Fragment>();
for (int i = 0; i < wiList.length; i++) {
WhiteboardImage tmpWi = wiList[i];
Bundle bundle = new Bundle();
bundle.putString("wiImage", gson.toJson(tmpWi));
bundle.putInt("positionInList", positionInList);
bundle.putInt("totalCount", wiList.length);
fragments.add(Fragment.instantiate(params[0], GalleryFragment.class.getName(), bundle));
}
mPagerAdapter = new GalleryPagerAdapter(getSupportFragmentManager(), fragments);
Thanks for any help!
I assume GalleryPagerAdapter is a FragmentViewPager? Then the number of fragments would be fragments.size(), no?