Issue
I want to create tabs dynamically on the basis of count of websites given in an array. On each click of tab I want to load a fragment with webview with website loaded in it by passing corresponding url of website from array. Can I implement pager in it too?
Thanks
I had posted a help request previously with my code at, or may be I was not able to define my problem there: Dynamically adding tabs in android with webview and fragment
Solution
Try this i ve done this long ago maybe few bugs but still:
Create arraylist of fragments and urls in mainactivity:
ArrayList<Fragment> fragments = new ArrayList<>();
ArrayList<String> urls = new ArrayList<>();
Now while u iterate through ur dynamic data keep adding:
while(iterator.hasNext()) {
fragments.add(new ftab1());
urls.add(url);
}
Now setup adapter passing above two arraylist:
PagerAdapter pageradapter= new PagerAdapter(getSupportFragmentManager(),getApplicationContext(),fragments,urls);
pager.setAdapter(pageradapter);
Now coming to PagerAdapter: Create two global variables
ArrayList<Fragment> fragments;
ArrayList<String> urls;
Define constructor and initialise the above 2 variables:
public PagerAdapter(FragmentManager fm, Context context, ArrayList<Fragment> fragments,ArrayList<String> urls) {
super(fm);
this.context = context;
this.fragments = fragments;
this.urls=urls;
}
Now main function is getItem() pass data in bundle to that activity:
@Override
public Fragment getItem(int i) {
Bundle data = new Bundle();
data.putString("url",urls.get(i));
fragments.get(i).setArguments(data);
return fragments.get(i);
}
Now get url in your ftab1 like this:
String url = getArguments().getString("url");
Answered By - aryanknp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.