WassimBenltaief/FlowLayout

why not use <Viewstub> to lazy load layout

Opened this issue · 6 comments

why not use to lazy load layout

yeah,it would be better!

@Owenmike yes that's true. I will make it or if you have enough time pr is welcome.
@THEONE10211024 I don;t see where MultiStateView is using a ViewStub ? can you point to it please ?

@WassimBenltaief sorry bro,you misunderstood it. I mean your project is just like Kennyc1012's MultiStateView,and you both didn't use . I really hope you can add it and make it better than MultiStateView

@THEONE10211024 👍
I gonna make it in the next days.

public class MultiStateView extends FrameLayout {

    private ViewStub errorView;
    private ViewStub emptyView;
    private ViewStub progressView;

    private CharSequence errorText;
    private Drawable emptyIcon;
    private CharSequence emptyText;

    private @State int mViewState = CONTENT;

    private OnClickListener listener;

    public MultiStateView(Context context) {
        this(context, null);
    }

    public MultiStateView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MultiStateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MultiStateView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr);
    }

    public void init(AttributeSet attrs, int defStyle) {
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.MultiStateView, defStyle, 0);
        errorText = array.getString(R.styleable.MultiStateView_msv_errorText);
        emptyIcon = array.getDrawable(R.styleable.MultiStateView_msv_emptyIcon);
        emptyText = array.getString(R.styleable.MultiStateView_msv_emptyText);
        array.recycle();
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        init(attrs, defStyle);
        inflate(context, R.layout.layout_multistate, this);
    }

    public void setErrorText(CharSequence errorText) {
        this.errorText = errorText;
    }

    public void setEmptyText(CharSequence emptyText) {
        this.emptyText = emptyText;
    }

    public void setState(@State int state) {
        if (mViewState == state) return;
        switch (state) {
            case CONTENT:
                setGone(errorView);
                setGone(emptyView);
                setGone(progressView);
                break;

            case PROGRESS:
                if (!setVisible(progressView)) {
                    progressView = (ViewStub) findViewById(R.id.progress_view);
                    progressView.inflate();
                    setVisible(progressView);
                }
                setGone(errorView);
                setGone(emptyView);
                break;

            case EMPTY:
                if (!setVisible(emptyView)) {
                    emptyView = (ViewStub) findViewById(R.id.empty_view);
                    final View view = emptyView.inflate();
                    final ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
                    icon.setImageDrawable(emptyIcon);
                    final TextView empty = (TextView) view.findViewById(android.R.id.empty);
                    if (!TextUtils.isEmpty(emptyText)) {
                        empty.setText(emptyText);
                    }
                    setVisible(emptyView);
                }
                setGone(progressView);
                setGone(errorView);
                break;

            case ERROR:
                if (!setVisible(errorView)) {
                    errorView = (ViewStub) findViewById(R.id.error_view);
                    final View view = errorView.inflate();
                    final TextView error = (TextView) view.findViewById(R.id.text_error);
                    error.setOnClickListener(listener);
                    if (!TextUtils.isEmpty(errorText)) {
                        error.setText(errorText);
                    }
                    setVisible(errorView);
                }
                setGone(progressView);
                setGone(emptyView);
                break;
        }
        this.mViewState = state;
    }

    private void setGone(final View view) {
        if (view != null)
            view.setVisibility(GONE);
    }

    private boolean setVisible(final View view) {
        if (view != null) {
            fadeIn(view);
            view.setVisibility(VISIBLE);
            return true;
        }
        return false;
    }

    private void fadeIn(final View view) {
        fadeIn(getContext(), view);
    }

    public static void fadeIn(final Context context, final View view) {
        fadeIn(context, view, true);
    }

    public static void fadeIn(final Context context, final View view, final boolean animate) {
        if (view != null)
            if (animate)
                view.startAnimation(AnimationUtils.loadAnimation(context, android.R.anim.fade_in));
            else
                view.clearAnimation();
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        this.listener = l;
    }

}
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ViewStub
        android:id="@+id/error_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/layout_error" />

    <ViewStub
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/layout_empty" />

    <ViewStub
        android:id="@+id/progress_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/layout_progress" />

</merge>
public class FlowLayout extends FrameLayout {

    private FrameLayout contentView;
    private MultiStateView multiStateView;

    public FlowLayout(Context context) {
        this(context, null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.FlowLayout, defStyle, 0);
        final boolean isLoad = array.getBoolean(R.styleable.FlowLayout_fl_isLoad, false);
        array.recycle();
        inflate(context, R.layout.layout_flow, this);
        contentView = (FrameLayout) findViewById(R.id.content_view);
        multiStateView = (MultiStateView) findViewById(R.id.multiState);
        multiStateView.init(attrs, defStyle);
        setState(isLoad ? MultiStateView.PROGRESS : MultiStateView.CONTENT);
    }

    /**
     * Add the main content of the RelativeLayout custom view to the subView contentView.
     * This is the main content that will contains the childs of FlowLayout.
     *
     * @param child
     * @param index
     * @param params
     */
    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (contentView == null) {
            super.addView(child, index, params);
        } else {
            //Forward these calls to the content view
            contentView.addView(child, index, params);
        }
    }

    public void setErrorText(CharSequence errorText) {
        if (multiStateView == null)
            multiStateView = (MultiStateView) findViewById(R.id.multiState);
        multiStateView.setErrorText(errorText);
    }

    public void setEmptyText(CharSequence emptyText) {
        if (multiStateView == null)
            multiStateView = (MultiStateView) findViewById(R.id.multiState);
        multiStateView.setEmptyText(emptyText);
    }

    public void setState(@State int state) {
        setState(state, false);
    }

    public void setState(@State int state, boolean isShowAnimat) {
        if (multiStateView == null)
            multiStateView = (MultiStateView) findViewById(R.id.multiState);
        if (contentView == null)
            contentView = (FrameLayout) findViewById(R.id.content_view);
        if (state == MultiStateView.CONTENT) {
            if (isShowAnimat)
                fadeIn(contentView);
            contentView.setVisibility(VISIBLE);
            multiStateView.setVisibility(GONE);
        } else {
            multiStateView.setVisibility(VISIBLE);
            multiStateView.setState(state);
            contentView.setVisibility(state == MultiStateView.EMPTY ? VISIBLE : GONE);
        }
    }

    private void fadeIn(final View view) {
        MultiStateView.fadeIn(getContext(), view);
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        if (multiStateView == null)
            multiStateView = (MultiStateView) findViewById(R.id.multiState);
        multiStateView.setOnClickListener(l);
    }

}
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <MultiStateView
        android:id="@+id/multiState"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_centerInParent="true" />

    <FrameLayout
        android:id="@+id/connectivity_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>
    
</merge>

like this.but the animat has some problem