美文网首页
Android 几行代码实现通用缺省图

Android 几行代码实现通用缺省图

作者: 坐怀灬不乱 | 来源:发表于2020-01-07 09:28 被阅读0次

    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中使用

    • 缺省图显示 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
                }
            }
        }
    
    默认空布局
    <?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>
    

    相关文章

      网友评论

          本文标题:Android 几行代码实现通用缺省图

          本文链接:https://www.haomeiwen.com/subject/scfractx.html