Koltin46,Android开发知识点

android:text=“蜂鸟专送”

android:textColor="#fff"

android:textSize=“12sp”

android:padding=“3dp”

android:layout_gravity=“center_vertical”

android:background="#468ade"

android:layout_marginRight=“4dp”

/>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“超时秒赔”

android:textColor="#468ade"

android:textSize=“12sp”

android:layout_gravity=“center_vertical”

/>

<ImageView

android:layout_width=“10dp”

android:layout_height=“10dp”

android:layout_alignParentRight=“true”

android:layout_marginTop=“10dp”

android:layout_marginBottom=“10dp”

android:layout_marginLeft=“10dp”

android:layout_marginRight=“20dp”

android:layout_centerVertical=“true”

android:background="@mipmap/bk_icon_arrow_small"/>

<View

android:layout_width=“match_parent”

android:layout_height=“7dp”

android:background="#fdd9d7d7"/>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“40dp”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:layout_marginLeft=“20dp”

android:textColor="#000"

android:text=“支付方式”

android:textSize=“15sp”/>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:layout_marginLeft=“20dp”

android:textColor="#000"

android:text=“在线支付”

android:layout_toLeftOf="@id/iv_arrow"

android:textSize=“12sp”/>

<ImageView

android:id="@id/iv_arrow"

android:layout_width=“10dp”

android:layout_height=“10dp”

android:layout_alignParentRight=“true”

android:layout_marginTop=“10dp”

android:layout_marginBottom=“10dp”

android:layout_marginLeft=“10dp”

android:layout_marginRight=“20dp”

android:layout_centerVertical=“true”

android:background="@mipmap/bk_icon_arrow_small"/>

<View

android:layout_width=“match_parent”

android:layout_height=“1dp”

android:background="#fdd9d7d7"/>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“40dp”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:layout_marginLeft=“20dp”

android:textColor="#000"

android:text=“红包”

android:textSize=“15sp”/>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:layout_marginLeft=“20dp”

android:textColor="#fdd9d7d7"

android:text=“无红包可用”

android:enabled=“false”

android:layout_toLeftOf="@id/iv_arrow"

android:textSize=“12sp”/>

<ImageView

android:id="@id/iv_arrow"

android:layout_width=“10dp”

android:layout_height=“10dp”

android:layout_alignParentRight=“true”

android:layout_marginTop=“10dp”

android:layout_marginBottom=“10dp”

android:layout_marginLeft=“10dp”

android:layout_marginRight=“20dp”

android:layout_centerVertical=“true”

android:background="@mipmap/bk_icon_arrow_small"/>

<View

android:layout_width=“match_parent”

android:layout_height=“1dp”

android:background="#fdd9d7d7"/>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“40dp”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:layout_marginLeft=“20dp”

android:textColor="#000"

android:text=“商家代金券”

android:textSize=“15sp”/>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:layout_marginLeft=“20dp”

android:textColor="#000"

android:text=“没有商家代金券可用”

android:layout_toLeftOf="@id/iv_arrow"

android:textSize=“12sp”/>

<ImageView

android:id="@id/iv_arrow"

android:layout_width=“10dp”

android:layout_height=“10dp”

android:layout_alignParentRight=“true”

android:layout_marginTop=“10dp”

android:layout_marginBottom=“10dp”

android:layout_marginLeft=“10dp”

android:layout_marginRight=“20dp”

android:layout_centerVertical=“true”

android:background="@mipmap/bk_icon_arrow_small"/>

<View

android:layout_width=“match_parent”

android:layout_height=“7dp”

android:background="#fdd9d7d7"/>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“40dp”>

<ImageView

android:id="@+id/iv_icon"

android:layout_width=“15dp”

android:layout_height=“15dp”

android:layout_marginLeft=“20dp”

android:layout_marginRight=“10dp”

android:layout_centerVertical=“true”

android:background="@mipmap/ic_launcher"/>

<TextView

android:id="@+id/tv_seller_name"

android:layout_toRightOf="@+id/iv_icon"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:textColor="#000"

android:text=“田老师红烧肉(海淀东北旺西路店)”

