2014-01-11 32 views
3

我在我的android應用中實現了導航抽屜。但現在我希望能夠在用戶單擊導航欄中的任何列表項時使用片段更改佈局。 這是我迄今爲止得到:Android - 導航抽屜碎片

XML

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:background="#000000" 
     android:layout_height="match_parent" >  
    </FrameLayout> 

    <ListView android:id="@+id/left_drawer" 
     android:layout_width="220dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#111"/> 
</android.support.v4.widget.DrawerLayout> 

Java文件

public class MainActivity extends Activity { 
final String[] data ={"one","two","three"}; 

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

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); 

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout); 
    final ListView navList = (ListView) findViewById(R.id.left_drawer); 
    navList.setAdapter(adapter); 
    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){ 
      drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener(){ 
       @Override 
       public void onDrawerClosed(View drawerView){ 
        super.onDrawerClosed(drawerView); 

       } 
      }); 
      drawer.closeDrawer(navList); 
     } 
    }); 
    } 
} 

使用上面的代碼,我實現了導航抽屜在我的應用程序,我看導航抽屜中的「一個」,「兩個」和「三個」列表項目,但除了抽屜關閉之外,點擊它們時沒有任何反應。 所以,我的問題是: 如何將片段功能添加到上述給定的代碼?

我是初學者。提前致謝!

+0

你必須通過FragmentActivity來做到這一點。 – Kishan

+0

@ kishandhamat爲什麼是這樣? – Raghunandan

+0

這可能有助於http://www.gadgetsaint.com/android/android-navigation-drawer-toolbar/#。WOBABBJ97fY – ASP

回答

7

在點擊有

selectItem(pos); 

然後

public void selectItem(int position) 
{ 
    switch(position) 
    { 
      case 0: 
        // fragment1 
        // use fragment transaction and add the fragment to the container 
        FragmentManager fragmentManager = getFragmentManager() 
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
        Fragment1 fragment = new Fragment1(); 
        fragmentTransaction.add(R.id.content_frame, fragment); 
        fragmentTransaction.commit(); 

      break; 
      case 1: 
        // fragment2 
      break; 
      case 2: 
        // fragment2 
      break; 
    } 
} 
+0

你可以給代碼而不是「//片段」嗎? –

+0

@ChinmayDabke看我的編輯。 – Raghunandan

+0

我應該把「selectItem(pos);」 「之後」drawer.closeDrawer(navList);「? –

2

使用:

public class MenuFragmentActivity extends FragmentActivity{ 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.base_layout); 
    addFragments(new Sample(), false, false, 
      AndyConstants.CONTENT_PAGE); 
    } 

    public void addFragments(Fragment fragment, boolean animate, 
     boolean addToBackStack, String tag) { 

    FragmentManager manager = getSupportFragmentManager(); 
    FragmentTransaction ft = manager.beginTransaction(); 
    if (animate) { 
     ft.setCustomAnimations(R.anim.fragment_from_right, 
     R.anim.fragment_from_left, R.anim.fragment_from_right, 
     R.anim.fragment_from_left); 
    } 
    if (addToBackStack) { 
     ft.addToBackStack(tag); 
    } 
    ft.replace(R.id.content_frame, fragment); 
    ft.commit(); 
} 

}

對於片段:

public class Sample extends Fragment { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
} 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
      View view = inflater.inflate(R.layout.page, container, false); 
     return view; 
    } 
    } 
+0

無需擴展'FragmentActivity'。如果您支持11以下的導航欄,則需要使用AppCompat。需要擴展'ActionBarActivity' – Raghunandan

+0

按照每個問題,它可以很容易地通過FragmentActivity完成。其導航抽屜爲 – Kishan

+0

。 – Raghunandan

1

產生Navigation Drawer Activity通過File->New->Activity->Navigation Drawer Activity,這裏有3個步驟

首先後,轉到app_bar_main.xml然後

更換

<include layout="@layout/content_main"/>

通過

<FrameLayout 
     android:id="@+id/frame_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     /> 

,去MainActivity - >onNavigationItemSelected然後修改它像

public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
    int id = item.getItemId(); 
    Fragment fragment = null; 
    if (id == R.id.nav_camera) { 
     fragment = CameraFragment.newInstance(); 
    } else if (id == R.id.nav_gallery) { 
     fragment = GalleryFragment.newInstance(); 
    } else if (id == R.id.nav_slideshow) { 
     fragment = SlideShowFragment.newInstance(); 
    } 

    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.frame_content, fragment).commit(); 

    setTitle(item.getTitle()); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

最後,創建Fragment類,例如片段

public class CameraFragment extends Fragment{ 

    public static CameraFragment newInstance() { 
     Bundle args = new Bundle(); 
     CameraFragment fragment = new CameraFragment(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
      @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_camera, null, false); 
     return rootView; 
    } 
} 

Demo project

enter image description here