addView定制的FrameLayout但点击不可见 [英] addView not visible in custom FrameLayout but clickable

查看:1132
本文介绍了addView定制的FrameLayout但点击不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造一个相对通用的ViewGroup容器将它切换到进度,并再次将其隐藏(+切换到内容)当加载任务准备。这应该是易于使用和适合大部分(不是全部)的情况。

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.

我知道这可能已经问了好几次,但是这方面的工作4-5小时后,我无法找到一个合适的解决方案。

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.

什么不起作用?
进度条根本不显示在所有。我试了一下,能见度和删除/添加所有的意见在几个方面。它根本就没有露面。如果调用hideProgressBar()孩子的(在XML中指定),例如一个列表视图,展示了完美的罚款。就在进度似乎是不可见的。进度条是可以点击虽然。

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.

任何想法?

下面是源:

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);
    }
}

这里是解决方案:
http://pastebin.com/2xjnCDLS

推荐答案

而不是创建自己的自定义视图只为切换视图,为什么不使用的 ViewSwitcher 呢?

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

它的工作方式是,你把里面viewSwitcher 2次,一个是prbably的进度(或持有它,也许一个TextView的布局),另一种是内容本身。

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。的 showNext ()。

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

,以确保正确的观点所示,你可以检查什么是从的 getCurrentView ()。

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

这篇关于addView定制的FrameLayout但点击不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