如何在 Web 视图加载完成之前显示进度对话框?
How can I display a progress dialog until web view is finished loading?
我有一个 activity 可以加载一个片段,该片段内部是一个 Web 视图 如何在所有 Web 视图内容加载完成之前显示该片段的进度对话框?
我的片段:
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Using the "View" to obtain the WebView
View view = inflater.inflate(R.layout.fragment_one, container, false);
//in app WebView
WebView myFacebookWebView = (WebView) view.findViewById(R.id.webview_facebook);
WebSettings webSettings = myFacebookWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myFacebookWebView.setWebViewClient(new WebViewClient());
myFacebookWebView.setWebChromeClient(new WebChromeClient());
myFacebookWebView.loadUrl("https://www.google.com/");
return view;
}
}
我的Activity:
public class SocialActivity extends AppCompatActivity {
private TabLayout mTabLayout;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_social);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(mViewPager);
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
mTabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "ONE");
adapter.addFragment(new FragmentTwo(), "TWO");
adapter.addFragment(new FragmentThree(), "THREE");
adapter.addFragment(new FragmentFour(), "FOUR");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment,String title) {
mFragmentList.add(fragment);
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
}
您可以覆盖 WebViewClient 的 onPageFinished(WebView view, String url) 方法。
WebView myFacebookWebView = (WebView) view.findViewById(R.id.webview_facebook);
WebSettings webSettings = myFacebookWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myFacebookWebView.setWebChromeClient(new WebChromeClient());
// START PROGRESS DIALOG HERE
myFacebookWebView.loadUrl("https://www.google.com/");
myFacebookWebView.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView view, String url)
{
// DISMISS PROGRESS DIALOG HERE
}
}
);
您可以在 class MyWebViewClient
的事件 onPageFinished(成功时)或 onReceivedError(错误时)关闭进程对话框
webView.setWebViewClient(new MyWebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// close your process dialog:
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// close your process dialog:
}
});
如果webview加载时间过长(但仍然成功),请检查超时关闭。
您可以像这样显示和隐藏进度对话框
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ProgressDialog progressBar = new ProgressDialog(getActivity());
progressBar.setMessage("Getting Faqs details...Please wait....");
progressBar.show();
progressBar.setCancellable(false);
//Using the "View" to obtain the WebView
View view = inflater.inflate(R.layout.fragment_one, container, false);
//in app WebView
WebView myFacebookWebView = (WebView) view.findViewById(R.id.webview_facebook);
WebSettings webSettings = myFacebookWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
dismissProgressDialog();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
dismissProgressDialog();
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
myFacebookWebView.loadUrl("https://www.google.com/");
return view;
}
private void dismissProgressDialog(){
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
我有一个 activity 可以加载一个片段,该片段内部是一个 Web 视图 如何在所有 Web 视图内容加载完成之前显示该片段的进度对话框?
我的片段:
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Using the "View" to obtain the WebView
View view = inflater.inflate(R.layout.fragment_one, container, false);
//in app WebView
WebView myFacebookWebView = (WebView) view.findViewById(R.id.webview_facebook);
WebSettings webSettings = myFacebookWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myFacebookWebView.setWebViewClient(new WebViewClient());
myFacebookWebView.setWebChromeClient(new WebChromeClient());
myFacebookWebView.loadUrl("https://www.google.com/");
return view;
}
}
我的Activity:
public class SocialActivity extends AppCompatActivity {
private TabLayout mTabLayout;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_social);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(mViewPager);
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
mTabLayout.setupWithViewPager(mViewPager);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentOne(), "ONE");
adapter.addFragment(new FragmentTwo(), "TWO");
adapter.addFragment(new FragmentThree(), "THREE");
adapter.addFragment(new FragmentFour(), "FOUR");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment,String title) {
mFragmentList.add(fragment);
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
}
您可以覆盖 WebViewClient 的 onPageFinished(WebView view, String url) 方法。
WebView myFacebookWebView = (WebView) view.findViewById(R.id.webview_facebook);
WebSettings webSettings = myFacebookWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myFacebookWebView.setWebChromeClient(new WebChromeClient());
// START PROGRESS DIALOG HERE
myFacebookWebView.loadUrl("https://www.google.com/");
myFacebookWebView.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView view, String url)
{
// DISMISS PROGRESS DIALOG HERE
}
}
);
您可以在 class MyWebViewClient
的事件 onPageFinished(成功时)或 onReceivedError(错误时)关闭进程对话框webView.setWebViewClient(new MyWebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// close your process dialog:
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// close your process dialog:
}
});
如果webview加载时间过长(但仍然成功),请检查超时关闭。
您可以像这样显示和隐藏进度对话框
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ProgressDialog progressBar = new ProgressDialog(getActivity());
progressBar.setMessage("Getting Faqs details...Please wait....");
progressBar.show();
progressBar.setCancellable(false);
//Using the "View" to obtain the WebView
View view = inflater.inflate(R.layout.fragment_one, container, false);
//in app WebView
WebView myFacebookWebView = (WebView) view.findViewById(R.id.webview_facebook);
WebSettings webSettings = myFacebookWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
dismissProgressDialog();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
dismissProgressDialog();
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
myFacebookWebView.loadUrl("https://www.google.com/");
return view;
}
private void dismissProgressDialog(){
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}