One Fragment changing WebView in ViewPager
What i want to do: Change the WebView of a fragment and add all the
WebViews, that i have, to the PagerAdapter.
What my Problem is: I want to have only 1 Fragment-Class(MyFragment). My
Fragment-Activity should add this MyFragment to a list of fragments and
then change the WebView of my Fragment-Class. Now it should add this
(same) MyFragment again to the list. In the end i give this list to the
Pager Adapter. And what i can't manage is, to change the WebView of
MyFragmentfrom inside the ViewPagerFragmentActivity. I think most of the
code is pretty unimportant, it is mostly about how to get the webview in
the fragment replaced by a new webview.
MyFragment:
public class MyFragment extends Fragment {
WebView wv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.tab_fragmentwebview_layout,
container, false);
}
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
wv = (WebView) getView().findViewById(R.id.fragmentwebview);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("file:///android_asset/web/html/content1.htm");
}
}
ViewPagerFragmentActivity:
public class ViewsPagerFragmentActivity extends ViewsPagerAdapter {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_viewpager);
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
// set the tab as per the saved state
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
this.initialiseViewPager();
setTabColor(mTabHost);
}
// Initialize ViewPager
protected void initialiseViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, MyFragment.class.getName()));
//change the WebView here with something like
wv.loadUrl("file:///android_asset/web/html/content2.htm");
fragments.add(Fragment.instantiate(this, MyFragment.class.getName()));
//change the WebView here
fragments.add(Fragment.instantiate(this, MyFragment.class.getName()));
this.mPagerAdapter = new
MyPagerAdapter(super.getSupportFragmentManager(), fragments);
this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
}
// Initialize the Tab Host
protected void initialiseTabHost(Bundle args) {
// this is not so important, i let it out here at this place
}
Do you need more information on the problem?
Is it even possible what i want to do? Are there better/more professional
ways to do so?
In the end i want to read several html-pages from a folder and put them
into a List. Then adapt the Fragment(actually the webview) with all these
html-pages and initialise them to my ViewPager.
No comments:
Post a Comment