我正在制作网络浏览器。我已经使用片段进行制表,但是当用户从一个制表符转到另一个制表符并再次返回时,webview的url会重置并且用户的url会丢失。 我的问题是;片段切换时如何停止webview重置。 我研究了左右和中心试图解决这个问题,但没有运气。 谢谢!
public class HomeFragment extends Fragment {
public HomeFragment(){}
private WebView WebViewB1;
private ProgressBar Pbar1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Pbar1 = (ProgressBar) rootView.findViewById(R.id.progBar1);
WebViewB1 = (WebView) rootView.findViewById(R.id.wvBrowserTab1);
WebSettings webSettings = WebViewB1.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewB1.getSettings().setLoadWithOverviewMode(true);
WebViewB1.getSettings().setUseWideViewPort(true);
WebViewB1.getSettings().setBuiltInZoomControls(true);
WebViewB1.getSettings().setAllowFileAccess(true);
WebViewB1.setWebViewClient(new InsideWebViewClient());
WebViewB1.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar1.getVisibility() == ProgressBar.GONE){
Pbar1.setVisibility(ProgressBar.VISIBLE);
}
Pbar1.setProgress(progress);
if(progress == 100) {
Pbar1.setVisibility(ProgressBar.GONE);
}
}
});
WebViewB1.loadUrl("http://google.com");
return rootView;
}
}
下面的滑动菜单活动
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer,
R.string.app_name,
R.string.app_name
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
displayView(0);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayView(position);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new Tab2();
break;
case 2:
fragment = new Tab3();
break;
case 3:
fragment = new Tab4();
break;
case 4:
fragment = new Tab5();
break;
case 5:
fragment = new Tab6();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
答案 0 :(得分:2)
好的,我不确定它的好主意,但以下是我所说的:
public abstract class AbstractFragment extends Fragment {
private View mFragmentView;
protected View getOnCreateView(LayoutInflater inflater, int layoutId){
if (mFragmentView== null)
mFragmentView = inflater.inflate(layoutId, null);
else
((ViewGroup) mFragmentView.getParent()).removeView(mFragmentView);
return mFragmentView;
}
// this method needed to detect if fragment's view init is needed
// via initView() - its the method where you do findViewById()
// and sets up your webveiw, etc. The idea is to set up the view
// only once - in case when it has not been init-ed
// this will prevent your webview from reload from the start url
protected boolean isFragmentViewInitialized;
@Override
public void onStart() {
super.onStart();
if (!isFragmentViewInitialized){
isFragmentViewInitialized = true;
initView();
}
}
// use this method to setup the view
public abstract void initView();
我们的想法是根据您需要保留的每个片段扩展此片段,并从getOnCreateView
调用onCreate()
,如:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return getOnCreateView(inflater,R.layout.activity_about);
}
public void initView(){
View rootView = getView();
Pbar1 = (ProgressBar) rootView.findViewById(R.id.progBar1);
WebViewB1 = (WebView) rootView.findViewById(R.id.wvBrowserTab1);
WebSettings webSettings = WebViewB1.getSettings();
webSettings.setJavaScriptEnabled(true);
WebViewB1.getSettings().setLoadWithOverviewMode(true);
WebViewB1.getSettings().setUseWideViewPort(true);
WebViewB1.getSettings().setBuiltInZoomControls(true);
WebViewB1.getSettings().setAllowFileAccess(true);
WebViewB1.setWebViewClient(new InsideWebViewClient());
WebViewB1.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
if(progress < 100 && Pbar1.getVisibility() == ProgressBar.GONE){
Pbar1.setVisibility(ProgressBar.VISIBLE);
}
Pbar1.setProgress(progress);
if(progress == 100) {
Pbar1.setVisibility(ProgressBar.GONE);
}
}
});
WebViewB1.loadUrl("http://google.com");
}
答案 1 :(得分:1)
由于我的标签监听器
,每次重新选择时,片段都会重新加载if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
做了一个更好的tabListener之后的东西,A-OK。查看此链接,了解标签监听器的改进方式 - http://developer.android.com/guide/topics/ui/actionbar.html#Tabs