2017-08-07 127 views
0

菜單項點擊不起作用我嘗試了各種事情,但它不會工作。安卓抽屜導航菜單項點擊不起作用

這裏是我的代碼:

首頁活動:

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

<?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

<?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

<?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作爲設計它不會顯示左邊抽屜,但是當我在設備上安裝應用程序,它會顯示在點擊左邊的抽屜裏,但是當我點擊任何時候也不會顯示檢查框和不工作。

回答

0

以下是解決此問題的簡單解決方案。您可以將您的HomeActivity方法鏈接到菜單項(無論您希望如何)。現在只需創建你的方法並進入菜單佈局。在菜單佈局中,只需在item標籤內添加android:onClick,並將特定的方法名稱放入其中。你完成了。讓我知道這是否有用。