addView not visible in custom FrameLayout but clickable

馋奶兔 提交于 2019-12-13 15:51:01

问题


I wanted to create a relatively generic ViewGroup container to have it switch to a progressbar and hide it again (+switch to content) when the loading task is ready. This should be easy to use and fit for most (not all) cases.

I know this might have been asked several times, but I could not find a fitting solutions after working on this for 4-5 hours.

What does not work? The progressbar simply does not show up at all. I tried it with Visibility and removing / adding all the views in several ways. It does simply not show up. If you call hideProgressBar() the childs (specified in xml), e.g. a listview, shows up perfectly fine. Just the ProgressBar seems to be invisible. The progressbar IS clickable though.

Any ideas?

here is the source:

public class ProgressBarContainer extends FrameLayout {

    private static final int DEFAULT_PROGRESSBAR_STYLE = android.R.style.Widget_Holo_Light_ProgressBar_Large;

    private boolean isIndeterminate = true; // default is true
    private int mProgressBarStyle = DEFAULT_PROGRESSBAR_STYLE;

    private ProgressBar mProgressBar;

    private List<View> childCache = new ArrayList<View>();

    public ProgressBarContainer(Context context) {
        super(context);
    }

    public ProgressBarContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public ProgressBarContainer(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    /**
     * Shows the ProgressBar and hides all "Content" Views
     */
    public void showProgressBar() {
        pushBackChilds();
        //
        // mProgressBar.setVisibility(View.VISIBLE);
        // mProgressBar.bringToFront();
        // setContentVisibility(View.INVISIBLE);

    }

    private void pushBackChilds() {
        for (int i = 0; i < getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (child != mProgressBar) {
                childCache.add(child);
                removeView(child);
            } else {
                Logging.d("ProgressBarContainer", "is ProgressBar at" + i);
            }
        }

        addView(mProgressBar);
    }

    /**
     * Hides the ProgressBar and shows all "Content" Views
     */
    public void hideProgressBar() {
        // mProgressBar.setVisibility(View.INVISIBLE);
        //
        // setContentVisibility(View.VISIBLE);
        rescueChilds();

    }

    private void rescueChilds() {

        removeView(mProgressBar);
        for (View child : childCache) {
            if (child != null) {
                this.addView(child);
            }
        }
    }

    /**
     * Changes visibility of all content except the ProgressBar
     * 
     * @param visibility
     */
    public void setContentVisibility(int visibility) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (child != mProgressBar) {
                child.setVisibility(visibility);
            } else {
                Logging.d("ProgressBarContainer", "is ProgressBar at" + i);
            }
        }
    }

    public ProgressBar getProgressBar() {
        return mProgressBar;
    }

    private void init(AttributeSet attrs) {
        // get styleables
        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.ProgressBarContainer);
        isIndeterminate = a.getBoolean(
                R.styleable.ProgressBarContainer_isIndeterminate, true);
        mProgressBarStyle = a.getInt(
                R.styleable.ProgressBarContainer_progressBarStyle,
                DEFAULT_PROGRESSBAR_STYLE);

        a.recycle();

        // init progressbar and stuff

        mProgressBar = new ProgressBar(getContext(), null, mProgressBarStyle);
        mProgressBar.setVisibility(ProgressBar.VISIBLE);
        mProgressBar.setIndeterminate(isIndeterminate);

        mProgressBar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Logging.d("ProgressBar", "Clicked progressbar");

            }
        });

         LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
         lp.gravity = Gravity.CENTER;
         mProgressBar.setLayoutParams(lp);
        // addView(mProgressBar);
    }
}

AND HERE IS THE SOLUTION: http://pastebin.com/2xjnCDLS


回答1:


instead of creating your own custom view just for switching views, why not use ViewSwitcher instead?

the way it works is that you put 2 views inside the viewSwitcher, one is prbably the progressBar (or a layout that holds it and maybe a textView) , and the other one is the content itself.

when you are ready to do the switching , you can use viewSwitcher.showNext() .

in order to be sure the correct view is shown, you can check what is returned from getCurrentView() .



来源:https://stackoverflow.com/questions/17864896/addview-not-visible-in-custom-framelayout-but-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!