简体   繁体   English

addView在自定义FrameLayout中不可见,但可单击

[英]addView not visible in custom FrameLayout but clickable

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. 我想创建一个相对通用的ViewGroup容器,以使其在加载任务准备就绪时切换到进度条并再次隐藏(+切换到内容)。 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. 我知道可能会问过几次,但是经过4-5个小时的研究后,我找不到合适的解决方案。

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), eg a listview, shows up perfectly fine. 如果您调用hideProgressBar(),则子级(以xml指定),例如列表视图,显示得很好。 Just the ProgressBar seems to be invisible. 只是ProgressBar似乎是不可见的。 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 解决方法是这里: http : //pastebin.com/2xjnCDLS

instead of creating your own custom view just for switching views, why not use ViewSwitcher instead? 与其创建仅用于切换视图的自定义视图,何不使用ViewSwitcher

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. 它的工作方式是在viewSwitcher中放置2个视图,一个视图可能是progressBar(或包含它的布局,也可能是textView),而另一个是内容本身。

when you are ready to do the switching , you can use viewSwitcher. 当您准备好进行切换时,可以使用viewSwitcher。 showNext () . showNext ()。

in order to be sure the correct view is shown, you can check what is returned from getCurrentView () . 为了确保显示正确的视图,可以检查getCurrentView ()返回的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM