// JSONViewAdapter.kt/** * Handle the styling of the right part of the json item view (i.e., the part that shows the value). * 处理JsonItemView右边部分的样式(即展示值的部分)。 * * @param value The value to be displayed in the json item view.(要在JsonItemView展示的value。) * @param itemView The json item view to be processed.(要处理的JsonItemView对象。) * @param appendComma Whether to append commas.(是否附加逗号。) * @param hierarchy The number of view hierarchies.(View的层次结构数量。)*/privatefunhandleValue(
value:Any?,
itemView:JSONItemView,
appendComma:Boolean,
hierarchy:Int
) {
itemView.showRight(SpannableStringBuilder().apply {
when (value) {
isNumber->// 处理值为Number类型的样式
handleNumberValue(itemView, value)
isBoolean->// 处理值为Boolean类型的样式
handleBooleanValue(itemView, value)
isString->// 处理值为String类型的样式
handleStringValue(itemView, value)
isJSONObject->// 处理值为JSONObject类型的样式
handleJSONObjectValue(itemView, value, appendComma, hierarchy)
isJSONArray->// 处理值为JSONArray类型的样式
handleJSONArrayValue(itemView, value, appendComma, hierarchy)
else->// 处理值为null的样式
handleNullValue(itemView)
}
if (appendComma) append(",")
})
}
// JSONRecyclerView.ktpackagecom.tanjiajun.widgetimportandroid.content.Contextimportandroid.util.AttributeSetimportandroidx.annotation.ColorIntimportandroidx.core.content.ContextCompatimportandroidx.recyclerview.widget.LinearLayoutManagerimportandroidx.recyclerview.widget.RecyclerViewimportcom.tanjiajun.widget.Rimportcom.tanjiajun.widget.JSONViewAdapterimportcom.tanjiajun.widget.DEFAULT_TEXT_SIZE_SPimportorg.json.JSONArrayimportorg.json.JSONObject/** * Created by TanJiaJun on 5/31/21.*/classJSONRecyclerView @JvmOverloads constructor(
context:Context,
attrs:AttributeSet? = null,
defStyleAttr:Int = 0
) : RecyclerView(context, attrs, defStyleAttr) {
privateval adapter =JSONViewAdapter(context)
init {
layoutManager =LinearLayoutManager(context)
setAdapter(adapter)
}
/** * 绑定JSON字符串数据。 * Bind the json string data. * * @param jsonString The json string to bind.(要绑定的JSON字符串。)*/funbindData(jsonString:String) =
adapter.bindData(jsonString)
/** * 绑定JSONObject数据。 * Bind the json object data. * * @param jsonObject The json object to bind.(要绑定的JSONObject。)*/funbindData(jsonObject:JSONObject) =
adapter.bindData(jsonObject)
/** * 绑定JSONArray数据。 * Bind the json array data. * * @param jsonArray The json array to bind.(要绑定的JSONArray。)*/funbindData(jsonArray:JSONArray) =
adapter.bindData(jsonArray)
/** * 设置JsonItemView的样式。 * Set the json item view styles. * * @param textSize The size of all text.(所有文本的大小。) * @param textColor The normal text color.(普通文本的颜色) * @param keyColor The color of the text of type key.(key类型文本的颜色。) * @param stringColor The color of the text of type String.(字符串类型文本的颜色。) * @param numberColor The color of the text of type Number.(Number类型文本的颜色。) * @param booleanColor The color of text of type Boolean.(Boolean类型文本的颜色。) * @param urlColor The color of url text.(url文本的颜色。) * @param nullColor The color of null text.(null文本的颜色。)*/
@JvmOverloads
funsetStyles(
textSize:Float = DEFAULT_TEXT_SIZE_SP,
@ColorInt textColor:Int = ContextCompat.getColor(context, R.color.default_text_color),
@ColorInt keyColor:Int = ContextCompat.getColor(context, R.color.default_key_color),
@ColorInt stringColor:Int = ContextCompat.getColor(context, R.color.default_string_color),
@ColorInt numberColor:Int = ContextCompat.getColor(context, R.color.default_number_color),
@ColorInt booleanColor:Int = ContextCompat.getColor(
context,
R.color.default_boolean_color
),
@ColorInt urlColor:Int = ContextCompat.getColor(context, R.color.default_url_color),
@ColorInt nullColor:Int = ContextCompat.getColor(context, R.color.default_null_color)
) {
with(adapter) {
this.textSize =when {
textSize <MIN_TEXT_SIZE->MIN_TEXT_SIZE
textSize >MAX_TEXT_SIZE->MAX_TEXT_SIZEelse-> textSize
}
this.textColor = textColor
this.keyColor = keyColor
this.stringColor = stringColor
this.numberColor = numberColor
this.booleanColor = booleanColor
this.urlColor = urlColor
this.nullColor = nullColor
// 刷新列表
notifyDataSetChanged()
}
}
privatecompanionobject {
constvalMIN_TEXT_SIZE=12.0FconstvalMAX_TEXT_SIZE=24.0F
}
}