我的Activity中有多个标签。每个选项卡都会打开电子商务网站的特定URL。当我从第一个选项卡向购物车添加项目并将选项卡更改为第二个选项卡时,添加的项目不会在第二个选项卡中更新。我该怎么办?
在桌面浏览器中,如果打开2个标签(同一个电子商务网站),我从第一个标签向购物车添加一个项目,然后在第二个标签中自动添加项目。我想在android选项卡中实现相同的功能。请帮忙
FragmentWebView.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_web_view_categories, container, false);
progressBar = view.findViewById(R.id.progressBarWVCategories);
webView = view.findViewById(R.id.webViewCategoriesActivity);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress < 100 && progressBar.getVisibility() == ProgressBar.GONE) {
progressBar.setVisibility(ProgressBar.VISIBLE);
}
progressBar.setProgress(newProgress);
if (newProgress == 100) {
progressBar.setVisibility(ProgressBar.GONE);
//toolbar.setTitle(webView.getTitle().trim());
}
}
});
webView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP) {
if (webView.canGoBack())
handler.sendEmptyMessage(1);
else
handler.sendEmptyMessage(2);
return true;
}
return false;
}
});
if (webViewBundle == null) {
webView.loadUrl(url);
} else {
webView.restoreState(webViewBundle);
}
return view;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//view.loadUrl();
}
}
ActivityWebView.java
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view_categories);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
toolbar = (Toolbar) findViewById(R.id.toolbarWebViewCategories);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(WebViewCategories.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
Intent intent = getIntent();
String toolbarTitle = intent.getStringExtra("toolbarTitle");
String[] tabsName = intent.getStringArrayExtra("tabsName");
String[] tabsLink = intent.getStringArrayExtra("tabsLink");
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager, tabsName, tabsLink);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
getSupportActionBar().setTitle(toolbarTitle);
}
private void setupViewPager(ViewPager viewPager, String[] tabsName, String[] tabsLink) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
for (int i = 0; i < tabsName.length; i++) {
adapter.addFragment(new FragmentWebViewCategories(tabsLink[i]), tabsName[i]);
}
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = 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);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
答案 0 :(得分:0)
不幸的是,这2页不了解彼此。您需要在更改标签上手动重新加载WebView
。
reloadWebView
方法添加到FragmentWebView
,您可以致电webView.reload()
或webView.loadUrl(url)
; OnPageChangeListener
。setupViewPager
添加到您的ViewPager
在页面监听器内部执行
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
FragmentWebView selectedFragment =
viewPager.getAdapter(). getCurrentFragment();
selectedFragment.reloadWebView();
}