Android如何检查FrameLayout已经查看是否添加
问题描述:
在我的应用程序中,我使用了Framelayout,并在FrameLayout中添加了贴图视图。 之前我添加贴纸视图如何检查在Framelayout已经查看添加与否。Android如何检查FrameLayout已经查看是否添加
在我的应用程序显示错误如下
java.lang.IllegalStateException: The specified child already has a parent.
我有加StickerTextView在RecyclerView适配器ItemView控件OnClickListener象下面这样:
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tv_sticker.setText(""+ staticData.greetings);
canvas.addView(tv_sticker);
}
});
答
我已经解决了addview之前使用removeAllViews()方法的错误像bellow :
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tv_sticker.setText(""+ staticData.greetings);
canvas.removeAllViews();
canvas.addView(tv_sticker);
}
});
答
您可以使用此添加视图前:
if(stickerView.getParent() instanceof ViewGroup) {
((ViewGroup)stickerView.getParent()).removeView(stickerView);
}
frameLayout.addView(stickerView);
你可以添加你的代码添加你的贴纸视图到框架布局的代码吗? –
您可以使用view.getParent()检查将作为子项的视图元素是否具有父项。 检查这个问题: https://stackoverflow.com/questions/17721991/check-if-view-element-is-added-to-layout-or-not-programmatically – smukamuka