Android Drawer导航菜单项单击不起作用 [英] Android Drawer navigation menu item click not working

查看:80
本文介绍了Android Drawer导航菜单项单击不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

菜单项单击不起作用我尝试了各种操作,但不起作用.

Menu Item click not working I tried various things but it won't work.

这是我的代码:

家庭活动:

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>

当我尝试将activity_home.xml作为设计查看时,它不会显示左抽屉,但是当我在设备上安装该应用程序时,它将在单击时显示左抽屉,但是当我单击任何时候时,它都不会显示复选框而不起作用.

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.

推荐答案

更新这些行对我有用.更新更新并重建Project后.

Update These lines of code works for me.. after update clean and rebuild Project.

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

这篇关于Android Drawer导航菜单项单击不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