- 添加所需依赖
implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
implementation 'com.github.mamumu:mmDialog:1.3.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.0.1'
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation 'com.jaeger.statusbarutil:library:1.5.1'
implementation 'com.ashokvarma.android:bottom-navigation-bar:1.3.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'de.hdodenhof:circleimageview:3.0.0'
implementation 'com.rxjava.rxhttp:rxhttp:1.1.6'
annotationProcessor 'com.rxjava.rxhttp:rxhttp-compiler:1.1.6'
implementation 'com.rxjava.rxlife:rxlife:1.0.9'
- 自定义BaseActivity
package com.iefeel.kaoqin;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.jaeger.library.StatusBarUtil;
import com.mumu.dialog.MMLoading;
import com.mumu.dialog.MMToast;
import rxhttp.wrapper.annotation.DefaultDomain;
public class BaseActivity extends AppCompatActivity {
private MMLoading mmLoading;
private MMToast mmToast;
@DefaultDomain() //设置为默认域名
public static String baseUrl = "http://xuexi.iefeel.com";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
protected void setStatusBar() {
StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
//显示加载中
protected void showLoading() {
if (mmLoading == null) {
MMLoading.Builder builder = new MMLoading.Builder(this)
.setMessage("加载中...")
.setCancelable(false)
.setCancelOutside(false);
mmLoading = builder.create();
}else {
mmLoading.dismiss();
MMLoading.Builder builder = new MMLoading.Builder(this)
.setMessage("加载中...")
.setCancelable(false)
.setCancelOutside(false);
mmLoading = builder.create();
}
mmLoading.show();
}
//显示加载中,带文字
protected void showLoading(String msg) {
if (mmLoading == null) {
MMLoading.Builder builder = new MMLoading.Builder(this)
.setMessage(msg)
.setCancelable(false)
.setCancelOutside(false);
mmLoading = builder.create();
}else {
mmLoading.dismiss();
MMLoading.Builder builder = new MMLoading.Builder(this)
.setMessage(msg)
.setCancelable(false)
.setCancelOutside(false);
mmLoading = builder.create();
}
mmLoading.show();
}
//隐藏加载中
protected void hideLoading() {
if (mmLoading != null && mmLoading.isShowing()) {
mmLoading.dismiss();
}
}
//显示成功提示
protected void showToastSuccess(String msg) {
if (mmToast == null) {
MMToast.Builder builder=new MMToast.Builder(this)
.setMessage(msg)
.setSuccess(true);
mmToast=builder.create();
}else {
mmToast.cancel();
MMToast.Builder builder=new MMToast.Builder(this)
.setMessage(msg)
.setSuccess(true);
mmToast=builder.create();
}
mmToast.show();
}
//显示失败提示
protected void showToastFailure(String msg) {
if (mmToast == null) {
MMToast.Builder builder=new MMToast.Builder(this)
.setMessage(msg)
.setSuccess(false);
mmToast=builder.create();
}else {
mmToast.cancel();
MMToast.Builder builder=new MMToast.Builder(this)
.setMessage(msg)
.setSuccess(false);
mmToast=builder.create();
}
mmToast.show();
}
}
- 添加MainActivity
package com.iefeel.kaoqin;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.navigation.NavigationView;
import com.jaeger.library.StatusBarUtil;
import com.mumu.dialog.MMAlertDialog;
public class MainActivity extends BaseActivity {
private DrawerLayout mDrawerLayout;
private Toolbar mToolbar;
private ViewGroup contentLayout;
private NavigationView mNavigation;
private int mStatusBarColor;
private int mAlpha = StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = findViewById(R.id.drawer_layout);
contentLayout = findViewById(R.id.main);
mNavigation = findViewById(R.id.navigation);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
mNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_camera:
MMAlertDialog.showDialog(MainActivity.this,
"系统提示",
"点击了Camera",
null,
"确定",
false,
null,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
break;
case R.id.nav_gallery:
MMAlertDialog.showDialog(MainActivity.this,
"系统提示",
"点击了Gallery",
null,
"确定",
false,
null,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
break;
default:
MMAlertDialog.showDialog(MainActivity.this,
"系统提示",
"点击了导航栏菜单" + menuItem.getTitle(),
null,
"确定",
false,
null,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
break;
}
return false;
}
});
}
@Override
protected void setStatusBar() {
mStatusBarColor = getResources().getColor(R.color.colorPrimary);
StatusBarUtil.setColorForDrawerLayout(this, findViewById(R.id.drawer_layout), mStatusBarColor, mAlpha);
}
//创建菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
//菜单点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.btn_about_us:
String version = getVersionName(this);
MMAlertDialog.showDialog(MainActivity.this,
getString(R.string.app_name),
"当前版本:" + version,
null,
"确定",
false,
null,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
break;
case R.id.btn_scan_some:
MMAlertDialog.showDialog(MainActivity.this,
"系统提示",
"开发中,敬请期待",
null,
"确定",
false,
null,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
break;
case R.id.btn_close_app:
finish();
break;
}
return true;
}
//获取版本名
public static String getVersionName(Context context) {
return getPackageInfo(context).versionName;
}
//获取版本号
public static int getVersionCode(Context context) {
return getPackageInfo(context).versionCode;
}
//通过PackageInfo得到的想要启动的应用的包名
private static PackageInfo getPackageInfo(Context context) {
PackageInfo pInfo = null;
try {
//通过PackageManager可以得到PackageInfo
PackageManager pManager = context.getPackageManager();
pInfo = pManager.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
return pInfo;
} catch (Exception e) {
e.printStackTrace();
}
return pInfo;
}
}
- 添加布局文件res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<TextView
android:id="@+id/txtv_hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Hello World"
android:textAlignment="center"/>
</LinearLayout>
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/white"
app:headerLayout="@layout/nav_header"
app:menu="@menu/activity_main_drawer"/>
</androidx.drawerlayout.widget.DrawerLayout>
- 添加抽屉头部res/layout/nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nav_header_bg"
android:scaleType="centerCrop"/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:src="@drawable/ic_profile"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="24dp"
android:padding="10dp"
android:textAlignment="center"
android:text="@string/nav_title"
android:textColor="@android:color/white"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="10dp"
android:textAlignment="center"
android:text="@string/nav_subtitle"
android:textColor="@android:color/white"
android:textSize="12sp"/>
</FrameLayout>
- 添加抽屉主体res/menu/activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Camera"/>
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery"/>
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow"/>
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools"/>
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share"/>
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send"/>
</menu>
</item>
<item android:title="Communicate2">
<menu>
<item
android:id="@+id/nav_share_two"
android:icon="@drawable/ic_menu_share"
android:title="Share2"/>
<item
android:id="@+id/nav_send_two"
android:icon="@drawable/ic_menu_send"
android:title="Send2"/>
</menu>
</item>
</menu>
- 添加右上角菜单res/menu/main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/btn_about_us" android:title="@string/about_us_text"/>
<item android:id="@+id/btn_scan_some" android:title="@string/scan_some_text"/>
<item android:id="@+id/btn_close_app" android:title="@string/close_app_text"/>
</menu>
- 运行效果图
更多内容点此查看。