Displaying progress bar for a webview in fragment view
我正在尝试在网页出现或加载之前显示进度条。我已经在 PlanetFragment 的 onCreateView 方法中声明了进度条,它扩展了片段我已经删除了网页的监听器。但是无论我写什么代码,进度条都不会出现。请帮忙
这是我的 MainActivity
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout; private CharSequence mDrawerTitle; @Override mTitle = mDrawerTitle = getTitle(); // set a custom shadow that overlays the main content when the drawer // enable ActionBar app icon to behave as action to toggle nav drawer // ActionBarDrawerToggle ties together the the proper interactions public void onDrawerOpened(View drawerView) { if (savedInstanceState == null) { @Override /* Called whenever we call invalidateOptionsMenu() */ @Override /* The click listner for ListView in the navigation drawer */ private void selectItem(int position) { FragmentManager fragmentManager = getFragmentManager(); // update selected item and title, then close the drawer @Override /** @Override @Override /** public PlanetFragment() { @Override int imageId = getResources().getIdentifier( final WebView webview = ((WebView) rootView final ProgressBar progressbar = ((ProgressBar) rootView webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() { webview.loadUrl(“javascript:document.getElementsByClassName(‘header’)[0].style.display=”none”;”); webview.getSettings().setJavaScriptEnabled(false); } webview.loadUrl(planet); |
}
这是我的 xml 布局:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent” tools:context=”.MainActivity”> <ProgressBar <WebView |
这是因为 ProgressBar 它在您的 xml 中”低于”WebView(这意味着在 xml 中声明的第一个小部件将是最深层次的小部件,小部件是堆叠的)。
尽量把ProgressBar放到xml下面,必要时隐藏,如下图:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent” tools:context=”.MainActivity”> <WebView <ProgressBar </RelativeLayout> |
目前 WebView 停留在您的 ProgressBar
前面
- 很高兴我能帮上忙。使用对你来说最自然的那个:) 干杯
这样的事情应该可以工作。我还没有测试过(现在没有编辑器)。解决方案是隐藏 webview 并在适当的时候显示它。
|
1
2 3 4 5 6 7 8 9 10 11 12 |
webview.setWebViewClient(new WebViewClient() {
@Override public void onPageFinished(WebView view, String url) { view.setVisibility(View.VISIBLE); //you might need this view.bringToFront(); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { view.setVisibility(View.GONE);//hide the webview that will display your dialog } }); |
- 谢谢,它奏效了。由于没有出现进度条,我没有使视图不可见。
- @PrashantYadav 很高兴为您提供帮助
来源:https://www.codenong.com/26193585/
