Koltin18.Takeout首页RecycleView多种类型的数据(4)

HomeFragment.kt

package com.example.takeout.ui.fragment

import android.app.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import com.example.takeout.ui.adapter.HomeRvAdapter
import org.jetbrains.anko.find

class HomeFragment : Fragment() {
    lateinit var homeRvAdapter:HomeRvAdapter//延迟初始化

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = View.inflate(activity, R.layout.fragment_home, null)
        val rvHome = view.find<RecyclerView>(R.id.rv_home)
        //上下布局
        rvHome.layoutManager = LinearLayoutManager(activity)
        homeRvAdapter = HomeRvAdapter(activity)
        rvHome.adapter = homeRvAdapter
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initData()
    }

    val datas: ArrayList<String> = ArrayList<String>()
    private fun initData() {
        for (i in 0 until 100) {
            datas.add("我是商家:" + i)
        }
        homeRvAdapter.setData(datas)
    }

}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_home"
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/ll_title_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#553190E8"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:id="@+id/ll_title_search"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="30dp"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"

                android:orientation="horizontal">

                <TextView
                    android:id="@+id/home_tv_address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="5dip"
                    android:layout_weight="1"
                    android:singleLine="true"
                    android:text="昌平区北七家宏福创业园修正大厦"

                    android:textColor="#FFF" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"

                    android:layout_marginRight="5dip"
                    android:src="@mipmap/home_address_drop_down" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="12dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="12dp"
                android:layout_weight="3"
                android:background="@drawable/home_search_text_background">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="10dip"
                    android:layout_weight="1"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginLeft="4dp"
                        android:src="@mipmap/home_search" />

                    <TextView

                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:text="输入商家、商品名称"
                        android:textColor="#FFF"
                        android:textSize="12sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>


    </LinearLayout>
</RelativeLayout>

HomeRvAdapter.kt

package com.example.takeout.ui.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import org.jetbrains.anko.find

class HomeRvAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    //定义常量
    companion object {
        val TYPE_TITLE = 0
        val TYPE_SELLER = 1
    }

    var mDatas: ArrayList<String> = ArrayList()

    fun setData(data: ArrayList<String>) {
        this.mDatas = data
        notifyDataSetChanged()
    }

    /**
     * 不同position对应不同类型
     */
    override fun getItemViewType(position: Int): Int {
        if (position == 0) {
            return TYPE_TITLE
        } else {
            return TYPE_SELLER
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val viewType = getItemViewType(position)
        when (viewType) {
            TYPE_TITLE -> (holder as TitleHolder).bindData("我是title----------------------------------------")
            TYPE_SELLER -> (holder as SellerHolder).bindData(mDatas[position - 1])
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            TYPE_TITLE -> return TitleHolder(View.inflate(context, R.layout.item_home_common, null))
            TYPE_SELLER -> return SellerHolder(View.inflate(context, R.layout.item_home_common, null))
            else -> return TitleHolder(View.inflate(context, R.layout.item_home_common, null))
        }
    }

    override fun getItemCount(): Int {
        if (mDatas.size > 0) {
            return mDatas.size + 1
        } else {
            return 0
        }
    }

    //内部类,商家的holder
    inner class SellerHolder(item: View) : RecyclerView.ViewHolder(item) {
        val textView: TextView

        init {
            textView = item.find(R.id.tv)
        }

        fun bindData(data: String) {
            textView.text = data
        }
    }

    //内部类,title的holder
    inner class TitleHolder(item: View) : RecyclerView.ViewHolder(item) {
        val textView: TextView

        init {
            textView = item.find(R.id.tv)
        }

        fun bindData(data: String) {
            textView.text = data
        }
    }
}

item_home_common.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="" />
</RelativeLayout>

效果图如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值