导航抽屉图标未显示且OnNavigationItemSelectedListener无法单击

时间:2018-05-20 06:13:37

标签: android android-fragments tabs navigation-drawer

我在我的启动器活动中使用带有标签片段的底部导航。 现在我想使用导航抽屉。导航抽屉工作但没有图标,并且在onclick

时无响应

我的LauncherActivity包含BottomNavigationView,BottomNavigationView包含片段。我在LauncherActivity上添加了Navigation Drawer的代码。但是没有图标显示。导航抽屉适用于滑动位onclick事件不适用于导航抽屉

所有代码都在这里。请帮忙

LauncherActivity.java

package com.cornicore.dream11statistics;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class LauncherActivity extends AppCompatActivity implements Tab1.OnFragmentInteractionListener,Tab2.OnFragmentInteractionListener,Tab3.OnFragmentInteractionListener{

    private InterstitialAd interstitialAd;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;

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


        drawerLayout = findViewById(R.id.nav_drawer_home);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(true);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.navigationViewHome);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                int id = item.getItemId();

                if (id == R.id.nav_home_news){
                    Toast.makeText(LauncherActivity.this,"News",Toast.LENGTH_SHORT).show();
                }

                if (id == R.id.nav_home_schedule){
                    Toast.makeText(LauncherActivity.this,"Schedule",Toast.LENGTH_SHORT).show();
                }

                if (id == R.id.nav_home_upcoming_series){
                    Toast.makeText(LauncherActivity.this,"Series",Toast.LENGTH_SHORT).show();
                }

                if (id == R.id.nav_home_ranking){
                    Toast.makeText(LauncherActivity.this,"Ranking",Toast.LENGTH_SHORT).show();
                }

                if (id == R.id.nav_home_point_table){

                    Intent intent = new Intent(LauncherActivity.this,KKRActivity.class);
                    startActivity(intent);

                }

                return false;
            }
        });


//Ads

        prepareAd();

        ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
        schedule.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (interstitialAd.isLoaded()){
                            interstitialAd.show();
                        }

                        prepareAd();
                    }
                });
            }
        },3,300, TimeUnit.SECONDS);

//Ads


// Bottom Navigation Start

                BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListner);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new HomeFragment()).commit();

// Bottom Navigation

    }


// Bottom Navigation Start
    private BottomNavigationView.OnNavigationItemSelectedListener navListner = new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    android.support.v4.app.Fragment selectedFragment = null;
                    switch(item.getItemId()){
                        case R.id.nav_home:
                            selectedFragment = new HomeFragment();
                            break;

                        case R.id.nav_pointtable:
                            selectedFragment = new PointTableFragment();
                            break;

                        case R.id.nav_tricks:
                            selectedFragment = new TricksFragment();
                            break;
                    }


                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
                    return true;
                }
            };

    @Override
    public void OnNavFragmentInteractionListener(Uri uri) {

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }




    public void prepareAd(){

        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId("ca-app-pub-2696458474841326/2346748005");
        interstitialAd.loadAd(new AdRequest.Builder().build());
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.menuShare){

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "Hey,Install this App and Play Dream 11 with proper statistics...Rate and Share https://play.google.com/store/apps/details?id=com.cornicore.dream11statistics";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, " ");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));

        }

        if (id == R.id.menuRateUs){

    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.cornicore.dream11statistics")));


        }

        if (id == R.id.menuFeedback){

            Intent intent = new Intent(this,FeedbackActivity.class);
            startActivity(intent);

        }

        actionBarDrawerToggle.onOptionsItemSelected(item);
        return true;
    }
}

activity_launcher.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/nav_drawer_home"
    tools:context=".LauncherActivity">

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationViewHome"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/navigation_menu_home"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">


    </android.support.design.widget.NavigationView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation" />


        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_alignParentBottom="true"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/bottom_navigation"
            tools:layout_editor_absoluteX="0dp">


        </android.support.design.widget.BottomNavigationView>


    </RelativeLayout>


</android.support.v4.widget.DrawerLayout>

navigation_menu_home.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav_home_news"
        android:title="News"
        android:icon="@drawable/ic_view_list_black_24dp" />

    <item
        android:id="@+id/nav_home_schedule"
        android:title="Schedule"
        android:icon="@drawable/ic_view_list_black_24dp" />

    <item
        android:id="@+id/nav_home_upcoming_series"
        android:title="Upcoming Series"
        android:icon="@drawable/ic_view_list_black_24dp" />

    <item
        android:id="@+id/nav_home_ranking"
        android:title="Ranking"
        android:icon="@drawable/ic_view_list_black_24dp" />

    <item
        android:id="@+id/nav_home_point_table"
        android:title="Point Table"
        android:icon="@drawable/ic_view_list_black_24dp" />

</menu>

navigation_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@drawable/share_background_gradient">

    <ImageView
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:background="@drawable/ic_logo" />


</LinearLayout>

0 个答案:

没有答案