更改选项卡不会更新Android中的Webview状态

时间:2018-03-12 16:08:39

标签: android android-webview android-websettings

我的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);
        }
    }

1 个答案:

答案 0 :(得分:0)

不幸的是,这2页不了解彼此。您需要在更改标签上手动重新加载WebView

  1. reloadWebView方法添加到FragmentWebView,您可以致电webView.reload()webView.loadUrl(url);
  2. 保存适配器中当前片段的链接,类似于https://stackoverflow.com/a/21104114/1735198
  3. OnPageChangeListener
  4. 中将setupViewPager添加到您的ViewPager
  5. 在页面监听器内部执行

    @Override
    public void onPageSelected(int position) {
          super.onPageSelected(position);
    
          FragmentWebView selectedFragment =
                         viewPager.getAdapter(). getCurrentFragment();
          selectedFragment.reloadWebView();     
    }