下面代码的实现效果就是下面那个截图,无任何其他功能,很单纯,就是Kotilin实现Android顶部导航栏与模块联动~~~
项目目录:
实现效果:
MainActivity:
package com.lgz.kotlinstudy.activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.lgz.kotlinstudy.R
import com.lgz.kotlinstudy.fragment.GoodsFragment
class MainActivity : AppCompatActivity() {
private val goodsTypeList = listOf("推荐", "百货", "食品","男装","女装","鞋包","手机")
private val fragmentList = ArrayList<GoodsFragment>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tableLayout = findViewById<TabLayout>(R.id.tab_layout)
val viewPager = findViewById<ViewPager>(R.id.view_pager)
for (goodsType in goodsTypeList) {
fragmentList.add(GoodsFragment(goodsType))
}
viewPager.adapter = ViewPagerAdapter(supportFragmentManager)
tableLayout.setupWithViewPager(viewPager)
}
// BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT ----> Indicates that only the current fragment will be in the {@link Lifecycle.State#RESUMED}
inner class ViewPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
override fun getCount(): Int {
return fragmentList.size
}
// 展示顶部导航栏标题
override fun getPageTitle(position: Int): CharSequence? {
return goodsTypeList[position]
}
}
}
GoodsAdapter:
package com.lgz.kotlinstudy.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lgz.kotlinstudy.R
import com.lgz.kotlinstudy.model.GoodsItemOne
class GoodsAdapter(private val goodsItemOneList: List<GoodsItemOne>) : RecyclerView.Adapter<GoodsAdapter.GoodsViewHolder>() {
inner class GoodsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val pic: TextView = itemView.findViewById(R.id.pic)
val description: TextView = itemView.findViewById(R.id.description)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodsViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.goods_item_one, parent,false)
return GoodsViewHolder(itemView)
}
override fun getItemCount(): Int {
return goodsItemOneList.size
}
override fun onBindViewHolder(holder: GoodsViewHolder, position: Int) {
val goodsItemOne = goodsItemOneList[position]
holder.pic.text = goodsItemOne.pic;
holder.description.text = goodsItemOne.description
}
}
GoodsFragment:
package com.lgz.kotlinstudy.fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lgz.kotlinstudy.KotlinStudyApplication
import com.lgz.kotlinstudy.R
import com.lgz.kotlinstudy.adapter.GoodsAdapter
import com.lgz.kotlinstudy.model.GoodsItemOne
class GoodsFragment(val title: String) : Fragment() {
private lateinit var goodsRecyclerView: RecyclerView
private val goodsItemOneList = listOf(
GoodsItemOne(
"image1",
"image1dec"
),
GoodsItemOne(
"image2",
"image2dec"
)
)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_goods, container, false)
goodsRecyclerView = view.findViewById(R.id.goods_recycler_view)
return view
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
goodsRecyclerView.layoutManager = LinearLayoutManager(KotlinStudyApplication.context)
goodsRecyclerView.adapter = GoodsAdapter(goodsItemOneList)
}
}
GoodsItemOneModel.kt:
package com.lgz.kotlinstudy.model
data class GoodsItemOne(val pic: String, val description: String)
ToastUtil.kt:
package com.lgz.kotlinstudy.util
import android.widget.Toast
import com.lgz.kotlinstudy.KotlinStudyApplication
// 设置String的扩展函数
fun String.showToast(){
Toast.makeText(KotlinStudyApplication.context,this,Toast.LENGTH_SHORT).show()
}
KotlinStudyApplication:
package com.lgz.kotlinstudy
import android.app.Application
import android.content.Context
class KotlinStudyApplication: Application(){
companion object{
lateinit var context: Context
}
override fun onCreate() {
super.onCreate()
context = baseContext
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="50dp" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
fragment_goods.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/goods_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
goods_item_one.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<TextView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/image" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/description" />
</LinearLayout>
记住在AndroidManifest.xml里面注明,下面指示的那句话。values文件夹下的东西就不展示了~~