我有一个加载片段的活动,在该片段内部是一个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;
}
}
我的活动:
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;
}
}
}
答案 0 :(得分:1)
您可以覆盖WebViewClient的onPageFinished(WebView视图,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
}
}
);
答案 1 :(得分:1)
你可以关闭onPageFinished事件(成功时)或类MyWebViewClient的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的时间加载时间过长(但仍然成功),您应该检查结束时间。
答案 2 :(得分:0)
您可以像这样显示和隐藏进度对话框
{{1}}