0

면책 조항 : 이것은 처음으로 조각을 사용하는 어려운 방법을 배우기 위해 작성한 첫 번째 앱이므로 내 코드가 여기 저기에 있습니다.메뉴 항목 내비게이션 창에서 클릭하여 조각을 호스팅하는 행위

로드가 잘되는 탐색보기 안에 메뉴 항목이 있지만 메뉴 항목 클릭이 작동하지 않습니다. 그것은 단지 나를 쳐다 보지 못한다. 조각을 표시하기 위해 의도를 사용하고 싶습니다. 그리고 나는 많은 다른 옵션을 변경하고 시도하는 것으로부터 길을 잃습니다.

NAV 서랍 & 메뉴 항목 XML :

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_pageone" 
    android:background="@drawable/rygg" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbarThumbVertical="@color/primary_dark_color" 
    tools:openDrawer="start" 
    tools:context="com.android.nohiccupsbeta.pageone"> 

    <FrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/fragmentContainer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <android.support.design.widget.NavigationView 
      android:id="@+id/navigation_header_container" 
      app:headerLayout="@layout/header" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      app:itemIconTint="@color/category_vodka" 
      app:itemTextColor="@color/primary_dark_color" 
      app:menu="@menu/drawermenu" 
      android:layout_gravity="start" > 

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

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

JAVA: 

public class pageone extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 
    DrawerLayout mDrawerLayout; 
    ActionBarDrawerToggle mToggle; 

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

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mToggle.onConfigurationChanged(newConfig); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu){ 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.drawermenu, menu); 
     return true; 
    } 
    /** 
    * 
    * @param item For the hamburger button 
    * @return 
    */ 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (mToggle.onOptionsItemSelected(item)){ 
      return true; 
     } 
     switch (item.getItemId()) { 
      case R.id.itemWhiskey: 
       Intent whiskeyIntent = new Intent(pageone.this, whiskeyActivity.class); 
       startActivity(whiskeyIntent); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    /** 
    * Make sure the drawer open and closes in sync with UI visual 
    * @param savedInstanceState 
    */ 
    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     mToggle.syncState(); 
    } 

    /** 
    * Function to make sure all the drawer open & closes properly 
    */ 
    public void setupDrawer() { 
     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_pageone); 
     mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_closed) { 
      @Override 
      public void onDrawerClosed(View closeView) { 
       Toast.makeText(pageone.this, "Happy You Learned", Toast.LENGTH_SHORT).show(); 
       super.onDrawerClosed(closeView); 
       invalidateOptionsMenu(); 
      } 
      @Override 
      public void onDrawerOpened(View openView) { 
       Toast.makeText(pageone.this, "Effects Of Alcohol", Toast.LENGTH_SHORT).show(); 
       super.onDrawerOpened(openView); 
       invalidateOptionsMenu(); 
      } 
     }; 
     mDrawerLayout.addDrawerListener(mToggle); 
    } 

    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     MenuItem itemWhiskey = (MenuItem) findViewById(R.id.itemWhiskey); 
     itemWhiskey.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 
      @Override 
      public boolean onMenuItemClick(MenuItem itemWhiskey) { 
       FragmentManager fm = getSupportFragmentManager(); 
       Fragment effectsFragment = fm.findFragmentById(R.id.frame_container2); 
       if (effectsFragment == null) { 
        effectsFragment = new WhiskeyFragment(); 
        fm.beginTransaction().add(R.id.frame_container2, effectsFragment).commit(); 
        getSupportActionBar().setTitle("Whiskey"); 
        itemWhiskey.setChecked(true); 
        mDrawerLayout.closeDrawers(); 
       } 
       return true; 
      } 
     }); 
     mDrawerLayout.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

XML FOR FRAGMENT: 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/frame_container2" 
    tools:context="com.android.nohiccupsbeta.WhiskeyFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:id="@+id/frag_whiskey_skin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="8dp" 
     android:textColor="#000000" 
     android:textSize="16sp" /> 

    <ImageButton 
     android:id="@+id/expand_collapse" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:background="@android:color/transparent" 
     android:src="@drawable/ic_expand_more" 
     android:padding="16dp"/> 

