vlonjat-gashi / progress-activity

Easily add loading, empty and error states in your app.
1.02k stars 171 forks source link

cannot process visibility of indirect children of ProgressActivity #16

Open ProgrammerAnthony opened 7 years ago

ProgrammerAnthony commented 7 years ago

I just meet with this problem and I give out some ways to solve this problem.FYI

in your sourcecode you use this code

`

public void addView(@NonNull View child, int index, ViewGroup.LayoutParams params) {

    super.addView(child, index, params);
    if (child.getTag() == null || (!child.getTag().equals(TAG_LOADING) &&
            !child.getTag().equals(TAG_EMPTY) && !child.getTag().equals(TAG_ERROR))) {
        contentViews.add(child);
    }

}

`

and process the visibility every time we change to different state. `

private void setContentVisibility(boolean visible, List<Integer> skipIds) {

    for (View v : contentViews) {
        if (!skipIds.contains(v.getId())) {
            v.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }

}

`

which means you will only process the view component in List<View> contentViews = new ArrayList<>();

but the addView method will not add the child views of a child viewgroup .which means a ImageView is inside a RelativeLayout,and the RelativeLayout is a child of ProgressActivity . In this context ,the ImageView will not be add to the contentViews instance .

so I made some changes

`

public void addView(@NonNull View child, int index, ViewGroup.LayoutParams params) {

    super.addView(child, index, params);
    if (child.getTag() == null || (!child.getTag().equals(TAG_LOADING) &&
            !child.getTag().equals(TAG_EMPTY) && !child.getTag().equals(TAG_ERROR))) {
         //            contentViews.add(child);
        contentViews.addAll(getAllChildren(child));
        Log.i("ProgressActivity", "sub children count of ProgressActivity is" + contentViews.size());
    }

}

/**
 * save all of the direct and  indirect children views
 *
 * @param
 */
private List<View> getAllChildren(View v) {

    if (!(v instanceof ViewGroup)) {
        List<View> viewArrayList = new ArrayList<>();
        viewArrayList.add(v);
        return viewArrayList;
    } else {
        if (((ViewGroup) v).getChildCount() == 0) {
            List<View> viewArrayList = new ArrayList<>();
            viewArrayList.add(v);
            return viewArrayList;
        } else {
            List<View> result = new ArrayList<>();

            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {

                View child = vg.getChildAt(i);

                List<View> viewArrayList = new ArrayList<>();
                viewArrayList.add(v);
                viewArrayList.addAll(getAllChildren(child));

                result.addAll(viewArrayList);
            }
            return result;
        }
    }
}`

now the instance of contentViews includes all of the chilrend and grandson ....component.

one more changes is needed: `

 /**
   * set children visibility of StatusLayout
    *
   * @param visible
    * @param skipIds
   */
private void setContentVisibility(boolean visible, List<Integer> skipIds) {
    for (View v : contentViews) {
        if (!skipIds.contains(v.getId())) {
            v.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
        }
        // when visible is true,which means set content Layout, the skipIds compoent need to be hide
        if (visible) {
            if (skipIds.contains(v.getId())) {
                v.setVisibility(View.INVISIBLE);
            }
        }
        //set parent to be visible when child want to be visible
        if (v.getVisibility() == View.VISIBLE) {
            setParentToVisible(v);
        }
    }
}

`

`

public void setParentToVisible(View v) {
    ViewParent mParent = v.getParent();
    if (mParent != null && mParent instanceof ViewGroup) {
        ((ViewGroup) mParent).setVisibility(View.VISIBLE);
        if (mParent.getParent() != null) {
            setParentToVisible((View) mParent);
        }
    }

}

`

the part of code which I add .means the example ImageView I told in this issue before if i skip to hide it .I needed to set it to visible .but at the same time ,we need to set its parent to be visible ,which is the RelativeLayout -the child of ProgressActivity.

For anyone who meet with this problem's recommendation.