android:maxLines=“1”

android:textSize=“15sp”/>

<View

android:layout_width=“match_parent”

android:layout_height=“1dp”

android:background="#fdd9d7d7"/>

<LinearLayout

android:id="@+id/ll_select_goods"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“vertical”>

<View

android:layout_width=“match_parent”

android:layout_height=“1dp”

android:background="#fdd9d7d7"/>

<RelativeLayout

android:layout_width=“match_parent”

android:layout_height=“40dp”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:layout_marginLeft=“20dp”

android:textColor="#000"

android:text=“配送费”

android:textSize=“12sp”/>

<TextView

android:id="@+id/tv_deliveryFee"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:layout_marginLeft=“20dp”

android:layout_marginRight=“20dp”

android:textColor="#000"

android:layout_alignParentRight=“true”

android:text=“¥4”

android:textSize=“12sp”/>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“50dp”

android:background="#aa000000"

android:clickable=“true”

android:gravity=“center_vertical”

android:orientation=“horizontal”>

<TextView

android:id="@+id/tv_CountPrice"

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”

android:layout_marginLeft=“20dp”

android:gravity=“center_vertical”

android:text=“待支付¥0”

android:textColor="#fff"

android:textSize=“18sp” />

<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”

/>

RecepitAddressActivity.kt收货地址的主界面,底下有一个新增地址的按钮,添加成功以后onRestart方法更新数据展示列表

package com.example.takeout.ui.activity

import android.content.Intent

import android.os.Bundle

import android.util.TypedValue

import androidx.appcompat.app.AppCompatActivity

import androidx.recyclerview.widget.LinearLayoutManager

import com.example.takeout.R

import com.example.takeout.model.beans.RecepitAddressBean

import com.example.takeout.model.dao.AddressDao

import com.example.takeout.ui.adapter.AddressRvAdapter

import com.example.takeout.ui.view.RecycleViewDivider

import com.example.takeout.utils.CommonUtil

import kotlinx.android.synthetic.main.activity_address_list.*

class RecepitAddressActivity : AppCompatActivity() {

lateinit var addressDao: AddressDao

lateinit var adapter: AddressRvAdapter

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_address_list)

addressDao = AddressDao(this)

rv_receipt_address.layoutManager = LinearLayoutManager(this)

//rv_receipt_address增加分割线

rv_receipt_address.addItemDecoration(

RecycleViewDivider(

this,

LinearLayoutManager.HORIZONTAL

)

)

adapter = AddressRvAdapter(this)

rv_receipt_address.adapter = adapter

if (CommonUtil.checkDeviceHasNavigationBar(this)) {

activity_address_list.setPadding(0, 0, 0, 48.dp2px())

}

tv_add_address.setOnClickListener {

val intent : Intent = Intent(this, AddOrEditAddressActivity::class.java)

startActivity(intent)

}

}

/**

  • onRestart中获取地址

*/

override fun onStart() {

super.onStart()

val addressList = addressDao.queryAllAddress()

if (addressList.isNotEmpty()) {

// toast(“一共有” + addressList.size + “个地址”)

adapter.setAddList(addressList as ArrayList)

}

}

fun Int.dp2px(): Int {

return TypedValue.applyDimension(

TypedValue.COMPLEX_UNIT_DIP,

toFloat(), resources.displayMetrics

).toInt()

}

}

AddOrEditAddressActivity.kt新增地址的操作界面,涉及到增删改查

package com.example.takeout.ui.activity

import android.content.DialogInterface

import android.content.Intent

import android.graphics.Color

import android.os.Bundle

import android.text.Editable

import android.text.TextUtils

import android.text.TextWatcher

import android.util.TypedValue

import android.view.View

import android.widget.Toast

import androidx.appcompat.app.AlertDialog

import androidx.appcompat.app.AppCompatActivity

import com.example.takeout.R

import com.example.takeout.model.beans.RecepitAddressBean

import com.example.takeout.model.dao.AddressDao

import com.example.takeout.utils.CommonUtil

import kotlinx.android.synthetic.main.activity_add_edit_receipt_address.*

