HomeRvAdapter.kt条目的点击事件触发点
package com.example.takeout.ui.adapter
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.daimajia.slider.library.SliderLayout
import com.daimajia.slider.library.SliderTypes.TextSliderView
import com.example.takeout.R
import com.example.takeout.model.beans.Seller
import com.example.takeout.ui.activity.BusinessActivity
import com.squareup.picasso.Picasso
import org.jetbrains.anko.find
class HomeRvAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
//定义常量
companion object {
val TYPE_TITLE = 0
val TYPE_SELLER = 1
}
val host = "http://127.0.0.1:8090/image?name="
var mDatas: ArrayList<Seller> = ArrayList()
fun setData(data: ArrayList<Seller>) {
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_title, null))
TYPE_SELLER -> return SellerHolder(View.inflate(context, R.layout.item_seller, 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 tvTitle: TextView
val ivLogo: ImageView
val rbScore: RatingBar
val tvSale: TextView
val tvSendPrice: TextView
val tvDistance: TextView
init {
tvTitle = item.find(R.id.tv_title)
ivLogo = item.find(R.id.seller_logo)
rbScore = item.find(R.id.ratingBar)
tvSale = item.find(R.id.tv_home_sale)
tvSendPrice = item.find(R.id.tv_home_send_price)
tvDistance = item.find(R.id.tv_home_distance)
item.setOnClickListener {
val intent: Intent = Intent(context, BusinessActivity::class.java)
context.startActivity(intent)
}
}
fun bindData(seller: Seller) {
tvTitle.text = seller.name
//图片路径http://127.0.0.1:8090/image?name=takeout/imgs/seller/3.jpg
println("seller.ensure====" + seller.ensure)
Picasso.with(context).load(host + seller.ensure).into(ivLogo)
rbScore.rating = seller.score.toFloat()
tvSale.text = "月售${seller.sale}单"
tvSendPrice.text = "¥${seller.sendPrice}起送/配送费¥${seller.deliveryFee}"
}
}
//存放图片的url和名称
var url_maps: HashMap<String, Int> = HashMap()
//内部类,title的holder
inner class TitleHolder(item: View) : RecyclerView.ViewHolder(item) {
val sliderLayout: SliderLayout
init {
sliderLayout = item.findViewById(R.id.slider)
}
fun bindData(data: String) {
if (url_maps.size == 0) {
url_maps.put("Hannibal", R.mipmap.pic1);
url_maps.put("Big Bang Theory", R.mipmap.pic2);
url_maps.put("House of Cards", R.mipmap.pic3);
url_maps.put("Game of Thrones", R.mipmap.pic4);
for ((key, value) in url_maps) {
val textSlideView: TextSliderView = TextSliderView(context)
textSlideView.description(key).image(value)
sliderLayout.addSlider(textSlideView)
}
}
}
}
}
BusinessActivity.kt详情界面通过tab和viewpager进行交互
package com.example.takeout.ui.activity
import android.content.Context
import android.os.Bundle
import android.util.TypedValue
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.example.takeout.R
import com.example.takeout.ui.fragment.CommentsFragment
import com.example.takeout.ui.fragment.GoodsFragment
import com.example.takeout.ui.fragment.SellerFragment
import kotlinx.android.synthetic.main.activity_business.*
class BusinessActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_business)
//微调底部的导航栏适配
if (checkDeviceHasNavigationBar(this)) {
fl_Container.setPadding(0, 0, 0, 48.dp2px())
}
vp.adapter = BusinessFragmentPagerAdapter()
tabs.setupWithViewPager(vp)
}
val fragments = listOf<Fragment>(GoodsFragment(), SellerFragment(), CommentsFragment())
val titles = listOf<String>("商品", "商家", "评论")
/**
* 把转化功能添加到Int类中作为扩展函数
*/
fun Int.dp2px(): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
toFloat(), resources.displayMetrics
).toInt()
}
//获取是否存在NavigationBar
fun checkDeviceHasNavigationBar(context: Context): Boolean {
var hasNavigationBar = false
val rs = context.getResources()
val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
if (id > 0) {
hasNavigationBar = rs.getBoolean(id)
}
try {
val systemPropertiesClass = Class.forName("android.os.SystemProperties")
val m = systemPropertiesClass.getMethod("get", String::class.java)
val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
if ("1" == navBarOverride) {
hasNavigationBar = false
} else if ("0" == navBarOverride) {
hasNavigationBar = true
}
} catch (e: Exception) {
}
return hasNavigationBar
}
inner class BusinessFragmentPagerAdapter : FragmentPagerAdapter(supportFragmentManager) {
override fun getPageTitle(position: Int): CharSequence {
return titles.get(position)
}
override fun getItem(position: Int): Fragment {
return fragments.get(position)
}
override fun getCount(): Int {
return titles.size
}
}
}
CommentsFragment.kt评论界面
package com.example.takeout.ui.fragment
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
/**
* 详情页,评论界面
*/
class CommentsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val sellerView = TextView(activity)
sellerView.text = "评论"
sellerView.gravity = Gravity.CENTER
sellerView.setTextColor(Color.BLACK)
return sellerView
}
}
GoodsFragment.kt商品界面
package com.example.takeout.ui.fragment
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
/**
* 详情页商品列表界面
*/
class GoodsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val sellerView = TextView(activity)
sellerView.text = "产品"
sellerView.gravity = Gravity.CENTER
sellerView.setTextColor(Color.BLACK)
return sellerView
}
}
SellerFragment.kt商家界面
package com.example.takeout.ui.fragment
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
/**
* 详情页商家界面
*/
class SellerFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val sellerView = TextView(activity)
sellerView.text = "商家"
sellerView.gravity = Gravity.CENTER
sellerView.setTextColor(Color.BLACK)
return sellerView
}
}
activity_business.xml引入BottomSheetLayout了作为上面的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fl_Container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.flipboard.bottomsheet.BottomSheetLayout
android:id="@+id/bottomSheetLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="false"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f6f89191"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="25dp">
<ImageButton
android:id="@+id/ib_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:background="@mipmap/abc_ic_ab_back_mtrl_am_alpha"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:singleLine="true"
android:text="田老师红烧肉(东北旺西路店)"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"/>
<ImageButton
android:id="@+id/ib_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:background="@mipmap/abc_ic_menu_moreoverflow_mtrl_alpha"/>
</LinearLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#00f"
app:tabIndicatorHeight="4dp"
app:tabSelectedTextColor="#00f"
app:tabTextColor="#000"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#aa000000"
android:clickable="true"
android:gravity="center_vertical"
android:onClick="onClick"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="50dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgCart"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_centerInParent="true"
android:src="@mipmap/icon_cart"/>
<TextView
android:id="@+id/tvSelectNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/circle_red"
android:gravity="center"
android:text="1"
android:textColor="#fff"
android:textSize="12sp"
android:visibility="gone"/>
</RelativeLayout>
<TextView
android:id="@+id/tvCountPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:text="¥0"
android:textColor="#fff"
android:textSize="14sp"/>
<TextView
android:id="@+id/tvSendPrice"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:gravity="center"
android:text="另需配送费¥4"
android:textColor="#fff"
android:textSize="14sp"/>
<TextView
android:id="@+id/tvDeliveryFee"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="¥30元起送"
android:textColor="#fff"
android:textSize="14sp"/>
<TextView
android:id="@+id/tvSubmit"
android:layout_width="90dp"
android:layout_height="match_parent"
android:background="#22c222"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:text="去结算"
android:textColor="#fff"
android:textSize="18sp"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
效果如下: