【问题标题】:Android how to check in FrameLayout already view add or notAndroid如何检查FrameLayout已经查看添加与否
【发布时间】:2018-01-06 15:46:58
【问题描述】:

在我的应用程序中,我使用了 Framelayout,并在 FrameLayout 中添加了贴纸视图。 在我添加贴纸视图之前如何检查 Framelayout 是否已添加视图。

在我的应用程序中显示如下错误

java.lang.IllegalStateException: The specified child already has a parent.

我在 RecyclerView 适配器 itemView OnClickListener 中添加了 StickerTextView,如下所示:

 itemView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                tv_sticker.setText(""+ staticData.greetings);
                canvas.addView(tv_sticker);
           }
        });

【问题讨论】:

  • 能否在您将贴纸视图添加到框架布局的位置添加代码?
  • 您可以使用 view.getParent() 检查将成为子项的视图元素是否具有父项。检查这个问题:stackoverflow.com/questions/17721991/…

标签: android android-framelayout


【解决方案1】:

您可以在添加视图之前使用它:

if(stickerView.getParent() instanceof ViewGroup) {
    ((ViewGroup)stickerView.getParent()).removeView(stickerView);
}
frameLayout.addView(stickerView);

【讨论】:

    【解决方案2】:

    我在 addview 之前使用 removeAllViews() 方法解决了错误,如下所示:

     itemView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                tv_sticker.setText(""+ staticData.greetings);
                canvas.removeAllViews();
                canvas.addView(tv_sticker);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多