添加到父级的View上的通知?

问题描述 投票:4回答:3

当我在Android中动态构建View时,我必须通过调用将其添加到“父”ViewGroup

myLinearLayout.addView(myView);

我知道我可以监督ViewGroup,通过优秀的onHierarchyChangeListener添加任何孩子,但在我的情况下,我需要在View本身的反馈。因此我的问题是:

有什么像View.onAddedToParent()回调或我可以建立的听众?

为了使事情变得非常清楚:我希望视图能够自己处理所有事情,我知道我可以在“父”中捕获事件,然后通知视图有关事情,但这不是必需的。我只能改变观点

编辑:我刚刚找到onAttachStateChangeListener,它似乎适用于大多数情况,但我想知道这是否真的是正确的解决方案。我想View也可以从一个ViewGroup传递到另一个public class CustomView extends View { public CustomView(Context context) { super(context); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); Log.d("CustomView", "onAttachedToWindow called for " + getId()); Toast.makeText(getContext(), "added", 1000).show(); } } 而不会脱离窗户。所以即使我愿意也不会收到活动。如果你有洞察力,请你详细说明一下吗?

提前致谢

android android-layout android-view android-custom-view
3个回答
5
投票

您可以在其onAttachedToWindow中创建自定义视图并执行您的操作

@Override
 protected void onAttachedToWindow() {
    // TODO Auto-generated method stub
    super.onAttachedToWindow();

    if(((View)getParent()).getId()== R.id.relativelayout2)
    {           
        Log.d("CustomView","onAttachedToWindow called for " + getId());
        Toast.makeText(context, "added", 1000).show();          
    }

}

如果要确保将自定义视图添加到所需的正确视图组中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    final LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    layout.setOnHierarchyChangeListener(new OnHierarchyChangeListener() {

        @Override
        public void onChildViewRemoved(View parent, View child) {
            Log.e("View","removed");
            if(child instanceof CustomButton){
                CustomButton button = (CustomButton)child;
                button.addListener();
            }
        }

        @Override
        public void onChildViewAdded(View parent, View child) {
            Log.e("View","added");
            if(child instanceof CustomButton){
                CustomButton button = (CustomButton)child;
                button.addListener();
            }
        }
    });

    for(int i = 0; i < 10; ++i){
        CustomButton view = new CustomButton(this);
        view.setText("Button "+i);
        layout.addView(view, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                layout.removeViewAt(layout.getChildCount()-1);
            }
        });

    }

    setContentView(layout);

}

2
投票

在我看来,你想要这样;

CreateViews;

public interface OnAddedListener {

    public void addListener();

}

监听;

public class CustomButton extends Button implements OnAddedListener{

    public CustomButton(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void addListener() {
        Log.e("","In button add listener");
    }



}

CustomButton类;

removeView()

1
投票

根据Android源代码,视图不能移动到另一个布局,除非在其父级首先调用removeView(),如果你查看removeViewInternal()的代码,它会调用removeViewInternal(),后者又调用this的重载, ,在view.dispatchDetachedFromWindow()线上调用this,其中,基于onDetachedFromWindow()线上的Android源代码调用addView()。然后使用onAttachedToWindow()添加视图,qazxswpoi以相同的方式调用qazxswpoi。

© www.soinside.com 2019 - 2024. All rights reserved.