简体   繁体   English

Android Drawer导航菜单项单击不起作用

[英]Android Drawer navigation menu item click not working

Menu Item click not working I tried various things but it won't work. 菜单项单击不起作用我尝试了多种操作,但不起作用。

Here are my code: 这是我的代码:

Home Activity: 家庭活动:

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
    private Button mBtRequest;
    private Button mBtContinue;
    private ProfileFragment mProfileFragment;
    private SetPreferences Setpref;
    private SlideUp slideUp;
    private View dim;
    private View sliderView;
    Toast toast;

    private TextView mTvDrawerName;
    private TextView mTvDrawerMobile;
    private TextView mtoolbarTitle;
    private String mfullName;
    private String mMobile;
    private SharedPreferences mSharedPreferences;
    private CompositeSubscription mSubscriptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Checking for first time launch - before calling setContentView()
        Setpref = new SetPreferences(this);
        if (!Setpref.IsLoggined()) {
            gotoMain();
            finish();
        }

        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        mSubscriptions = new CompositeSubscription();
        initSharedPreferences();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        toolbar.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        mBtRequest = (Button) findViewById(R.id.btn_request);
        mBtContinue = (Button) findViewById(R.id.btn_continue);
        mBtRequest.setOnClickListener(view -> slideUp());
        mBtContinue.setOnClickListener(view -> checkout());


        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        sliderView = findViewById(R.id.slideView);
        sliderView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (toast != null){
                    toast.cancel();
                }
                toast = Toast.makeText(HomeActivity.this, "click", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        dim = findViewById(R.id.dim);
        slideUp = new SlideUpBuilder(sliderView)
                .withListeners(new SlideUp.Listener.Events() {
                    @Override
                    public void onSlide(float percent) {
                        dim.setAlpha(1 - (percent / 100));
                    }

                    @Override
                    public void onVisibilityChanged(int visibility) {
                        if (visibility == View.GONE){
                           // fab.show();
                        }
                    }
                })
                .withStartGravity(Gravity.BOTTOM)
                .withLoggingEnabled(true)
                .withGesturesEnabled(true)
                .withStartState(SlideUp.State.HIDDEN)
                .build();

        Typeface myraid_bold = Typeface.createFromAsset(getAssets(),"fonts/MyriadPro-Bold_0.otf");
        Typeface myraid_semibold = Typeface.createFromAsset(getAssets(),"fonts/MyriadPro-Semibold_0.otf");

        View header = navigationView.getHeaderView(0);
        mTvDrawerName = (TextView) header.findViewById(R.id.nav_user_name);
        mTvDrawerMobile = (TextView) header.findViewById(R.id.nav_mobile_number);
        mtoolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        mTvDrawerName.setText(mfullName);
        mTvDrawerMobile.setText(mMobile);

        mTvDrawerName.setTypeface(myraid_bold);
        mTvDrawerMobile.setTypeface(myraid_bold);
        mtoolbarTitle.setTypeface(myraid_semibold);
        mBtRequest.setTypeface(myraid_bold);
        mBtContinue.setTypeface(myraid_bold);
    }


    private void initSharedPreferences() {
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        mfullName = mSharedPreferences.getString(Constants.FULLNAME,"");
        mMobile = mSharedPreferences.getString(Constants.MOBILE,"");
    }


    private void gotoProfile(){
        if (mProfileFragment == null) {
            mProfileFragment = new ProfileFragment();
        }
        getFragmentManager().beginTransaction().replace(R.id.fragmentFrame,mProfileFragment,ProfileFragment.TAG).commit();
    }

    private void gotoMain(){
        Setpref.setIsLoggined(false);
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);
    }

    private void slideUp(){
        slideUp.show();
    }

    private void checkout(){

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void logout() {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(Constants.MOBILE,"");
        editor.putString(Constants.TOKEN,"");
        editor.putString(Constants.FULLNAME,"");
        editor.apply();
        gotoMain();
    }

    private void showSnackBarMessage(String message) {
        Snackbar.make(findViewById(R.id.drawer_layout),message,Snackbar.LENGTH_SHORT).show();
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_my_addresses) {
            // Handle the camera action
        } else if (id == R.id.nav_my_orders) {

        } else if (id == R.id.nav_refer) {

        }  else if (id == R.id.nav_logout) {
           logout();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

activity_home_drawer.xml activity_home_drawer.xml

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

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_my_addresses"
            android:icon="@drawable/ic_location_on_white_24px"
            android:title="My Addresses" />
        <item
            android:id="@+id/nav_my_orders"
            android:icon="@drawable/ic_history_white_24px"
            android:title="My Orders" />
        <item
            android:id="@+id/nav_refer"
            android:icon="@drawable/ic_record_voice_over_white_24px"
            android:title="Refer &amp; Earn" />
    </group>

    <group
        android:id="@+id/menu_bottom"
        android:checkableBehavior="none">

        <item
            android:id="@+id/nav_logout"
            android:icon="@drawable/ic_exit_to_app_white_24px"
            android:title="Logout" />
    </group>
</menu>

nav_header_main.xml nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Aashish Kaushik"
        android:id="@+id/nav_user_name"
        android:textColor="@android:color/white"/>

    <TextView
        android:id="@+id/nav_mobile_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9910454530"
        android:textColor="@android:color/white"/>

</LinearLayout>

activity_home.xml activity_home.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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:alpha="0"
        android:id="@+id/dim"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/dimBg" />

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_home_drawer" />

    <!-- The Fragment -->
    <FrameLayout
        android:id="@+id/fragmentFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

When I try to view activity_home.xml as a design it will not showing left drawer but when I install the app on device it will show the left drawer on click but when I click on any time it will not showing check able box and not working. 当我尝试将activity_home.xml作为设计查看时,它不会显示左抽屉,但是当我在设备上安装该应用程序时,它将在单击时显示左抽屉,但是当我单击任何时候时,它都不会显示复选框并且无法正常工作。

Update These lines of code works for me.. after update clean and rebuild Project. 更新这些代码行对我有用..更新干净并重建Project之后。

  NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.bringToFront();

Here is a simple solution to address this issue. 这是解决此问题的简单解决方案。 You can just link your HomeActivity methods to the menu items (whichever you want to). 您可以仅将HomeActivity方法链接到菜单项(无论您要选择什么)。 Now just create your methods and go to the menu layout. 现在,只需创建您的方法并转到菜单布局即可。 In the menu layout just add the android:onClick inside the item tag and put the particular method name inside it. 在菜单布局中,只需在item标签内添加android:onClick,然后在其中放入特定的方法名称即可。 And you are done. 您完成了。 Let me know if this was useful. 让我知道这是否有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM