0
我需要在進度條/工具欄中放置進度條。我可以通過以下鏈接進行操作(http://www.michenux.net/android-refresh-item-action-bar-circular-progressbar-578.html)。 但是我面臨的問題是爲了顯示和隱藏進度條,遵循我在此使用的回調設計模式。下面是代碼片段 -當它作爲回調函數被調用時,菜單對象將變爲空
public interface ICallBack {
void setRefreshActionButtonState(boolean b);
}
public class MyFragment extends Fragment implements View.OnClickListener, ICallBack {
private Menu optionsMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
webView = (WebView) findViewById(R.id.main_web);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://stackoverflow.com/");
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
this.optionsMenu = menu;
getActivity().getMenuInflater().inflate(R.menu.menu_home_base, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
// Logic to load the webview again
}
return super.onOptionsItemSelected(item);
}
@Override
public void setRefreshActionButtonState(boolean refreshing) {
if (optionsMenu != null) {
final MenuItem refreshItem = optionsMenu.findItem(R.id.action_refresh);
if (refreshItem != null) {
if (refreshing) {
refreshItem.setActionView(R.layout.toolbar_indeterminate_progress);
} else {
refreshItem.setActionView(null);
}
}
}
}
}
public class MyWebViewClient extends WebViewClient {
ICallBack callBack;
public MyWebViewClient(Activity context) {
callBack = new MyFragment();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
callBack.setRefreshActionButtonState(true);//Showing the progressbar
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
callBack.setRefreshActionButtonState(false);//Hiding the progressbar
}
}
當setRefreshActionButtonState()得到調用,然後我得到optionsMenu值空,導致無法顯示和隱藏進度。
有人可以幫助我弄清楚,在那裏我做錯了。
感謝,
在創建視圖之前,您無法調用getactivity()。 –
嗨Rushi,謝謝你的回覆。我相信onCreateOptionsMenu()下的getActivity()在onCreateView()之後得到調用。請讓我知道,如果我錯了。 – Ajay