上篇文章实现了Tab选项卡,那么本篇文章就封装下Toolbar,至于Toolbar是什么,能做什么大家可以去看看D_clock爱吃葱花的文章Android开发:最详细的 Toolbar 开发实践总结;部分设置我也是从文章里学到了,那么我们先来看代码:
开发语言:Kotlin
Toolbar封装
ToolbarManager接口
package com.vslimit.kotlindemo.ui
import android.support.v7.graphics.drawable.DrawerArrowDrawable
import android.support.v7.widget.Toolbar
import com.vslimit.kotlindemo.R
import com.vslimit.kotlindemo.activity.MainActivity
import com.vslimit.kotlindemo.extensions.ctx
import org.jetbrains.anko.appcompat.v7.onMenuItemClick
import org.jetbrains.anko.*
interface ToolbarManager {
val toolbar: Toolbar
var toolbarTitle: String
get() = toolbar.title.toString()
set(value) {
toolbar.title = value
}
fun enableHomeAsUp(up: () -> Unit) {
toolbar.navigationIcon = createUpDrawable()
toolbar.setNavigationOnClickListener { up() }
}
fun enableMenu(id: Int = R.menu.menu_home, up: () -> Unit) {
toolbar.inflateMenu(id)
toolbar.onMenuItemClick {
when (it!!.itemId) {
R.id.index -> toolbar.ctx.startActivity<MainActivity>()
else -> up()
}
true
}
}
fun hiddenMenu(redId: Int) {
toolbar.menu.findItem(redId).isVisible = false
}
private fun createUpDrawable() = DrawerArrowDrawable(toolbar.ctx).apply { progress = 1f }
}
kotlin中的接口与java 8 中的接口类似,接口允许方法有实现,和抽象类的区别是,接口中的属性不允许直接初始化,默认是abstract的。因此,在ToolbarManager中定义了几个方法,当Activity或Fragment继承接口时,可按照需要来实现或扩展。
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent" xmlns:toolbar="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
toolbar:title="Kotlin Demo"
toolbar:titleTextColor="@android:color/white"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
ToolbarManager使用
Activity或Fragment都可以按照需要来继承ToolbarManager,本文中使用Activity来继承,首先在BaseActivity添加如下代码:
BaseActivity
package com.vslimit.kotlindemo.activity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import com.vslimit.kotlindemo.R
import com.vslimit.kotlindemo.ui.ToolbarManager
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.find
/**
* Created by vslimit on 16/1/12.
*/
abstract class BaseActivity : AppCompatActivity(), ToolbarManager, AnkoLogger {
abstract val layoutResourceId: Int
override val toolbar by lazy { find<Toolbar>(R.id.toolbar) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layoutResourceId)
}
override fun onResume() {
super.onResume()
}
override fun onPause() {
super.onPause()
}
override fun onDestroy() {
super.onDestroy()
}
override fun onStop() {
super.onStop()
}
fun switchContent(from: Fragment, to: Fragment) {
val fm: FragmentManager = supportFragmentManager
//添加渐隐渐现的动画
val ft: FragmentTransaction = fm.beginTransaction()
if (!to.isAdded) {
// 先判断是否被add过
ft.hide(from).add(R.id.frameLayout, to) // 隐藏当前的fragment,add下一个到Activity中
} else {
ft.hide(from).show(to) // 隐藏当前的fragment,显示下一个
}
ft.commit()
}
fun initFragment(to: Fragment) {
val fm: FragmentManager = supportFragmentManager
//添加渐隐渐现的动画
// val ft: FragmentTransaction = fm.beginTransaction()
fm.beginTransaction().add(R.id.frameLayout, to).commit()
}
open fun initTitle(title: String = "Kotlin Demo") {
toolbarTitle = title
}
open fun initBack() {
enableHomeAsUp { onBackPressed() }
}
open fun initHome() {
enableMenu {}
}
}
在具体的Activity中,比如MainActivity的布局中引入toolbar.xml
activity_main.xml
<include layout="@layout/toolbar" android:id="@+id/tool"/>
MainActivity
将title设置为首页
initTitle("首页")
DemoActivity
设置标题与后退键
package com.vslimit.kotlindemo.activity
import android.os.Bundle
import com.vslimit.kotlindemo.R
import com.vslimit.kotlindemo.model.FragmentEnum
class DemoActivity(override val layoutResourceId: Int = R.layout.activity_demo) : BaseActivity() {
companion object {
val NAME = "DemoActivity:name"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (savedInstanceState == null) {
val name = intent.getStringExtra(NAME)
initFragment(FragmentEnum.valueOf(name).fragment)
}
initTitle("DemoActivity")
initBack()
}
}
效果图如下:
至此Toolbar封装完成,如果有定制需求,可以在ToolbarManager中实现,当然了,Fragment也可以继承ToolbarManager,具体的大家可以自己扩展下。
Toobar的代码详见:https://github.com/vslimit/kotlindemo