当前位置:   article > 正文

ANDROID drawerlayout+fragment侧滑_android fragment drawerlayout

android fragment drawerlayout
  1. <android.support.v4.widget.DrawerLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/id_drawerlayout"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent" >
  7. <FrameLayout
  8. android:id="@+id/id_framelayout"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="#fffaf0" >
  12. </FrameLayout>
  13. <LinearLayout
  14. android:id="@+id/id_linearlayout"
  15. android:layout_width="200dp"
  16. android:layout_height="match_parent"
  17. android:layout_gravity="start"
  18. android:background="#f5f5f5"
  19. android:orientation="vertical" >
  20. <ListView
  21. android:id="@+id/id_listView"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:divider="#deb887"
  25. android:dividerHeight="1dp" >
  26. </ListView>
  27. </LinearLayout>
  28. </android.support.v4.widget.DrawerLayout >

这里直接放一个drawerlayout,里面上面那个是主内容

下面的是侧滑菜单,这里放的一个ListView

下面放java文件

  1. package com.example.sliding;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.support.v4.app.FragmentActivity;
  5. import android.support.v4.app.FragmentManager;
  6. import android.support.v4.widget.DrawerLayout;
  7. import android.support.v7.app.ActionBarActivity;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.AdapterView.OnItemClickListener;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.ListView;
  15. public class MainActivity extends ActionBarActivity implements OnItemClickListener {
  16. private DrawerLayout mDrawLayout;
  17. private ListView mListView;
  18. private FragmentManager fragmentManager;
  19. private Fragment fragment1,fragment2,fragment3;
  20. private String[] str;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. intiView();
  26. }
  27. private void intiView() {
  28. mDrawLayout=(DrawerLayout) findViewById(R.id.id_drawerlayout);
  29. mListView=(ListView) findViewById(R.id.id_listView);
  30. str = new String[] { "item1", "item2", "item3"};
  31. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.
  32. R.layout.simple_list_item_1, str);
  33. mListView.setAdapter(adapter);
  34. mListView.setOnItemClickListener(this);
  35. fragmentManager = getSupportFragmentManager();
  36. fragment1= new Fragment1();
  37. fragment2 = new Fragment2();
  38. fragment3 = new Fragment3();
  39. fragmentManager.beginTransaction().replace(R.id.id_framelayout, fragment1).commit();
  40. setTitle("fragment 1");
  41. mDrawLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
  42. });
  43. }
  44. @Override
  45. public boolean onCreateOptionsMenu(Menu menu) {
  46. // Inflate the menu; this adds items to the action bar if it is present.
  47. getMenuInflater().inflate(R.menu.main, menu);
  48. return true;
  49. }
  50. @Override
  51. public boolean onOptionsItemSelected(MenuItem item) {
  52. // Handle action bar item clicks here. The action bar will
  53. // automatically handle clicks on the Home/Up button, so long
  54. // as you specify a parent activity in AndroidManifest.xml.
  55. int id = item.getItemId();
  56. if (id == R.id.action_settings) {
  57. return true;
  58. }
  59. return super.onOptionsItemSelected(item);
  60. }
  61. @Override
  62. public void onItemClick(AdapterView<?> parent, View view, int position,
  63. long id) {
  64. switch (position) {
  65. case 0:
  66. fragmentManager.beginTransaction().
  67. replace(R.id.id_framelayout, fragment1).commit();
  68. setTitle("fragment1");
  69. break;
  70. case 1:
  71. fragmentManager.beginTransaction().
  72. replace(R.id.id_framelayout, fragment2).commit();
  73. setTitle("fragment2");
  74. break;
  75. case 2:
  76. fragmentManager.beginTransaction().
  77. replace(R.id.id_framelayout, fragment3).commit();
  78. setTitle("fragment3");
  79. break;
  80. }
  81. mDrawLayout.closeDrawers();
  82. }
  83. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/279594?site
推荐阅读
相关标签
  

闽ICP备14008679号