Activity
private FragmentTransaction fragmentTransaction;
private Fragment_one fragment_one;
private Fragment_two fragmentTwo;
private LinearLayout drawLayout;
private DrawerLayout mDraw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取布局管理器
fragmentTransaction = getSupportFragmentManager().beginTransaction();
//fragment
fragment_one = new Fragment_one();
fragmentTwo = new Fragment_two();
//加入管理器
fragmentTransaction.add(R.id.frame, fragment_one);
fragmentTransaction.add(R.id.frame, fragmentTwo);
//默认展示
fragmentTransaction.hide(fragmentTwo);
//提交
fragmentTransaction.commit();
//初始化DrawLayout
mDraw = findViewById(R.id.myDraw);
//初始化侧拉布局
drawLayout = findViewById(R.id.drawlayout);
//list集合
ListView listView = findViewById(R.id.list);
List<String> list = new ArrayList<>();
list.add("Fragment one");
list.add("Fragment two");
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, list));
//条目点击联动fragment
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.hide(fragment_one);
fragmentTransaction.hide(fragmentTwo);
switch (position) {
case 0:
fragmentTransaction.show(fragment_one);
break;
case 1:
fragmentTransaction.show(fragmentTwo);
break;
}
fragmentTransaction.commit();
mDraw.closeDrawer(drawLayout);
}
});
//点击图标打开侧拉篮
ImageView mMenuList = findViewById(R.id.img_icon);
mMenuList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDraw.openDrawer(drawLayout);
}
});
}
layout
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myDraw"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<ImageView
android:id="@+id/img_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/menu" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="八维电商"
android:textSize="20sp" />
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"></FrameLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/drawlayout"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher_round" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
</ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>