1. 程式人生 > >Android ViewStub控制元件

Android ViewStub控制元件

前言

使用ViewStub可以實現介面的延遲載入。

1. ViewStub簡單應用

(1) 佈局檔案
layout為載入View,inflatedId為載入後View的id。

<ViewStub
    android:id="@+id/view_stub"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inflatedId="@+id/tv_inflate"
    android:layout="@layout/view_stub_inflate"/>

(2) view_stub_inflate.xml檔案

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="view_stub_inflate" />

(3) 載入

  • inflate方法
    ViewStub viewStub = findViewById(R.id.view_stub);
    if (mTvInflate == null) {
        mTvInflate = (TextView) viewStub.inflate();
    }
  • setVisibility方法
    ViewStub viewStub = findViewById(R.id.view_stub);
    if (viewStub.getVisibility() != View.VISIBLE) {
        viewStub.setVisibility(View.VISIBLE);
    }

2. ViewStub解析

(1) 建構函式
ViewStub在建構函式內,獲取id, inflatedId和layout,並設定為不可見。

public ViewStub(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context);

    final TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.ViewStub, defStyleAttr, defStyleRes);
    mInflatedId = a.getResourceId(R.styleable.ViewStub_inflatedId, NO_ID);
    mLayoutResource = a.getResourceId(R.styleable.ViewStub_layout, 0);
    mID = a.getResourceId(R.styleable.ViewStub_id, NO_ID);
    a.recycle();

    setVisibility(GONE);
    setWillNotDraw(true);
}

(2) inflate方法

inflate方法會解析layout,並替代當前ViewStub的位置。

public View inflate() {
    final ViewParent viewParent = getParent();

    if (viewParent != null && viewParent instanceof ViewGroup) {
        if (mLayoutResource != 0) {
            final ViewGroup parent = (ViewGroup) viewParent;
            final View view = inflateViewNoAdd(parent);
            replaceSelfWithView(view, parent);

            mInflatedViewRef = new WeakReference<>(view);
            if (mInflateListener != null) {
                mInflateListener.onInflate(this, view);
            }

            return view;
        } else {
            throw new IllegalArgumentException("ViewStub must have a valid layoutResource");
        }
    } else {
        throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent");
    }
}

// 解析layout,並設定ID
private View inflateViewNoAdd(ViewGroup parent) {
    final LayoutInflater factory;
    if (mInflater != null) {
        factory = mInflater;
    } else {
        factory = LayoutInflater.from(mContext);
    }
    final View view = factory.inflate(mLayoutResource, parent, false);

    if (mInflatedId != NO_ID) {
        view.setId(mInflatedId);
    }
    return view;
}

// 代替當前ViewStub的位置 
private void replaceSelfWithView(View view, ViewGroup parent) {
    final int index = parent.indexOfChild(this);
    parent.removeViewInLayout(this);

    final ViewGroup.LayoutParams layoutParams = getLayoutParams();
    if (layoutParams != null) {
        parent.addView(view, index, layoutParams);
    } else {
        parent.addView(view, index);
    }
}

(3) setVisibility方法

先判斷mInflatedViewRef是否為空,也就是有沒有呼叫過inflate方法。

public void setVisibility(int visibility) {
    if (mInflatedViewRef != null) {
        View view = mInflatedViewRef.get();
        if (view != null) {
            view.setVisibility(visibility);
        } else {
            throw new IllegalStateException("setVisibility called on un-referenced view");
        }
    } else {
        super.setVisibility(visibility);
        if (visibility == VISIBLE || visibility == INVISIBLE) {
            inflate();
        }
    }
}