Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
shengqf committed Aug 1, 2019
1 parent 9ab3572 commit 87c0938
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
20 changes: 10 additions & 10 deletions app/src/main/java/com/shengqf/bottompw/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.shengqf.bottompw

import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.shengqf.view.bottom_pw.BottomPW
Expand All @@ -20,19 +19,19 @@ class MainActivity : AppCompatActivity() {
if (mBottomPW == null) {
mBottomPW = BottomPW(this)
.setList(mList)
.setPwPadding(10)//BottomPW的内边距,单位dp(默认是10dp)
.setPwPadding(10f)//BottomPW的内边距,单位dp(默认是10dp)
.setCornerRadius(10)//圆角大小,单位dp(默认是10dp)
.setShowTitleTv(false)//是否显示列表上方的标题,默认不显示
.setTitleStr("请选择")//列表上方的标题内容,默认为
.setTitleHeight(49)//列表上方的标题高度,单位dp(默认是49dp)
.setTitleColor(ContextCompat.getColor(this@MainActivity, R.color.text_tips))//列表上方的标题字体颜色,默认是ContextCompat.getColor(mActivity, R.color.text_tips),text_tips:#999
.setShowTitleTv(true)//是否显示列表上方的标题,默认不显示
.setTitleStr("请选择")//列表上方的标题内容,默认为"请选择"
.setTitleHeight(49f)//列表上方的标题高度,单位dp(默认是49dp)
.setTitleColor(R.color.red)//列表上方的标题字体颜色,默认颜色是:#999999
.setTitleSize(16)//列表上方的标题字体大小,默认是16sp
.setItemHeight(49)//列表Item的高度,单位dp(默认是49dp)
.setItemTextColor(ContextCompat.getColor(this@MainActivity, R.color.text_primary))//列表Item的字体颜色,默认是ContextCompat.getColor(mActivity, R.color.text_primary),text_primary:#333
.setItemHeight(49f)//列表Item的高度,单位dp(默认是49dp)
.setItemTextColor(R.color.blue)//列表Item的字体颜色,默认颜色是:#333333
.setItemTextSize(16)//列表Item的字体大小,默认是16sp
.setShowCancelTv(true)//是否显示底部的cancel按钮,默认显示
.setCancelHeight(49)//cancel按钮的高度,单位dp(默认是49dp)
.setCancelTextColor(ContextCompat.getColor(this@MainActivity, R.color.text_tips))//cancel按钮的字体颜色,默认是ContextCompat.getColor(mActivity, R.color.text_tips),text_tips:#999
.setCancelHeight(49f)//cancel按钮的高度,单位dp(默认是49dp)
.setCancelTextColor(R.color.text_primary)//cancel按钮的字体颜色,默认颜色是:#999999
.setCancelTextSize(16)//cancel按钮字体大小,默认是16sp
.setCancelTextStr("取消")//默认文字是"取消"
.setOnPwItemClickListener(getOnPwItemClickListener())//列表Item点击事件
Expand All @@ -58,4 +57,5 @@ class MainActivity : AppCompatActivity() {
}
}
}

}
48 changes: 24 additions & 24 deletions view/src/main/java/com/shengqf/view/bottom_pw/BottomPW.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ class BottomPW(activity: Activity) {
private var mOnPwItemClickListener: OnPwItemClickListener? = null
private var mOnPwDismissListener: OnPwDismissListener? = null

private var mPwPadding: Int = dp2px(10f)//Pw默认padding10dp
private var mCornerRadius: Int = dp2px(10f)//圆角默认大小10dp
private var mShowTitleTv = false //是否显示标题
private var mTitleStr: String = "请选择" //标题字体内容
private var mTitleHeight: Int = dp2px(49f) //title的行高
private var mTitleColor: Int = ContextCompat.getColor(mActivity, R.color.text_tips) //title字体颜色
private var mTitleSize: Int = 16 //title字体大小
private var mItemHeight: Int = dp2px(49f) //列表Item的行高
private var mItemTextColor: Int = ContextCompat.getColor(mActivity, R.color.text_primary) //列表Item的字体颜色
private var mItemTextSize: Int = 16 //列表Item的字体大小
private var mShowCancelTv = true //是否显示底部的取消
private var mCancelHeight: Int = dp2px(49f) //取消字体的行高
private var mCancelTextColor: Int = ContextCompat.getColor(mActivity, R.color.text_tips) //取消字体的颜色
private var mCancelTextSize: Int = 16 //取消字体的大小
private var mCancelTextStr: String = "取消" //取消字体的内容
private var mPwPadding: Float = 10f//Pw默认padding,,默认为10dp
private var mCornerRadius: Int = 10//圆角大小,默认为10dp
private var mShowTitleTv = false //是否显示标题,默认不显示
private var mTitleStr: String = "请选择" //标题字体内容,默认为"请选择"
private var mTitleHeight: Float = 49f //title的行高,默认为49dp
private var mTitleColor: Int = R.color.text_tips //title字体颜色
private var mTitleSize: Int = 16 //title字体大小,默认是16sp
private var mItemHeight: Float = 49f //列表Item的行高,默认是49dp
private var mItemTextColor: Int = R.color.text_primary //列表Item的字体颜色
private var mItemTextSize: Int = 16 //列表Item的字体大小,默认是16sp
private var mShowCancelTv = true //是否显示底部的取消,默认显示
private var mCancelHeight: Float = 49f //底部取消按钮的行高,默认49dp
private var mCancelTextColor: Int = R.color.text_tips //取消字体的颜色
private var mCancelTextSize: Int = 16 //取消字体的大小,默认是16sp
private var mCancelTextStr: String = "取消" //取消字体的内容,默认为"取消"

//设置列表数据源
fun setList(list: ArrayList<String>): BottomPW {
Expand All @@ -75,7 +75,7 @@ class BottomPW(activity: Activity) {
return this
}

fun setPwPadding(pwPadding: Int): BottomPW {
fun setPwPadding(pwPadding: Float): BottomPW {
this.mPwPadding = pwPadding
return this
}
Expand All @@ -95,7 +95,7 @@ class BottomPW(activity: Activity) {
return this
}

fun setTitleHeight(titleHeight: Int): BottomPW {
fun setTitleHeight(titleHeight: Float): BottomPW {
this.mTitleHeight = titleHeight
return this
}
Expand All @@ -110,7 +110,7 @@ class BottomPW(activity: Activity) {
return this
}

fun setItemHeight(itemHeight: Int): BottomPW {
fun setItemHeight(itemHeight: Float): BottomPW {
this.mItemHeight = itemHeight
return this
}
Expand All @@ -130,7 +130,7 @@ class BottomPW(activity: Activity) {
return this
}

fun setCancelHeight(cancelHeight: Int): BottomPW {
fun setCancelHeight(cancelHeight: Float): BottomPW {
this.mCancelHeight = cancelHeight
return this
}
Expand Down Expand Up @@ -169,7 +169,7 @@ class BottomPW(activity: Activity) {
private fun initView() {
mContentView = LayoutInflater.from(mActivity).inflate(R.layout.layout_pw_bottom, null)
//pw内边距
mContentView.setPadding(mPwPadding, mPwPadding, mPwPadding, mPwPadding)
mContentView.setPadding(dp2px(mPwPadding), dp2px(mPwPadding), dp2px(mPwPadding), dp2px(mPwPadding))

with(mContentView) {
//设置圆角
Expand All @@ -180,9 +180,9 @@ class BottomPW(activity: Activity) {
if (mShowTitleTv) {
view_stub.inflate()
title_tv.text = mTitleStr
title_tv.layoutParams.height = mTitleHeight
title_tv.layoutParams.height = dp2px(mTitleHeight)
title_tv.textSize = mTitleSize.toFloat()
title_tv.setTextColor(mTitleColor)
title_tv.setTextColor(ContextCompat.getColor(mActivity,mTitleColor))
}

//列表
Expand All @@ -192,9 +192,9 @@ class BottomPW(activity: Activity) {

//取消按钮
cancel_tv.visibility = if (mShowCancelTv) View.VISIBLE else View.GONE
cancel_tv.setTextColor(mCancelTextColor)
cancel_tv.setTextColor(ContextCompat.getColor(mActivity,mCancelTextColor))
cancel_tv.textSize = mCancelTextSize.toFloat()
cancel_tv.layoutParams.height = mCancelHeight
cancel_tv.layoutParams.height = dp2px(mCancelHeight)
cancel_tv.text = mCancelTextStr
cancel_tv.setOnClickListener {
mPopupWindow.dismiss()
Expand Down
10 changes: 5 additions & 5 deletions view/src/main/java/com/shengqf/view/bottom_pw/PwAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class PwAdapter constructor(context: Context) : RecyclerView.Adapter<PwAdapter.P
return this
}

private var mItemHeight: Int = dp2px(49f) //列表Item的行高
private var mItemTextColor: Int = ContextCompat.getColor(mContext,R.color.text_primary) //列表Item的字体颜色
private var mItemHeight: Float = 49f //列表Item的行高
private var mItemTextColor: Int = R.color.text_primary //列表Item的字体颜色
private var mItemTextSize: Int = 16 //列表Item的字体大小

fun setItemHeight(itemHeight: Int): PwAdapter {
fun setItemHeight(itemHeight: Float): PwAdapter {
this.mItemHeight = itemHeight
return this
}
Expand Down Expand Up @@ -65,9 +65,9 @@ class PwAdapter constructor(context: Context) : RecyclerView.Adapter<PwAdapter.P
override fun onBindViewHolder(holder: PwViewHolder, position: Int) {
with(holder.itemView) {
item_tv.text = mList[position]
item_tv.setTextColor(mItemTextColor)
item_tv.setTextColor(ContextCompat.getColor(mContext,mItemTextColor))
item_tv.textSize = mItemTextSize.toFloat()
item_tv.layoutParams.height = mItemHeight
item_tv.layoutParams.height = dp2px(mItemHeight)
divider.visibility = if (position == mList.size - 1) View.GONE else View.VISIBLE
item_tv.setOnClickListener {
mOnItemClickListener?.onItemClick(position)
Expand Down

0 comments on commit 87c0938

Please sign in to comment.