</FrameLayout> 

JAVA: 

public class WhiskeyFragment extends Fragment { 

    private TextView mWhiskeySkin; 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle SavedInstanceState) { 
     super.onViewCreated(view, SavedInstanceState); 
     getActivity().setTitle("WHISKEY EFFECTS"); 

     mWhiskeySkin = (TextView) view.findViewById(R.id.frag_whiskey_skin); 
     mWhiskeySkin.setText(R.string.whiskey_skin); 
     hasOptionsMenu(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.fragment_whiskey, container, false); 
     hasOptionsMenu(); 
     return v; 
    } 
} 

XML FOR SECOND ACTIVITY: 

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/frame_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

JAVA: 

public class whiskeyActivity extends AppCompatActivity { 

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

    public class Whiskeyed { 

     private String whiskeySkin; 
     private String whiskeyBrain; 

     public String getWhiskeySkin(){ 
      return whiskeySkin; 
     } 
     public String getWhikeyBrain(){ 
      return whiskeyBrain; 
     } 
     public void setWhiskeySkin(String whiskey_skin){ 
      this.whiskeySkin = whiskey_skin; 
     } 

     public void setWhiskeyBrain(String whiskeyBrain) { 
      this.whiskeyBrain = whiskeyBrain; 
     } 
    } 

} 
+0

처음으로 탐색 창을 사용하는 경우 Android 개발자 기본 탐색 활동을 쉽고 이해하기 쉽도록 통합 해보십시오. 다른 사람이이 코드를 시도하면 도움이 될 것입니다. –

+0

예 차라리이 코드를 고치려고합니다. 힘든 방법입니다. 고맙습니다. 당신의 도움에 감사드립니다. – 7kevin

답변

0

시도는 여기에서 pageone.java

당신의 onNavigationItemSelected의 내용에 변경 :

@Override 
public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
    MenuItem itemWhiskey = (MenuItem) findViewById(R.id.itemWhiskey); 
    itemWhiskey.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 
     @Override 
     public boolean onMenuItemClick(MenuItem itemWhiskey) { 
      FragmentManager fm = getSupportFragmentManager(); 
      Fragment effectsFragment = fm.findFragmentById(R.id.frame_container2); 
      if (effectsFragment == null) { 
       effectsFragment = new WhiskeyFragment(); 
       fm.beginTransaction().add(R.id.frame_container2, effectsFragment).commit(); 
       getSupportActionBar().setTitle("Whiskey"); 
       itemWhiskey.setChecked(true); 
       mDrawerLayout.closeDrawers(); 
      } 
      return true; 
     } 
    }); 
    mDrawerLayout.closeDrawer(GravityCompat.START); 
    return true; 
} 

여기에 :

@Override 
public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_pageone); // ID of your drawerLayout 
    int id = item.getItemId(); 

    switch (id) { 
     case R.id.menu1: // Change this as your menuitem in menu.xml. 
      // Your fragment code goes here.. 
      FragmentManager fm = getSupportFragmentManager(); 
      Fragment effectsFragment = fm.findFragmentById(R.id.frame_container2); 
      if (effectsFragment == null) { 
       effectsFragment = new WhiskeyFragment(); 
       fm.beginTransaction().add(R.id.frame_container2, effectsFragment).commit(); 
       getSupportActionBar().setTitle("Whiskey"); 
       itemWhiskey.setChecked(true); 
       mDrawerLayout.closeDrawers(); 
      } 
     break; 
     case R.id.menu2: // Change this as your menuitem in menu.xml. 
      // or Your fragment code goes here... 
     break; 
    } 
    mDrawerLayout.closeDrawer(GravityCompat.START, true); 
    return true; 
} 
+0

진행해 주셔서 감사합니다. 이 시점에서 onCreateOptionsMenu (오른쪽 상단 corer 용)는 원하는 반응을 제공하는 버튼이지만 탐색 메뉴 항목은 아직 작동하지 않습니다. 조각 선언 후에는 시가에 대한 코드를 navigationItemSelected 함수에 넣으려고했지만 시가는 사용하지 않았습니다. – 7kevin

관련 문제