Kotlin实现Android顶部导航栏与模块联动

该博客展示了如何使用Kotlin在Android应用中实现顶部TabLayout导航栏与ViewPager的联动,通过创建多个GoodsFragment展示不同的商品模块,每个Fragment包含一个RecyclerView展示商品列表。博客中详细给出了MainActivity、GoodsAdapter、GoodsFragment等关键类的代码实现。
摘要由CSDN通过智能技术生成

下面代码的实现效果就是下面那个截图,无任何其他功能,很单纯,就是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文件夹下的东西就不展示了~~

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值