即使在Android应用中单击抽屉链接,底部导航链接仍保持活动状态

时间:2019-04-20 21:00:01

标签: android mobile webview

非常奇怪的行为。当我单击底部的导航按钮,然后单击抽屉按钮时,反之亦然,上一个按钮保持突出显示状态。有没有做对的事情?我的代码如下;

public class MainActivity extends AppCompatActivity {

        private ActionBar actionBar;
        private Toolbar toolbar;
        private BottomNavigationView navigation;
        private String postUrl = "url";
        private WebView webView;
        public static ImageView share;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            initToolbar();
            initNavigationMenu();
            initComponent();

            webView = (WebView) findViewById(R.id.webView);
            webView.setWebViewClient(new MyWebViewClient());
            webView.getSettings().setJavaScriptEnabled(true);
            //request Focus on down.
            webView.requestFocus(View.FOCUS_DOWN);
            //set OnTouchListener to your webView.
            webView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                        case MotionEvent.ACTION_UP:
                            if (!v.hasFocus()) {
                                v.requestFocus();
                            }
                            break;
                    }
                    return false;
                }
            });

            progress_bar = (ProgressBar) findViewById(R.id.progress_bar);
            webView.setHorizontalScrollBarEnabled(false);
            webView.clearCache(true);
            webView.clearHistory();
            share = (ImageView) findViewById(R.id.action_share);

            webView.loadUrl(postUrl);
        }

        private void initToolbar() {
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setTitle("App Name");
            // Tools.setSystemBarColor(this);
        }

        private void initComponent() {
            navigation = (BottomNavigationView) findViewById(R.id.navigation);
            navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.navigation_favorites:
                            actionBar.setTitle(item.getTitle());
                            webView.loadUrl("url");
                            return true;
                        case R.id.navigation_nearby:
                            actionBar.setTitle(item.getTitle());
                            webView.loadUrl("url");
                            return true;
                    }
                    return false;
                }
            });

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_search_setting, menu);
            return true;
        }

       private void initNavigationMenu() {
            NavigationView nav_view = (NavigationView) findViewById(R.id.nav_view);
            final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                }
            };
            drawer.setDrawerListener(toggle);
            toggle.syncState();
            nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(final MenuItem item) {

                    switch (item.getItemId()) {
                        case R.id.nav_starred:
                            actionBar.setTitle(item.getTitle());
                            webView.loadUrl("url");
                            break;
                        case R.id.nav_setting:
                            actionBar.setTitle(item.getTitle());
                            webView.loadUrl("url");
                            break;
                    }

                    drawer.closeDrawers();
                    return true;
                }
            });

            // open drawer at start
            // drawer.openDrawer(GravityCompat.START);
        }

        private class MyWebViewClient extends WebViewClient {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;

            }

        }

        @Override
        // This method is used to detect back button
        public void onBackPressed() {
            if(webView.canGoBack()) {
                webView.goBack();
            } else {
                // Let the system handle the back button
                super.onBackPressed();
            }
        }

}

Screenshot

欢迎任何帮助。我已经在寻找解决方案,但没有得到任何有用的信息。

0 个答案:

没有答案