代码之家  ›  专栏  ›  技术社区  ›  avalancha

是否将视图通知添加到父项?

  •  4
  • avalancha  · 技术社区  · 11 年前

    当我建立 View 在Android中,我必须动态地将其添加到“父级” ViewGroup 通过呼叫

    myLinearLayout.addView(myView);
    

    我知道我可以监督 视图组 任何孩子都可以通过优秀的 onHierarchyChangeListener ,但就我而言,我需要 看法 它本身因此,我的问题是:

    有类似的东西吗 View.onAddedToParent() 回调还是我可以构建的监听器?

    为了让事情变得非常清楚:我希望视图能够自己处理所有事情,我知道我可以在“父”中捕捉到事件,然后通知视图有关事情,但这是 此处需要。我只能改变看法

    编辑:我刚刚找到 onAttachStateChangeListener 它似乎适用于大多数情况,但我想知道这是否真的是正确的解决方案。我在想 看法 还不如从一个传下来 视图组 在不脱离窗户的情况下将其转移到另一个。所以即使我想参加活动,我也不会参加。如果你有见解,你能详细说明一下吗?

    提前谢谢

    3 回复  |  直到 11 年前
        1
  •  5
  •   Diego Palomar    11 年前

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

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

    如果您想确保您的自定义视图添加到您想要的正确视图组

    @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();          
        }
    
    }
    
        2
  •  3
  •   kyay10    5 年前

    根据Android源代码,除非 removeView() 首先在其父对象上调用,如果您查看 removeView() ,它调用 removeViewInternal() ,这反过来又会导致 removeView内部() ,在 this 电话线呼叫 view.dispatchDetachedFromWindow() ,基于上的Android源代码 this 电话线呼叫 onDetachedFromWindow() 。然后使用添加视图 addView() ,它调用 onAttachedToWindow() 以同样的方式。

        3
  •  2
  •   nurisezgin    11 年前

    在我看来,你想要这样;

    创建视图;

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

    听众;

    public interface OnAddedListener {
    
        public void addListener();
    
    }
    

    CustomButton类;

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