Android学习笔记21

接下来要实作跳转之後的activty连接着viewpager跟tabitem去对应到相对的fragment
首先先跳转到一个新的activity

<Button
    android:id="@+id/next"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="next"/>
binding.next.setOnClickListener {
    start(FragmentActivity::class.java)
}

那这边说一下这个start是我在第十天的时候有说道的baseactivity那今天使用的时候发现了一个bug,修改完後应该是长这样

protected fun start(next: Class<*>, bundle: Bundle?, finished: Boolean) {
    val intent = Intent(this, next)
    if(bundle == null)intent.putExtras(Bundle())
    else intent.putExtras(bundle)
    startActivity(intent)
    if (finished) this.finish()
}

intent到的next应该要是我上面传入的建构值而不是新增一个
那接着是新的activity的xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="viewmodel"
            type="com.example.myapplication.FragmentViewModel" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <androidx.viewpager.widget.ViewPager
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"/>
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"/>
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3"/>
        </com.google.android.material.tabs.TabLayout>
    </LinearLayout>

</layout>

再来是activity

class FragmentActivity : BaseActiivity(){
    private val fragmentViewModel by lazy {
        initViewModel(application , FragmentViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<ActivityFragmentBinding>(this,R.layout.activity_fragment)
        binding.lifecycleOwner = this
        binding.viewmodel = fragmentViewModel
    }
}

跳转之後就长这样啦
https://ithelp.ithome.com.tw/upload/images/20210930/20141791nf4bCpUQKE.png


<<:  端点防护软件 - 政府组态基准 GCB

>>:  初学者跪着学JavaScript Day15 : 阵列中没被定义的空值(empty item)

喜欢的事情要成为专业,才能做喜欢的事情?

「喜欢的事情要成为专业,才能做喜欢的事情。」​ 这是我昨天跟朋友聊天时,听到最不合理,但却又像现代的...

“Work Smart” vs “Work Hard”? (单选题)

孵了很久,终於孵出一篇新文章! 这篇是我思考了很久才决定发表的,因为目前疫情刚趋缓,所以许多企业赶着...

DAY 30 Big Data 5Vs – Value(价值) - AWS & YOU

YOU不是什麽厉害服务的缩写,就是你 因为整个资料分析过程中做有价值的 — 就是使用者本人。 AI ...

Sass/Css 设计模式(Smacss) DAY37

今天要来介绍 Sass/Css 设计模式啦~~ 首先我们先认识 最好入门的 Smacss 官网: h...

DAY 4 - 牛头怪

大家好~ 我是五岁~ 今天来画牛头怪~ 今天会尝试卡通风格~ 目标是一只跟人一样站立的牛头怪,武器是...