import org.jetbrains.anko.toast

class AddOrEditAddressActivity : AppCompatActivity(), View.OnClickListener {

override fun onClick(v: View?) {

when (v?.id) {

R.id.ib_back -> finish()

R.id.ib_add_phone_other -> rl_phone_other.visibility = View.VISIBLE

R.id.ib_delete_phone -> et_phone.setText("")

R.id.ib_delete_phone_other -> et_phone_other.setText("")

R.id.ib_select_label -> selectLabel()

R.id.btn_ok -> {

val isOk = checkReceiptAddressInfo()

if (isOk) {

if (intent.hasExtra(“addressBean”)) {

updateAddress()

}else {

//新增地址

insertAddress()

}

}

}

R.id.btn_location_address -> {

// val intent = Intent(this, MapLocationActivity::class.java)

// startActivityForResult(intent, 1001)

}

}

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

super.onActivityResult(requestCode, resultCode, data)

if(resultCode == 200){

if(data!=null) {

val title = data.getStringExtra(“title”)

val address = data.getStringExtra(“address”)

et_receipt_address.setText(title)

et_detail_address.setText(address)

}

}

}

private fun updateAddress() {

var username = et_name.text.toString().trim()

var sex = “女士”

if (rb_man.isChecked) {

sex = “先生”

}

var phone = et_phone.text.toString().trim()

var phoneOther = et_phone_other.text.toString().trim()

var address = et_receipt_address.text.toString().trim()

var detailAddress = et_detail_address.text.toString().trim()

var label = tv_label.text.toString()

addressBean.username = username

addressBean.sex = sex

addressBean.phone = phone

addressBean.phoneOther = phoneOther

addressBean.address = address

addressBean.detailAddress = detailAddress

addressBean.label = label

addressDao.updateRecepitAddressBean(addressBean)

toast(“更新地址成功”)

finish()

}

private fun insertAddress() {

var username = et_name.text.toString().trim()

var sex = “女士”

if (rb_man.isChecked) {

sex = “先生”

}

var phone = et_phone.text.toString().trim()

var phoneOther = et_phone_other.text.toString().trim()

var address = et_receipt_address.text.toString().trim()

var detailAddress = et_detail_address.text.toString().trim()

var label = tv_label.text.toString()

addressDao.addRecepitAddressBean(RecepitAddressBean(999, username, sex, phone, phoneOther, address, detailAddress, label, “38”))

toast(“新增地址成功”)

finish()

}

val titles = arrayOf(“无”, “家”, “学校”, “公司”)

val colors = arrayOf("#778899", “#ff3399”, “#ff9933”, “#33ff99”)

lateinit var addressDao: AddressDao

private fun selectLabel() {

val builder = AlertDialog.Builder(this)

builder.setTitle(“请选择地址标签”)

builder.setItems(titles, object : DialogInterface.OnClickListener {

override fun onClick(dialog: DialogInterface?, which: Int) {

tv_label.text = titles[which].toString()

tv_label.setBackgroundColor(Color.parseColor(colors[which]))

tv_label.setTextColor(Color.BLACK)

}

})

builder.show()

}

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_add_edit_receipt_address)

processIntent()

addressDao = AddressDao(this)

if (CommonUtil.checkDeviceHasNavigationBar(this)) {

activity_add_address.setPadding(0, 0, 0, 48.dp2px())

}

btn_location_address.setOnClickListener(this)

ib_back.setOnClickListener(this)

ib_add_phone_other.setOnClickListener(this)

ib_delete_phone.setOnClickListener(this)

ib_delete_phone_other.setOnClickListener(this)

ib_select_label.setOnClickListener(this)

btn_ok.setOnClickListener(this)

et_phone.addTextChangedListener(object : TextWatcher {

override fun afterTextChanged(s: Editable?) {

if (!TextUtils.isEmpty(s)) {

ib_delete_phone.visibility = View.VISIBLE

} else {

ib_delete_phone.visibility = View.INVISIBLE

}

}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

}

})

