Activity中使用
fun goneLayout(activity: Activity, ids: IntArray, text: String, res: Int = R.mipmap.no, state: Int = View.GONE) {
val viewGroup: ViewGroup = activity.findViewById(android.R.id.content)
if (viewGroup.tag != null) return
val relative: RelativeLayout = LayoutInflater.from(activity).inflate(R.layout.empty_layout, null, false) as RelativeLayout
viewGroup.removeView(relative)
viewGroup.addView(relative)
if (res == 0) {
relative.EmptyLayout_img.visibility = View.GONE
} else {
relative.EmptyLayout_img.visibility = View.VISIBLE
Glide.with(activity).load(res).into(relative.EmptyLayout_img)
}
relative.EmptyLayout_text.also {
if (text.isEmpty())
it.visibility = state
else {
it.visibility = View.VISIBLE
it.text = text
}
}
ids.forEach {
activity.findViewById<View>(it).visibility = View.GONE
}
viewGroup.tag = relative
}
fun visibleLayout(activity: Activity, ids: IntArray) {
val viewGroup: ViewGroup = activity.findViewById(android.R.id.content)
if (viewGroup.tag == null) {
val relative: RelativeLayout = LayoutInflater.from(activity).inflate(R.layout.empty_layout, null, false) as RelativeLayout
viewGroup.removeView(relative)
} else {
viewGroup.removeView(viewGroup.tag as View)
viewGroup.tag = null
}
ids.forEach {
activity.findViewById<View>(it).visibility = View.VISIBLE
}
}
Fragment中使用
fun goneLayout(activity: Activity, view: View, ids: IntArray, text: String, res: Int = R.mipmap.no) {
if (view is ViewGroup) {
val relative: RelativeLayout = LayoutInflater.from(activity).inflate(R.layout.empty_layout, view, false) as RelativeLayout
if (view.tag != null) return
view.removeView(relative)
view.addView(relative)
layoutParamsMatchParent(relative)
if (res == 0)
relative.EmptyLayout_img.visibility = View.GONE
else {
relative.EmptyLayout_img.visibility = View.VISIBLE
Glide.with(activity).load(res).into(relative.EmptyLayout_img)
}
relative.EmptyLayout_text.also {
if (text.isEmpty())
it.visibility = View.GONE
else {
it.visibility = View.VISIBLE
it.text = text
}
}
ids.forEach {
view.findViewById<View>(it).visibility = View.GONE
}
view.tag = relative
}
}
fun visibleLayout(activity: Activity, view: View, ids: IntArray) {
if (view is ViewGroup) {
val relative: RelativeLayout = LayoutInflater.from(activity).inflate(R.layout.empty_layout, view, false) as RelativeLayout
if (view.tag == null) {
view.removeView(relative)
} else {
view.removeView(view.tag as View)
view.tag = null
}
ids.forEach {
view.findViewById<View>(it).visibility = View.VISIBLE
}
}
}
默認(rèn)空布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/EmptyLayout_Gen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center">
<ImageView
android:id="@+id/EmptyLayout_img"
android:layout_width="@dimen/heart_anim_length"
android:layout_height="@dimen/heart_anim_length"
android:layout_centerInParent="true"
tools:ignore="ContentDescription"/>
<TextView
android:id="@+id/EmptyLayout_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/EmptyLayout_img"
android:layout_centerInParent="true"
android:layout_marginTop="@dimen/dp_5"
android:textColor="@color/black_434343"
android:textSize="@dimen/sp_15"/>
</RelativeLayout>