packagecom.example.toutiao.activity;// ...
importandroidx.annotation.RequiresApi;importandroidx.appcompat.app.AppCompatActivity;importandroidx.core.widget.NestedScrollView;importcom.airbnb.lottie.LottieAnimationView;importcom.example.toutiao.R;importcom.google.android.material.floatingactionbutton.FloatingActionButton;/**
* A Activity to be showed news detail.
*/publicclassNewsDetailActivityextendsAppCompatActivity{privateWebViewmNewsDetailWebView;privateProgressBarmProgressBar;privateButtonmBackButton;privateLottieAnimationViewmLoadingAnimationView;privateFloatingActionButtonmScrollToTopFAB;privateNestedScrollViewmWebNestedScrollView;@RequiresApi(api=Build.VERSION_CODES.M)@SuppressLint("SetJavaScriptEnabled")@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_news_detail);Bundleargs=getIntent().getExtras();Stringurl="";if(args!=null){url=args.getString("source_url");}Windowwindow=getWindow();window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);window.setStatusBarColor(getResources().getColor(R.color.tabbed_bg));mLoadingAnimationView=findViewById(R.id.animation_view_loading);mLoadingAnimationView.setAnimation("load-animation.json");mLoadingAnimationView.setSpeed(1);mLoadingAnimationView.playAnimation();mNewsDetailWebView=findViewById(R.id.web_view);mProgressBar=findViewById(R.id.progress_bar_loading);mProgressBar.setMax(100);mProgressBar.setProgress(1);// avoid the url has "http"
if(url.contains("http")){setNewsDetailWebView(url);}else{setNewsDetailWebView("https://m.toutiao.com"+url);}mBackButton=findViewById(R.id.button_back);setBackButtonOnClick();mWebNestedScrollView=findViewById(R.id.nested_scroll_view_web);mWebNestedScrollView.setOnScrollChangeListener(newView.OnScrollChangeListener(){@OverridepublicvoidonScrollChange(Viewv,intscrollX,intscrollY,intoldScrollX,intoldScrollY){if(scrollY==0){// hide FAB when NestedScrollView is at the top
mScrollToTopFAB.hide();}else{mScrollToTopFAB.show();}}});mScrollToTopFAB=findViewById(R.id.fab_scroll_to_top);mScrollToTopFAB.hide();mScrollToTopFAB.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){// scroll to top
mWebNestedScrollView.smoothScrollTo(0,0);}});}@SuppressLint("SetJavaScriptEnabled")privatevoidsetNewsDetailWebView(Stringurl){// TODO: Make WebView Faster
mNewsDetailWebView.setWebChromeClient(newWebChromeClient(){publicvoidonProgressChanged(WebViewview,intprogress){mProgressBar.setProgress(progress);}});mNewsDetailWebView.setWebViewClient(newWebViewClient(){@OverridepublicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){mProgressBar.setVisibility(View.VISIBLE);returnsuper.shouldOverrideUrlLoading(view,url);}@OverridepublicvoidonPageFinished(WebViewview,Stringurl){mLoadingAnimationView.setVisibility(View.GONE);mProgressBar.setVisibility(View.GONE);}});mNewsDetailWebView.getSettings().setAppCacheEnabled(true);mNewsDetailWebView.getSettings().setLoadsImagesAutomatically(true);mNewsDetailWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);// hardware acceleration
mNewsDetailWebView.setLayerType(View.LAYER_TYPE_HARDWARE,null);// enabling javascript
mNewsDetailWebView.getSettings().setJavaScriptEnabled(true);// enable Dom storage
mNewsDetailWebView.getSettings().setDomStorageEnabled(true);mNewsDetailWebView.loadUrl(url);}privatevoidsetBackButtonOnClick(){mBackButton.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){Leave();}});}@OverridepublicvoidonBackPressed(){super.onBackPressed();if(mNewsDetailWebView.canGoBack()){mNewsDetailWebView.goBack();}else{Leave();}}/**
* back to MainActivity
*/publicvoidLeave(){// mNewsDetailWebView.clearCache(true);
// mNewsDetailWebView.clearHistory();
// mNewsDetailWebView.clearFormData();
finish();overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);}}