在android片段中加载webview时隐藏导航抽屉和选项卡

时间:2017-02-14 07:45:48

标签: android android-navigation-drawer

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    // Remove the below line after defining your own ad unit ID.
   /* private static final String TOAST_TEXT = "Test ads are being shown. "
            + "To show live ads, replace the ad unit ID in res/values/strings.xml with your own ad unit ID.";*/

    boolean doubleBackToExitPressedOnce = false;
    //set viewpager adapter
    ViewPagerAdapter pagerAdapter;
    private ViewPager viewPager;
    private DrawerLayout drawer;
    private TabLayout tabLayout;
    private String[] pageTitle = {"Home","Movies","Songs", "Comedy"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);


        // Load an ad into the AdMob banner view.
        AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        adView.loadAd(adRequest);

        // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
       // Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
        setSupportActionBar(toolbar);
        //create default navigation drawer toggle
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        //setting Tab layout (number of Tabs = number of ViewPager pages)
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        for (int i = 0; i < 4; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(pageTitle[i]));
        }

        //set gravity for tab bar
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        //handling navigation view item event
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        assert navigationView != null;
        navigationView.setNavigationItemSelectedListener(this);

        //set viewpager adapter
        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        //change Tab selection when swipe ViewPager
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));


        //change ViewPager page when tab selected

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {


            @Override

            public void onTabSelected(TabLayout.Tab tab) {

                viewPager.setCurrentItem(tab.getPosition());

            }


            @Override

            public void onTabUnselected(TabLayout.Tab tab) {


            }


            @Override

            public void onTabReselected(TabLayout.Tab tab) {


            }

        });

    }






    @Override

    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        int id = item.getItemId();


        switch (id) {

            case R.id.movies:
                viewPager.setCurrentItem(0);

                break;

            case R.id.comedy:
                viewPager.setCurrentItem(1);

                break;

            case R.id.songs:
                viewPager.setCurrentItem(2);

                break;

            case R.id.animation:
                Intent in = new Intent(this, Animation.class);

                // intent.putExtra("string", "");

                startActivity(in);
                break;

            case R.id.albums:
                Intent intent = new Intent(this, DesActivity.class);

                // intent.putExtra("string", "");

                startActivity(intent);

                break;

        }


        drawer.closeDrawer(GravityCompat.START);

        return true;

    }


    @Override

    public void onBackPressed() {

        int position = viewPager.getCurrentItem();

        Fragment fragment = pagerAdapter.getItem(position);

        if (fragment != null) {

            switch (position) {

                case 0:
                    if (((Home) fragment).hasBackPage()) return;

                    break;

                case 1:
                    if (((MoviesActivity) fragment).hasBackPage()) return;

                    break;
                case 2:
                    if (((SongsActivity) fragment).hasBackPage()) return;

                    break;
                case 3:
                    if (((ComedyActivity) fragment).hasBackPage()) return;

                    break;

            }


            if (position == 0) {

                if (doubleBackToExitPressedOnce) {

                    super.onBackPressed();

                    finish();

                    return;

                }

                this.doubleBackToExitPressedOnce = true;

                Toast.makeText(this, "Touch again to Exit", Toast.LENGTH_SHORT).show();


                new Handler().postDelayed(new Runnable() {


                    @Override

                    public void run() {

                        doubleBackToExitPressedOnce = false;

                    }

                }, 2000);

            } else {

                viewPager.setCurrentItem(0);

            }

        }

    }


}

1 个答案:

答案 0 :(得分:0)

最初隐藏TabLayoutNavigationDrawer,并使用以下代码显示何时加载WebView

mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                showViews();
            }
        });