Skip to content

Commit 0f1697a

Browse files
committed
add drawer
1 parent b373bfb commit 0f1697a

File tree

7 files changed

+104
-39
lines changed

7 files changed

+104
-39
lines changed

app/src/main/java/com/codeest/geeknews/ui/main/MainActivity.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
package com.codeest.geeknews.ui.main;
22

3+
import android.support.design.widget.NavigationView;
4+
import android.support.v4.widget.DrawerLayout;
5+
import android.support.v7.app.ActionBarDrawerToggle;
6+
import android.support.v7.widget.Toolbar;
7+
import android.view.MenuItem;
8+
39
import com.codeest.geeknews.R;
410
import com.codeest.geeknews.base.BaseActivity;
11+
import com.codeest.geeknews.util.LogUtil;
12+
13+
import butterknife.BindView;
514

615
/**
716
* Created by codeest on 16/8/9.
817
*/
918

1019
public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View{
1120

21+
@BindView(R.id.drawer)
22+
DrawerLayout mDrawerLayout;
23+
@BindView(R.id.toolbar)
24+
Toolbar mToolbar;
25+
@BindView(R.id.navigation)
26+
NavigationView mNavigationView;
27+
28+
ActionBarDrawerToggle mDrawerToggle;
29+
30+
int currentNavigationId = 0;
31+
1232
@Override
1333
protected void initInject() {
1434
getActivityComponent().inject(this);
@@ -21,7 +41,22 @@ protected int getLayout() {
2141

2242
@Override
2343
protected void initEventAndData() {
44+
setToolBar(mToolbar,"首页");
45+
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
46+
mDrawerToggle.syncState();
47+
mDrawerLayout.setDrawerListener(mDrawerToggle);
2448

49+
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
50+
@Override
51+
public boolean onNavigationItemSelected(MenuItem menuItem) {
52+
LogUtil.d(menuItem.getTitle().toString());
53+
menuItem.setChecked(true); // 改变item选中状态
54+
setTitle(menuItem.getTitle()); // 改变页面标题,标明导航状态
55+
currentNavigationId = menuItem.getItemId();
56+
mDrawerLayout.closeDrawers(); // 关闭导航菜单
57+
return true;
58+
}
59+
});
2560
}
2661

2762
@Override
Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
4-
android:layout_height="match_parent"
5-
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
android:layout_height="match_parent">
65

7-
<LinearLayout
8-
android:orientation="vertical"
6+
<include layout="@layout/view_toolbar"
7+
android:id="@+id/toolbar"/>
8+
9+
<android.support.v4.widget.DrawerLayout
10+
android:id="@+id/drawer"
911
android:layout_width="match_parent"
10-
android:layout_height="match_parent">
11-
<android.support.v7.widget.Toolbar
12-
android:id="@+id/tool_bar"
13-
android:layout_width="match_parent"
14-
android:layout_height="?attr/actionBarSize"/>
15-
<FrameLayout
16-
android:id="@+id/fl_main_content"
12+
android:layout_height="match_parent"
13+
android:layout_below="@id/toolbar"
14+
xmlns:app="http://schemas.android.com/apk/res-auto">
15+
16+
<LinearLayout
17+
android:orientation="vertical"
1718
android:layout_width="match_parent"
18-
android:layout_height="match_parent"/>
19-
</LinearLayout>
19+
android:layout_height="match_parent">
20+
21+
<FrameLayout
22+
android:id="@+id/fl_main_content"
23+
android:layout_width="match_parent"
24+
android:layout_height="match_parent"/>
25+
</LinearLayout>
26+
27+
<android.support.design.widget.NavigationView
28+
android:id="@+id/navigation"
29+
android:layout_width="240dp"
30+
android:layout_height="match_parent"
31+
android:layout_gravity="start"
32+
app:headerLayout="@layout/view_drawer_header"
33+
app:menu="@menu/drawer_menu"
34+
>
35+
</android.support.design.widget.NavigationView>
36+
</android.support.v4.widget.DrawerLayout>
37+
</RelativeLayout>
2038

21-
<android.support.design.widget.NavigationView
22-
android:layout_width="240dp"
23-
android:layout_height="match_parent"
24-
android:layout_gravity="start"
25-
app:headerLayout="@layout/view_drawer_header"
26-
app:menu="@menu/drawer_menu"
27-
>
28-
</android.support.design.widget.NavigationView>
29-
</android.support.v4.widget.DrawerLayout>
3039

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:orientation="vertical" android:layout_width="match_parent"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
44
android:layout_height="match_parent">
55

6+
<ImageView
7+
android:id="@+id/drawer_cover"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent" />
610
<TextView
711
android:layout_width="wrap_content"
812
android:layout_height="wrap_content"
9-
android:text="Hello World!"/>
13+
android:layout_alignParentBottom="true"
14+
android:layout_marginBottom="6dp"
15+
android:layout_marginStart="20dp"
16+
android:textSize="18sp"
17+
android:text="@string/app_name"/>
1018

11-
</LinearLayout>
19+
</RelativeLayout>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/tool_bar"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:fitsSystemWindows="true"
7+
android:background="?attr/colorPrimaryDark"
8+
android:minHeight="?attr/actionBarSize"/>

app/src/main/res/menu/drawer_menu.xml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<menu xmlns:android="http://schemas.android.com/apk/res/android">
3-
<group android:id="@+id/g1">
3+
<group android:id="@+id/group_content">
44
<item
5-
android:id="@+id/favorite"
5+
android:id="@+id/drawer_zhihu"
66
android:icon="@mipmap/ic_launcher"
7-
android:title="收藏"/>
7+
android:title="@string/zhihu"/>
88
<item
9-
android:id="@+id/wallet"
9+
android:id="@+id/drawer_gank"
1010
android:icon="@mipmap/ic_launcher"
11-
android:title="钱包"/>
11+
android:title="@string/gank"/>
1212
</group>
13-
<group android:id="@+id/g2">
13+
<group android:id="@+id/group_other">
1414
<item
15-
android:id="@+id/photo"
15+
android:id="@+id/drawer_about"
1616
android:icon="@mipmap/ic_launcher"
17-
android:title="相册"/>
17+
android:title="@string/about"/>
1818
</group>
19-
<item
20-
android:id="@+id/file"
21-
android:icon="@mipmap/ic_launcher"
22-
android:title="文件"/>
2319
</menu>

app/src/main/res/values/strings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
<resources>
22
<string name="app_name">GeekNews</string>
3+
4+
//main drawer
5+
<string name="zhihu">知乎</string>
6+
<string name="gank">干货集中营</string>
7+
<string name="about">关于</string>
8+
<string name="drawer_open">打开</string>
9+
<string name="drawer_close">关闭</string>
310
</resources>

app/src/main/res/values/styles.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
<item name="colorPrimary">@color/colorPrimary</item>
77
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
88
<item name="colorAccent">@color/colorAccent</item>
9+
<item name="actionMenuTextColor">#ffffff</item>
910

1011
<item name="android:windowIsTranslucent">true</item>
12+
<item name="android:windowTranslucentStatus">true</item>
1113
</style>
1214

1315
</resources>

0 commit comments

Comments
 (0)