et_phone_other.addTextChangedListener(object : TextWatcher {

override fun afterTextChanged(s: Editable?) {

if (!TextUtils.isEmpty(s)) {

ib_delete_phone_other.visibility = View.VISIBLE

} else {

ib_delete_phone_other.visibility = View.INVISIBLE

}

}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

}

})

}

lateinit var addressBean: RecepitAddressBean

private fun processIntent() {

if (intent.hasExtra(“addressBean”)) {

addressBean = intent.getSerializableExtra(“addressBean”) as RecepitAddressBean

tv_title.text = “修改地址”

ib_delete.visibility = View.VISIBLE

ib_delete.setOnClickListener {

addressDao.deleteRecepitAddressBean(addressBean)

toast(“删除此地址成功”)

finish()

}

et_name.setText(addressBean.username)

val sex = addressBean.sex

if (“先生”.equals(sex)) {

rb_man.isChecked = true

} else {

rb_women.isChecked = true

}

et_phone.setText(addressBean.phone)

et_phone_other.setText(addressBean.phoneOther)

et_receipt_address.setText(addressBean.address)

et_detail_address.setText(addressBean.detailAddress)

tv_label.text = addressBean.label

}

}

fun Int.dp2px(): Int {

return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,

toFloat(), resources.displayMetrics).toInt()

}

fun checkReceiptAddressInfo(): Boolean {

val name = et_name.getText().toString().trim()

if (TextUtils.isEmpty(name)) {

Toast.makeText(this, “请填写联系人”, Toast.LENGTH_SHORT).show()

return false

}

val phone = et_phone.getText().toString().trim()

if (TextUtils.isEmpty(phone)) {

Toast.makeText(this, “请填写手机号码”, Toast.LENGTH_SHORT).show()

return false

}

if (!isMobileNO(phone)) {

Toast.makeText(this, “请填写合法的手机号”, Toast.LENGTH_SHORT).show()

return false

}

val receiptAddress = et_receipt_address.getText().toString().trim()

if (TextUtils.isEmpty(receiptAddress)) {

Toast.makeText(this, “请填写收获地址”, Toast.LENGTH_SHORT).show()

return false

}

val address = et_detail_address.getText().toString().trim()

if (TextUtils.isEmpty(address)) {

Toast.makeText(this, “请填写详细地址”, Toast.LENGTH_SHORT).show()

return false

}

return true

}

fun isMobileNO(phone: String): Boolean {

val telRegex = “[1][358]\d{9}”//"[1]“代表第1位为数字1,”[358]“代表第二位可以为3、5、8中的一个,”\d{9}"代表后面是可以是0~9的数字,有9位。

return phone.matches(telRegex.toRegex())

}

}

AddressDao.kt地址数据的增删改查操作

package com.example.takeout.model.dao

import android.content.Context

import android.util.Log

import com.example.takeout.model.beans.RecepitAddressBean

import com.j256.ormlite.dao.Dao

class AddressDao(val context: Context) {

lateinit var addressDao: Dao<RecepitAddressBean, Int>

init {

val openHelper = TakeoutOpenHelper(context)

addressDao = openHelper.getDao(RecepitAddressBean::class.java)

}

fun addRecepitAddressBean(bean: RecepitAddressBean){

try{

addressDao.create(bean)

}catch (e : Exception){

Log.e(“addressBean”, e.localizedMessage)

}

}

fun deleteRecepitAddressBean(bean: RecepitAddressBean){

try{

addressDao.delete(bean)

}catch (e : Exception){

Log.e(“addressBean”, e.localizedMessage)

}

}

fun updateRecepitAddressBean(bean: RecepitAddressBean){

try{

addressDao.update(bean)

}catch (e : Exception){

Log.e(“addressBean”, e.localizedMessage)

}

}

fun queryAllAddress():List{

try{

return addressDao.queryForAll()

}catch (e : Exception){

Log.e(“addressBean”, e.localizedMessage)

return ArrayList()

}

}

}

RecepitAddressBean.kt地址数据库的bean文件,作为bean进行增删改查的操作

package com.example.t
akeout.model.beans

