2016-06-29 5 views
0

안녕하세요 내 탐색 창에이 문제가 있습니다. 서랍이 열리지 만 onClickListener에 문제가 있습니다. 서랍에있는 항목을 클릭하면 관련 레이아웃이 표시되고 검은 페이지가 표시됩니다. 도와주세요내비게이션 서랍 조각 문제

home.java 

import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 
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.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.SubMenu; 
import android.view.View; 
import android.widget.Toast; 

public class home extends AppCompatActivity { 

    //Defining Variables 
    private Toolbar toolbar; 
    private NavigationView navigationView; 
    private DrawerLayout drawerLayout; 

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

     // Initializing Toolbar and setting it as the actionbar 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     //Initializing NavigationView 
     navigationView = (NavigationView) findViewById(R.id.navigation_view); 

     //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu 
     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 

      // This method will trigger on item Click of navigation menu 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 


       //Checking if the item is in checked state or not, if not make it in checked state 
       if(menuItem.isChecked()) menuItem.setChecked(false); 
       else menuItem.setChecked(true); 

       //Closing drawer on item click 
       drawerLayout.closeDrawers(); 

       //Check to see which item was being clicked and perform appropriate action 
       switch (menuItem.getItemId()){ 


        //Replacing the main content with ContentFragment Which is our Inbox View; 
        case R.id.dashboard: 
         Toast.makeText(getApplicationContext(),"dashboard",Toast.LENGTH_SHORT).show(); 
         Dashboard fragment = new Dashboard(); 
         android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
         fragmentTransaction.replace(R.id.frame,fragment); 
         fragmentTransaction.commit(); 
         break; 

        // For rest of the options we just show a toast on click 

        case R.id.profile: 
         Toast.makeText(getApplicationContext(),"profile",Toast.LENGTH_SHORT).show(); 
         new Profile(); 
         break; 
        case R.id.logs: 
         Toast.makeText(getApplicationContext(),"logs",Toast.LENGTH_SHORT).show(); 
         new Logs(); 
         break; 
        case R.id.statements: 
         Toast.makeText(getApplicationContext(),"statements",Toast.LENGTH_SHORT).show(); 
         new Statements(); 
         break; 
        case R.id.timeline: 
         Toast.makeText(getApplicationContext(),"timeliine",Toast.LENGTH_SHORT).show(); 
         new Timeline(); 
         break; 
        case R.id.settings: 
         Toast.makeText(getApplicationContext(),"settings",Toast.LENGTH_SHORT).show(); 
         new Settings(); 
         break; 

        default: 
         Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show(); 
         break; 

       } 
      return true; } 
     }); 

     // Initializing Drawer Layout and ActionBarToggle 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
     ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){ 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank 
       super.onDrawerClosed(drawerView); 
      } 

      @Override 
      public void onDrawerOpened(View drawerView) { 
       // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank 

       super.onDrawerOpened(drawerView); 
      } 
     }; 

     //Setting the actionbarToggle to drawer layout 
     drawerLayout.setDrawerListener(actionBarDrawerToggle); 

     //calling sync state is necessay or else your hamburger icon wont show up 
     actionBarDrawerToggle.syncState(); 






    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.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); 
    } 
} 

답변

0

case 문에서는 조각을 올바르게 전환하지 않습니다. 당신은 새로운 Fragment 클래스를 인스턴스화 만하고 그것으로 아무것도하지 않습니다. 당신이 뭔가를 할 필요가 :

Fragment fragment; 
switch (menuItem.getItemId()){ 


       //Replacing the main content with ContentFragment Which is our Inbox View; 
       case R.id.dashboard: 
        Toast.makeText(getApplicationContext(),"dashboard",Toast.LENGTH_SHORT).show(); 
        fragment = new Dashboard(); 
        break; 

       // For rest of the options we just show a toast on click 

       case R.id.profile: 
        Toast.makeText(getApplicationContext(),"profile",Toast.LENGTH_SHORT).show(); 
        fragment = new Profile(); 
        break; 
       case R.id.logs: 
        Toast.makeText(getApplicationContext(),"logs",Toast.LENGTH_SHORT).show(); 
        fragment = new Logs(); 
        break; 
       case R.id.statements: 
        Toast.makeText(getApplicationContext(),"statements",Toast.LENGTH_SHORT).show(); 
        fragment = new Statements(); 
        break; 
       case R.id.timeline: 
        Toast.makeText(getApplicationContext(),"timeliine",Toast.LENGTH_SHORT).show(); 
        fragment = new Timeline(); 
        break; 
       case R.id.settings: 
        Toast.makeText(getApplicationContext(),"settings",Toast.LENGTH_SHORT).show(); 
        fragment = new Settings(); 
        break; 

       default: 
        Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show(); 
        break; 

      } 
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
        fragmentTransaction.replace(R.id.frame,fragment); 
        fragmentTransaction.commit(); 

이 방법은 특정 단편 프레임을 교체, 적절한 조각의 클래스마다 조각을 인스턴스화하고 말 것입니다.

Digresion : 생성자가 아닌 조각을 얻으려면 newInstance을 사용하는 것이 좋습니다. 이를 Singleton 디자인 패턴이라고합니다.

+0

이제는 정상적으로 작동합니다. 또한 항목을 선택했을 때 색상을 변경하고 싶다면 (예 : 선택) 색상 오버레이처럼 –

+0

친구와 문제 없습니다. 당신은 그것을하는 방법을 찾아 낼 수 있습니다 또는 [이 질문을] 참조 (http://stackoverflow.com/questions/22374660/navigation-drawer-item-background-colour-for-selected-item) – Vucko