Android-3 Kotlin實現(xiàn)Tab選項卡

在之前兩篇文章中鼎俘,介紹了Android中網(wǎng)球請求的實現(xiàn)拐格,那么本篇文章中朵纷,我們實現(xiàn)下現(xiàn)在APP中最通用的Tabbar的實現(xiàn):

Tabbar1.0

以前的項目中Tabbar是使用Gridview實現(xiàn)赴恨,這里貼出部分代碼:
首先定義一個Tab的model類:

open class Tab(var res: Int, 
var selRes: Int, var name: String,
var tag:String,var f: Fragment) {}

Activity中的布局activity_home為:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content">
        <include layout="@layout/toolbar" android:id="@+id/tool"/>
        <GridView android:background="@color/white" android:layout_alignParentBottom="true"
                  android:numColumns="4" android:id="@+id/gridGv"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"/>
        <FrameLayout android:layout_below="@id/tool" android:layout_above="@id/gridGv"
                     android:id="@+id/frameLayout" android:layout_width="match_parent"
                     android:layout_height="match_parent"/>

    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

HomeActivity,其中當用戶未登錄時书斜,則跳到LoginActivity

class HomeActivity : BaseActivity() {

    internal var items = listOf(
            Tab(R.mipmap.ic_home, R.mipmap.ic_home_sel, "首頁", "index", HomeFragment.getInstance()),
            Tab(R.mipmap.ic_product, R.mipmap.ic_product_sel, "產(chǎn)品", "product", ProductFragment.getInstance()),
            Tab(R.mipmap.ic_cart, R.mipmap.ic_cart_sel, "購物車", "cart", CartFragment.getInstance()),
            Tab(R.mipmap.ic_user, R.mipmap.ic_user_sel, "我的", "me", MeFragment.getInstance()))

    var adapter: TabAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        adapter = TabAdapter(this)

        adapter!!.addAll(items)
        gridGv.gravity = Gravity.CENTER
        gridGv.selector = ColorDrawable(Color.TRANSPARENT)
        toFragment(items[0].f, R.id.frameLayout)
        gridGv.adapter = adapter
        initTitle("首頁")
        gridGv.onItemClick { adapterView, view, i, l ->
            if (items[i].tag.equals("me") && TextUtils.isEmpty(Preference.with(this).token) ) {
                startActivity<LoginActivity>()
                return@onItemClick
            }
            toFragment(items[i].f, R.id.frameLayout)
            initTitle(items[i].name)
            adapter?.pos = i
            adapter?.notifyDataSetChanged()
        }
        adapter!!.notifyDataSetChanged()

    }

    fun toFragment(f: Fragment, id: Int) {
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(id, f)
        transaction.commit()
    }

}

TabAdapter

/**
 * Created by vslimit on 16/1/15.
 */
class TabAdapter(context: Context) : ArrayAdapter<Tab>(context, 0) {
    val inflater = LayoutInflater.from(context)
    var pos: Int = 0
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val item = getItem(position)
        val view = convertView ?: inflater.inflate(R.layout.item_tab_menu, parent, false)

        val nameView = view.findViewById(R.id.item_text) as TextView
        val imageView = view.findViewById(R.id.item_image) as ImageView

        nameView.text = item.name
        nameView.textColor = if (position == pos) context.resources.getColor(R.color.head_orange) else context.resources.getColor(R.color.product_name_black)
        imageView.imageResource = if (position == pos) item.selRes else item.res

        return view
    }

這樣基于Gridview已經(jīng)實現(xiàn)了,但是指蚁,考慮到RecycleView比較流行菩佑,于是在新的版本中,使用了RecycleView來實現(xiàn)凝化,并對其中的部分實現(xiàn)進行了優(yōu)化稍坯,下面我們來看Tabbar2.0

Tabbar2.0

Tab類是預定好的,并且是有序的搓劫,因此瞧哟,采用enum來實現(xiàn):

package com.vslimit.kotlindemo.model

import com.vslimit.kotlindemo.R
import com.vslimit.kotlindemo.fragment.BaseFragment
import com.vslimit.kotlindemo.fragment.MainFragment
import com.vslimit.kotlindemo.fragment.ProductFragment

/**
 * Created by vslimit on 16/12/2.
 */
enum class TabTagEnum (val text: String, val res: Int, val selRes: Int, val fragment: BaseFragment) {
    HOME("首頁", R.mipmap.ic_home, R.mipmap.ic_home_sel, MainFragment.getInstance()), PRODUCT("發(fā)現(xiàn)", R.mipmap.ic_product, R.mipmap.ic_product_sel, ProductFragment.getInstance()), CART("購物車", R.mipmap.ic_cart, R.mipmap.ic_cart_sel, MainFragment.getInstance()), ME("我的", R.mipmap.ic_user, R.mipmap.ic_user_sel, MainFragment.getInstance())
}

Activity布局activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".activity.MainActivity">
    <android.support.v7.widget.RecyclerView
            android:id="@+id/tabRv"
            android:layout_width="match_parent"
            android:layout_height="49dp"
            android:layout_alignParentBottom="true"
            android:background="@android:color/white"/>
    <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_above="@id/tabRv" android:id="@+id/line" android:background="@android:color/black"/>
    <FrameLayout android:layout_above="@id/line"
                 android:id="@+id/frameLayout"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"/>
</RelativeLayout>

MainActivity之前使用replace,remove來進行Fragment的替換枪向,但是勤揩,考慮到性能優(yōu)化腹暖,改用hide舌界,show的方式姆蘸,可這樣處理容易導致fragment重疊問題叽讳,這里使用了@YoKey 的文章中的方式進行處理羡洁,詳見傳送門牵舱。

package com.vslimit.kotlindemo.activity