import com.j256.ormlite.field.DatabaseField

import com.j256.ormlite.table.DatabaseTable

import java.io.Serializable

@DatabaseTable(tableName = “t_address”) class RecepitAddressBean() : Serializable {

@DatabaseField(generatedId = true)

var id: Int = 0

@DatabaseField(columnName = “username”)

var username: String = “”

@DatabaseField(columnName = “sex”)

var sex: String = “女”

@DatabaseField(columnName = “phone”)

var phone: String = “”

@DatabaseField(columnName = “phoneOther”)

var phoneOther: String = “”

@DatabaseField(columnName = “address”)

var address: String = “”

@DatabaseField(columnName = “detailAddress”)

var detailAddress: String = “”

@DatabaseField(columnName = “label”)

var label = “”

@DatabaseField(columnName = “userId”)

var userId: String = “38”

constructor(id: Int, username: String, sex: String, phone: String, phoneOther: String,

address: String, detailAddress: String, label: String, userId: String) : this() {

this.id = id

this.username = username

this.sex = sex

this.phone = phone

this.phoneOther = phoneOther

this.address = address

this.detailAddress = detailAddress

this.label = label

this.userId = userId

}

}

TakeoutOpenHelper.kt创建新的数据库用来维护地址的列表,需要更新数据库的版本号

package com.example.takeout.model.dao

import android.content.Context

import android.database.sqlite.SQLiteDatabase

import com.example.takeout.beans.User

import com.example.takeout.model.beans.RecepitAddressBean

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper

import com.j256.ormlite.support.ConnectionSource

import com.j256.ormlite.table.TableUtils

/**

  • app版本 数据库版本

  • 1.1版本 1 用户登录

  • 1.3版本 2 地址管理

*/

class TakeoutOpenHelper(val context: Context) : OrmLiteSqliteOpenHelper(context, “takeout_kotlin.db”, null, 2) {

override fun onCreate(p0: SQLiteDatabase?, connectionSource: ConnectionSource?) {

//创建user表

TableUtils.createTable(connectionSource, User::class.java)

//创建地址表

TableUtils.createTable(connectionSource, RecepitAddressBean::class.java)

}

override fun onUpgrade(p0: SQLiteDatabase?, p1: ConnectionSource?, oldVersion: Int, newVersion: Int) {

//升级app的用户会执行此方法

TableUtils.createTable(connectionSource, RecepitAddressBean::class.java)

}

}

AddressRvAdapter.kt增加地址以后展示地址列表的界面,使用RecycleView实现

package com.example.takeout.ui.adapter

import android.annotation.SuppressLint

import android.app.Activity

import android.content.Context

import android.content.Intent

import android.view.LayoutInflater

import android.view.View

import android.view.ViewGroup

import android.widget.ImageView

import android.widget.TextView

import androidx.recyclerview.widget.RecyclerView

import com.example.takeout.R

import com.example.takeout.model.beans.RecepitAddressBean

import com.example.takeout.ui.activity.AddOrEditAddressActivity

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

var addressList = arrayListOf()

fun setAddList(list: ArrayList) {

this.addressList = list

notifyDataSetChanged()

}

override fun getItemCount(): Int {

return addressList.size

}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

(holder as AddressItemHolder).bindData(addressList.get(position))

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

val itemView = LayoutInflater.from(context).inflate(R.layout.item_receipt_address,parent,false)

return AddressItemHolder(itemView)

}

