Reputation: 3829
I have multiple WebViews
stored in a HashMap
which I need to be able to show/hide at any time.
When a row in a ListView
is tapped, I need to show the WebView
associated with that row and hide all the others. The other WebViews
cannot be destroyed as I do not wish to unload their contents.
Is this possible?
Creation and storage of the WebView
in one Fragment
:
public WebView createWebView(Long id) {
WebView wv = new WebView(getActivity());
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:(registerCredentialsChangedCallback(function(credentials) {if (credentials.length >= 1) {window.location.href = 'callback:' + credentials;} else {alert('Error: callback set by registerCredentialsChangedCallback was given null string');}})) ()");
}
});
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
wv.loadUrl(makeViewerURL(id));
webViews.put(id, wv);
return wv;
}
onCreate
in another Fragment
. I'm using an interface
to pass the id via the Activity
to the Fragment
where the WebView
should be displayed:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_directory, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.list);
DatabaseHelper dbHelper = new DatabaseHelper(getActivity());
android.R.layout.simple_list_item_1, android.R.id.text1, values);
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1, dbHelper.getViewers(), new String[] { "description", "_id" }, new int[] { android.R.id.text1 }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Long id = adapter.getItemId(i);
mCallback.onViewerSelected(id);
}
});
return rootView;
}
XML Layout of View which should contain WebView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF" >
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Thanks!
Upvotes: 1
Views: 793
Reputation: 9994
Yes, it is possible - but you need to have a key value pairs of id and WebView
.
private Map<Long, View> mWebViews = new HashMap<Long, View>();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Long id = adapter.getItemId(i);
if(mViews.contains(id))
mWebView = mViews.get(id)
else
mWebView = createWebView();
}
});
Then you just need to find the corresponding WebView
which the user selected in List and make it visible. In order to hide the WebView
you need to use:
mWebView.setVisibility(View.Gone);
Addenda: instead of webView in your xml file, use a frameLayout such as:
<FrameLayout
android:id="@+id/web_container"
android:layout_width="fill_parent"
android:layout_height="420dip"
android:layout_gravity="center"
android:clickable="true"
android:enabled="true"
android:paddingLeft="10dip"
android:paddingRight="10dip" />
And then in the code add the WebView to Framelayout:
FrameLayout mWebContainer = (FrameLayout) findViewById(R.id.web_container);
webView = new WebView(getApplicationContext());
mWebContainer.addView(webView);
Upvotes: 1