import android.os.Bundle
import android.support.annotation.Nullable
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.GridLayoutManager
import com.vslimit.kotlindemo.R
import com.vslimit.kotlindemo.model.TabTagEnum
import com.vslimit.kotlindemo.adapter.TabAdapter
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.info

class MainActivity : AppCompatActivity(), AnkoLogger {

    internal var items = TabTagEnum.values().asList()

    var adapter: TabAdapter? = null

    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (savedInstanceState == null) {
            initFragment(TabTagEnum.HOME.fragment)
        }
        val layoutManager: GridLayoutManager = GridLayoutManager(this, 4)
        tabRv.layoutManager = layoutManager
        adapter = TabAdapter(items) {
            switchContent(items[adapter!!.pos].fragment, it.fragment)
            adapter!!.pos = it.ordinal
            adapter!!.notifyDataSetChanged()
        }
        tabRv.adapter = adapter
        adapter!!.notifyDataSetChanged()
        info("onCreate")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        info("onRestoreInstanceState")
    }

    override fun onStart() {
        super.onStart()
        info("onStart")
    }

    override fun onResume() {
        super.onResume()
        info("onResume")
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        info("onSaveInstanceState")
    }


    override fun onPause() {
        super.onPause()
        info("onPause")
    }

    override fun onStop() {
        super.onStop()
        info("onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        info("onDestroy")
    }

    fun switchContent(from: Fragment, to: Fragment) {
        val fm: FragmentManager = supportFragmentManager
        //添加漸隱漸現(xiàn)的動畫
        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
        //添加漸隱漸現(xiàn)的動畫
//        val ft: FragmentTransaction = fm.beginTransaction()
        fm.beginTransaction().add(R.id.frameLayout, to).commit()
    }
}

TabAdapter

package com.vslimit.kotlindemo.adapter

import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.vslimit.kotlindemo.R
import com.vslimit.kotlindemo.extensions.ctx
import com.vslimit.kotlindemo.model.TabTagEnum
import kotlinx.android.synthetic.main.tab_item_menu.view.*
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.layoutInflater
import org.jetbrains.anko.onClick

/**
 * Created by vslimit on 16/12/2.
 */
class TabAdapter(val items: List<TabTagEnum>, val itemClick: (TabTagEnum) -> Unit) : RecyclerView.Adapter<TabAdapter.ViewHolder>() {

    var pos: Int = TabTagEnum.HOME.ordinal

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = parent.ctx.layoutInflater.inflate(R.layout.tab_item_menu, parent, false)
        return ViewHolder(view, itemClick)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindForecast(items[position], pos == position)
    }

    override fun getItemCount() = items.size

    class ViewHolder(view: View, val itemClick: (TabTagEnum) -> Unit) : RecyclerView.ViewHolder(view) {

        fun bindForecast(item: TabTagEnum, flag: Boolean) {
            with(item) {
                itemView.item_image.imageResource = if (flag) item.selRes else item.res
                itemView.item_text.text = item.text
                itemView.onClick { itemClick(item) }
            }
        }
    }

}

至此倦畅,基于RecycleViewTabbar已經(jīng)實現(xiàn)遮糖,效果如圖:

tabbar.png

RecycleView的代碼詳見:https://github.com/vslimit/kotlindemo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市叠赐,隨后出現(xiàn)的幾起案子欲账,更是在濱河造成了極大的恐慌,老刑警劉巖芭概,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赛不,死亡現(xiàn)場離奇詭異,居然都是意外死亡罢洲,警方通過查閱死者的電腦和手機踢故,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人畴椰,你說我怎么就攤上這事臊诊。” “怎么了斜脂?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵抓艳,是天一觀的道長。 經(jīng)常有香客問我帚戳,道長玷或,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任片任,我火速辦了婚禮偏友,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘对供。我一直安慰自己位他,他們只是感情好,可當我...
    茶點故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布产场。 她就那樣靜靜地躺著鹅髓,像睡著了一般。 火紅的嫁衣襯著肌膚如雪京景。 梳的紋絲不亂的頭發(fā)上窿冯,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天,我揣著相機與錄音确徙,去河邊找鬼醒串。 笑死,一個胖子當著我的面吹牛鄙皇,可吹牛的內(nèi)容都是我干的芜赌。 我是一名探鬼主播,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼育苟,長吁一口氣:“原來是場噩夢啊……” “哼较鼓!你這毒婦竟也來了椎木?” 一聲冷哼從身側(cè)響起违柏,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎香椎,沒想到半個月后漱竖,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡畜伐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年馍惹,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,742評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡万矾,死狀恐怖悼吱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情良狈,我是刑警寧澤后添,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站薪丁,受9級特大地震影響遇西,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜严嗜,卻給世界環(huán)境...
    茶點故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一粱檀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧漫玄,春花似錦茄蚯、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至刨秆,卻和暖如春凳谦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背衡未。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工尸执, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人缓醋。 一個月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓如失,卻偏偏與公主長得像,于是被迫代替她去往敵國和親送粱。 傳聞我的和親對象是個殘疾皇子褪贵,可洞房花燭夜當晚...
    茶點故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,312評論 25 707
  • STP分析定義:STP分析即市場細分、選擇目標市場和產(chǎn)品定位抗俄。STP分析是整個營銷建設的基礎脆丁,STP法則對各自的市...
    貓你在哪里閱讀 16,782評論 0 6
  • 夢想槽卫,是自由的。但是胰蝠,實現(xiàn)夢想歼培,度過幸福一生的震蒋,是少之又少的。因此躲庄,絕大多數(shù)人查剖,沒那么幸運,要么傷心地長吁短嘆噪窘,...
    流水桃花閱讀 326評論 0 2