inner class AddressItemHolder(itemView: View) :RecyclerView.ViewHolder(itemView){

val ivEdit: ImageView

val tvname: TextView

val tv_sex:TextView

val tv_phone:TextView

val tv_label:TextView

val tv_address:TextView

lateinit var address : RecepitAddressBean

init {

ivEdit = itemView.findViewById(R.id.iv_edit) as ImageView

ivEdit.setOnClickListener{

val intent = Intent(context, AddOrEditAddressActivity::class.java)

intent.putExtra(“addressBean”, address)

context.startActivity(intent)

}

tvname = itemView.findViewById(R.id.tv_name) as TextView

tv_sex = itemView.findViewById(R.id.tv_sex) as TextView

tv_phone = itemView.findViewById(R.id.tv_phone) as TextView

tv_label = itemView.findViewById(R.id.tv_label) as TextView

tv_address = itemView.findViewById(R.id.tv_address) as TextView

itemView.setOnClickListener {

val data = Intent()

data.putExtra(“address”, address)

(context as Activity).setResult(200, data)

(context as Activity).finish()

}

}

@SuppressLint(“SetTextI18n”)

fun bindData(address: RecepitAddressBean) {

this.address = address

tvname.text = address.username

tv_sex.text = address.sex

tv_phone.text = address.phone + “,” + address.phoneOther

tv_address.text = “ a d d r e s s . a d d r e s s , {address.address}, address.address,{address.detailAddress}”

tv_label.text = address.label

}

}

}

RecycleViewDivider.kt给RecycleView增加分隔线

package com.example.takeout.ui.view

import android.content.Context

import android.graphics.Canvas

import android.graphics.Paint

import android.graphics.Rect

import android.graphics.drawable.Drawable

import android.view.View

import androidx.core.content.ContextCompat

import androidx.recyclerview.widget.LinearLayoutManager

import androidx.recyclerview.widget.RecyclerView

class RecycleViewDivider

/**

  • 默认分割线:高度为2px,颜色为灰色

  • @param context

  • @param orientation 列表方向

*/

(context: Context, private val mOrientation: Int//列表的方向:LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL

) : RecyclerView.ItemDecoration() {

private var mPaint: Paint? = null

private var mDivider: Drawable? = null

private var mDividerHeight = 2//分割线高度,默认为1px

init {

if (mOrientation != LinearLayoutManager.VERTICAL && mOrientation != LinearLayoutManager.HORIZONTAL) {

throw IllegalArgumentException(“请输入正确的参数!”)

}

val a = context.obtainStyledAttributes(ATTRS)

mDivider = a.getDrawable(0)

a.recycle()

}

/**

  • 自定义分割线

  • @param context

  • @param orientation 列表方向

  • @param drawableId 分割线图片

*/

constructor(context: Context, orientation: Int, drawableId: Int) : this(context, orientation) {

mDivider = ContextCompat.getDrawable(context, drawableId)

mDividerHeight = mDivider!!.intrinsicHeight

}

/**

  • 自定义分割线

  • @param context

  • @param orientation 列表方向

  • @param dividerHeight 分割线高度

  • @param dividerColor 分割线颜色

*/

constructor(context: Context, orientation: Int, dividerHeight: Int, dividerColor: Int) : this(context, orientation) {

mDividerHeight = dividerHeight

mPaint = Paint(Paint.ANTI_ALIAS_FLAG)

mPaint!!.color = dividerColor

mPaint!!.style = Paint.Style.FILL

}

//获取分割线尺寸

override fun getItemOffsets(

outRect: Rect,

view: View,

parent: RecyclerView,

state: RecyclerView.State

) {

super.getItemOffsets(outRect, view, parent, state)

outRect.set(0, 0, 0, mDividerHeight)

}

//绘制分割线

override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {

super.onDraw(c, parent, state)

if (mOrientation == LinearLayoutManager.VERTICAL) {

drawVertical(c, parent)

} else {

drawHorizontal(c, parent)

}

}

//绘制横向 item 分割线

private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {

val left = parent.paddingLeft

val right = parent.measuredWidth - parent.paddingRight

val childSize = parent.childCount

for (i in 0…childSize - 1) {

val child = parent.getChildAt(i)

val layoutParams = child.layoutParams as RecyclerView.LayoutParams

val top = child.bottom + layoutParams.bottomMargin

val bottom = top + mDividerHeight

if (mDivider != null) {

mDivider!!.setBounds(left, top, right, bottom)

mDivider!!.draw(canvas)

}

if (mPaint != null) {

canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(),

mPaint!!

)

}

}

}

//绘制纵向 item 分割线

