2017-12-03 1 views
0

저는 Android에 익숙하지 않아 액티비티로만 앱을 개발했습니다. 이제이를 변경하고 조각이있는 탐색 서랍을 만들었습니다.탐색 드로어에 조각을 소개하십시오.

문제는 활동을 조각으로 변경하는 방법을 모른다는 것입니다. 내가 봤어 무엇

:

<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" 
    tools:context="games.asee.videogames_asee_project.BlankFragment"> 

    <ListView 
     android:id="@+id/listTOP" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/background_light" /> 

</FrameLayout> 
: 모든

첫째, 나는 BlankFragment을 만들고 나는 조각이 순간에 BlankFragment라고 내 활동에 적응이 레이아웃입니다

이 조각의 클래스 (난 단지 onCreateView의 코드를 수정)입니다 :

@Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      JuegosOperaciones crud = JuegosOperaciones.getInstance(getContext()); 
      items = crud.getTOP(); 
      adapter = new JuegoAdapter(getContext(), items); 
      ListView itemsListView = (ListView) getView().findViewById(R.id.listTOP); 
      itemsListView.setAdapter(adapter); 
      // Inflate the layout for this fragment 
      return inflater.inflate(R.layout.fragment_blank, container, false); 

     } 

그러나 navigationDrawer에서 프래그먼트를 호출하는 방법을 모르겠다. getFragmentManager을 호출하려고 시도했지만 앱이 아무 것도하지 않는다!

} else if (id == R.id.nav_manage) { 
      // getFragmentManager().beginTransaction().add(R.id.listTOP,new BlankFragment()).commit(); 
     } 

자세한 코드가 필요하면 알려주십시오.

다시 한 번 감사드립니다.

+0

초보자는 https://guides.codepath.com/android/fragment-navigation-drawer를 사용해보십시오. – fida1989

답변

0

먼저 주요 활동에서 탐색 서랍 항목을 초기화하고 설정하십시오. 탐색 항목에 클릭을 사용하여 MainActivity.class를이 코드

FragmentManager manager = getSupportFragmentManager(); 
     FragmentTransaction fTransaction = manager.beginTransaction(); 
     fTransaction.replace(R.id.frameForPane, my_fragment); 
     fTransaction.commit(); 
     mainDrawer.closeDrawers(); 

를 사용하여 프레임 레이아웃 에 조각을 교체하고 설정할 때 후이 :

public class MainActivity extends AppCompatActivity { 



    // ... 



    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

     // ...From section above... 

     // Find our drawer view 

     nvDrawer = (NavigationView) findViewById(R.id.nvView); 

     // Setup drawer view 

     setupDrawerContent(nvDrawer); 

    } 



    private void setupDrawerContent(NavigationView navigationView) { 

     navigationView.setNavigationItemSelectedListener(

       new NavigationView.OnNavigationItemSelectedListener() { 

        @Override 

        public boolean onNavigationItemSelected(MenuItem menuItem) { 

         selectDrawerItem(menuItem); 

         return true; 

        } 

       }); 

    } 



    public void selectDrawerItem(MenuItem menuItem) { 

     // Create a new fragment and specify the fragment to show based on nav item clicked 

     Fragment fragment = null; 

     Class fragmentClass; 

     switch(menuItem.getItemId()) { 

      case R.id.nav_first_fragment: 

       fragmentClass = FirstFragment.class; 

       break; 

      case R.id.nav_second_fragment: 

       fragmentClass = SecondFragment.class; 

       break; 

      case R.id.nav_third_fragment: 

       fragmentClass = ThirdFragment.class; 

       break; 

      default: 

       fragmentClass = FirstFragment.class; 

     } 



     try { 

      fragment = (Fragment) fragmentClass.newInstance(); 

     } catch (Exception e) { 

      e.printStackTrace(); 

     } 



     // Insert the fragment by replacing any existing fragment 

     FragmentManager fragmentManager = getSupportFragmentManager(); 

     fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit(); 



     // Highlight the selected item has been done by NavigationView 

     menuItem.setChecked(true); 

     // Set action bar title 

     setTitle(menuItem.getTitle()); 

     // Close the navigation drawer 

     mDrawer.closeDrawers(); 

    } 



    // ... 

} 

이 열기를 다음 조각을 통합하는 데 도움이 될 것이다 탐색 항목을 클릭하십시오.

관련 문제