Dynamicbox
Dynamicbox copied to clipboard
Why Layout will be empty in fragment?
hi, when i use dynamicbox in WebView fragment. dynamicbox will empty my webView fragment Layout when i set the layout heigh="match_parent".
xml Code( WebView Fragment):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
<LinearLayout
android:id="@+id/shopping_cart_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<WebView
android:id="@+id/shopping_webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
</LinearLayout>
Fragment Code
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dynamicBox = new DynamicBox(ShoppingCartContentPageFragment.this.getActivity(), frameLayout);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
sessionManager = new SessionManager(getActivity());
if(sessionManager.getLang().equals("tc")){
lang = "zh";
}else{
lang = "en";
}
actionBar = ((SupportActivity) getActivity()).getSupportActionBar();
actionBar.setTitle(getLocaleStringResource(new Locale(lang), R.string.system_shopping_cart, getActivity()));
MainActivity.mDrawerToggle.setDrawerIndicatorEnabled(true);
if(view == null){
view = inflater.inflate(R.layout.fragment_shopping_cart, container, false);
initWebView();
}else{
ViewGroup viewGroup = (ViewGroup)view.getParent();
if(viewGroup != null){
viewGroup.removeView(view);
}
}
return view;
}
private void initWebView(){
mWebView = (WebView) view.findViewById(R.id.shopping_webView);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
dynamicBox.setLoadingMessage(getLocaleStringResource(new Locale(lang), R.string.system_loading, getActivity()));
dynamicBox.showLoadingLayout();
webViewSuccess = true;
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
if(webViewSuccess){
dynamicBox.hideAll();
view.loadUrl("javascript:$('.shopping-cart-wrapper').css('padding-top','0px');");
view.loadUrl("javascript:$('.steps-contaier').css('padding-top','0px');");
view.loadUrl("javascript:$('#cart-to-shopping').remove();");
view.setVisibility(View.VISIBLE);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
return true;
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e("error_code", description);
mWebView.setVisibility(View.GONE);
webViewSuccess = false;
dynamicBox.setOtherExceptionTitle("Error");
dynamicBox.setOtherExceptionMessage(description);
dynamicBox.showExceptionLayout();
dynamicBox.setClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dynamicBox.hideAll();
mWebView.reload();
}
});
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
});
// Enable Javascript
mWebView.getSettings().setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
mWebView.getSettings().setAppCachePath(getActivity().getApplicationContext().getCacheDir().getAbsolutePath());
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
mWebView.loadUrl(AppConfig.URL_ROOT + sessionManager.getLang() + "/cart/");
}
But when i hide all Dynamicbox code. This fragment can show webview content
What wrong i make? can you help me?
Hi @cyyuk, Could move the DynamicBox initialization to onViewCreated https://developer.android.com/reference/android/app/Fragment.html#onViewCreated(android.view.View,android.os.Bundle)
HI @medyo
after move the DynamicBox initialization to onViewCreated
@Override
public void onViewCreated(View view,
Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
dynamicBox = new DynamicBox(HomeContentPageFragment.this.getActivity(), view);
}
I run in API 18(android 4.1) your code is OK, layout is not empty. But I run in >=API 22(Android 5.1.1), layout also empty.....