private fun drawVertical(canvas: Canvas, parent: RecyclerView) {

val top = parent.paddingTop

val bottom = parent.measuredHeight - parent.paddingBottom

val childSize = parent.childCount

for (i in 0…childSize - 1) {

val child = parent.getChildAt(i)

val layoutParams = child.layoutParams as RecyclerView.LayoutParams

val left = child.right + layoutParams.rightMargin

val right = left + mDividerHeight

if (mDivider != null) {

mDivider!!.setBounds(left, top, right, bottom)

mDivider!!.draw(canvas)

}

if (mPaint != null) {

canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(),

mPaint!!

)

}

}

}

companion object {

private val ATTRS = intArrayOf(android.R.attr.listDivider)

}

}

item_receipt_address.xml单个收获地址的展示item布局

<?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=“80dp”

android:background="#fff">

<LinearLayout

android:layout_toLeftOf="@+id/iv_edit"

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_marginTop=“10dp”

android:layout_marginBottom=“10dp”

android:layout_marginLeft=“20dp”

android:layout_marginRight=“20dp”

android:orientation=“vertical”>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:id="@+id/tv_name"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“20sp”

android:textColor="#000"

android:textStyle=“bold”

android:text=“黑马程序员”/>

<TextView

android:id="@+id/tv_sex"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“15sp”

android:textColor="#fd282626"

android:layout_marginLeft=“5dp”

android:layout_marginRight=“5dp”

android:layout_gravity=“center_vertical”

android:text=“先生”/>

<TextView

android:id="@+id/tv_phone"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“15sp”

android:textColor="#fd282626"

android:layout_gravity=“center_vertical”

android:text=“13787006927,18989898989”

android:maxLines=“1”/>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginTop=“5dp”

android:orientation=“horizontal”>

<TextView

android:id="@+id/tv_label"

android:layout_width=“30dp”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:maxLines=“1”

android:padding=“3dp”

android:textSize=“10sp”

android:background="#fff"

android:gravity=“center”

android:text=""

android:textColor="#000"

android:layout_marginRight=“10dp”

/>

<TextView

android:id="@+id/tv_address"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“12sp”

android:textColor="#fd9b9999"

android:text=“中关村软件园”/>

<ImageView

android:id="@+id/iv_edit"

android:layout_width=“20dp”

android:layout_height=“20dp”

android:layout_alignParentRight=“true”

android:layout_marginTop=“10dp”

android:layout_marginBottom=“10dp”

android:layout_marginRight=“20dp”

android:layout_centerVertical=“true”

android:background="@mipmap/address_icon_edit"/>

activity_add_edit_receipt_address.xml增加地址的界面

eight=“wrap_content”

android:textSize=“20sp”

android:textColor="#000"

android:textStyle=“bold”

android:text=“黑马程序员”/>

<TextView

android:id="@+id/tv_sex"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“15sp”

android:textColor="#fd282626"

android:layout_marginLeft=“5dp”

android:layout_marginRight=“5dp”

android:layout_gravity=“center_vertical”

android:text=“先生”/>

<TextView

android:id="@+id/tv_phone"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“15sp”

android:textColor="#fd282626"

android:layout_gravity=“center_vertical”

android:text=“13787006927,18989898989”

android:maxLines=“1”/>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginTop=“5dp”

android:orientation=“horizontal”>

<TextView

android:id="@+id/tv_label"

android:layout_width=“30dp”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:maxLines=“1”

android:padding=“3dp”

android:textSize=“10sp”

android:background="#fff"

android:gravity=“center”

android:text=""

android:textColor="#000"

android:layout_marginRight=“10dp”

/>

<TextView

android:id="@+id/tv_address"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“12sp”

android:textColor="#fd9b9999"

android:text=“中关村软件园”/>

<ImageView

android:id="@+id/iv_edit"

android:layout_width=“20dp”

android:layout_height=“20dp”

android:layout_alignParentRight=“true”

android:layout_marginTop=“10dp”

android:layout_marginBottom=“10dp”

android:layout_marginRight=“20dp”

android:layout_centerVertical=“true”

android:background="@mipmap/address_icon_edit"/>

activity_add_edit_receipt_address.xml增加地址的界面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值