打开他
buildFeatures {
viewBinding true
}
?
布局是这样的
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:orientation="horizontal"
android:paddingStart="5dp"
android:paddingTop="10dp"
android:paddingEnd="5dp"
android:paddingBottom="10dp">
<ImageView
android:id="@+id/ivIGE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/gift"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvIGEName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:text="木盒"
android:textColor="@color/color_black"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ivIGE" />
<TextView
android:id="@+id/tvIGEPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:text="14000"
android:textColor="@color/color_red"
android:textSize="16sp"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvIGEName" />
<TextView
android:id="@+id/tvIGEIntegral"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:text="@string/integral"
android:textColor="@color/color_red"
android:textSize="14sp"
app:layout_constraintBaseline_toBaselineOf="@id/tvIGEPrice"
app:layout_constraintStart_toEndOf="@id/tvIGEPrice"
app:layout_constraintTop_toBottomOf="@id/tvIGEName" />
<TextView
android:id="@+id/tvButCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:text="@string/sold_by_"
android:textSize="12sp"
android:textColor="@color/color_999999"
app:layout_constrainedWidth="true"
app:layout_constraintBaseline_toBaselineOf="@id/tvIGEPrice"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvIGEIntegral"
app:layout_constraintTop_toBottomOf="@id/tvIGEName" />
</androidx.constraintlayout.widget.ConstraintLayout>
大概
代码是这样的
?
package com.anguomob.market.activity
import android.app.Activity
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.anguomob.total.R
import com.anguomob.total.bean.Goods
import com.anguomob.total.common.AG_QN_PUBLIC
import com.anguomob.total.databinding.ItemGiftExchangeBinding
import com.anguomob.total.utils.AGLogger
import com.anguomob.total.utils.ScreenUtil
import com.bumptech.glide.Glide
import java.math.BigDecimal
import java.math.RoundingMode
class GiftExchangeAdapter(activity: Activity) : RecyclerView.Adapter<ViewHolder>() {
private var activity: Activity = activity
var mDataList: ArrayList<Goods>? = null;
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val data = ItemGiftExchangeBinding.inflate(LayoutInflater.from(parent.context));
return ViewHolder(data.root)
}
var listener: ((Int, Goods) -> Unit)? = null
fun setOnItemClickListener(listener: (Int, Goods) -> Unit) {
this.listener = listener
}
fun format1(value: Double): String {
var bd = BigDecimal(value)
bd = bd.setScale(2, RoundingMode.HALF_UP)
return bd.toString()
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val bind = ItemGiftExchangeBinding.bind(holder.itemView);
val goods = mDataList!!.get(position);
bind.parentView.setOnClickListener {
listener!!(position, goods)
}
val publicityMap = goods.publicity_map;
if (!TextUtils.isEmpty(publicityMap)) {
val res = publicityMap.split("|")
val firstMap = AG_QN_PUBLIC + res[0]
AGLogger.e(TAG, "firstMap $firstMap")
bind.ivIGE.visibility = View.VISIBLE
Glide.with(activity).load(firstMap).into(bind.ivIGE)
} else {
bind.ivIGE.visibility = View.GONE
}
bind.tvIGEName.text = goods.name;
bind.tvIGEPrice.text = (goods.lowest_price * 1000 + 200).toInt().toString();
bind.tvButCount.text =
String.format(activity.resources.getString(R.string.sold_by_), goods.buy_count)
}
fun setData(mNoteList: ArrayList<Goods>) {
mDataList = mNoteList
notifyDataSetChanged()
}
override fun getItemCount(): Int {
return if (mDataList == null) 0 else mDataList!!.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
init {
}
}
companion object {
private const val TAG = "GiftExchangeAdapter"
}
}
简写了findViewById非常好用
|