【问题标题】:How do I inflate a view/container multiple times by button click inside a Fragment?如何通过在片段内单击按钮多次膨胀视图/容器?
【发布时间】:2019-10-08 16:46:37
【问题描述】:

这是我在 StackOverflow 上的第一篇文章。到目前为止,这个网站一直是救命稻草。

我对此很陌生,到目前为止我已经能够解决我的问题,但我在这里被困在这个问题上。

我正在一个片段中工作(我是新手)。我正在尝试用视图膨胀(我对此知之甚少)此片段中的现有容器。我希望我能正确解释这一点。

上下文:我试图从用户那里获得一些测量值(和其他变量)来计算墙的面积、长度等。用户应该能够通过单击按钮“添加墙”;这应该只是从现有布局膨胀。

我尝试在不同的生命周期中移动代码(onCreate 与 onCreatedView 等)。我还尝试在LayoutInflator 中传递不同的参数(父视图,将 attachToRoot 设置为 false、true 等)

CalculatorActivity.java

    public class CalculatorActivity extends AppCompatActivity {

      //...

      private FragmentNewCalc fragmentNewCalc;    

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        fragmentNewCalc = new FragmentNewCalc();
        //Replace Containers
        getSupportFragmentManager().beginTransaction()
          .replace(R.id.container_wrap, fragmentNewCalc)
          .commit();
    }
}

这在这个 XML 中膨胀 ->

<FrameLayout
    android:id="@+id/container_wrap"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

FragmentNewCalc.java


      LinearLayout wallContainer;
      LinearLayout test; //testing with LinearLayout
      CardView newWallCard; //this is what I want to use though


      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_new_calc, container, false);

        wallContainer = (LinearLayout) v.findViewById(R.id.new_wall_container);
         //LayoutInflater inflater2; //testing
        //inflater2 = getLayoutInflater();
        //newWallCard = (CardView) inflater.inflate(R.layout.frag_new_walls, container, false);
        test = (LinearLayout) inflater.inflate(R.layout.wall_add_section, null); //tested this wallContainer as parent view and true/false   

        fab_add_wall = (FloatingActionButton) v.findViewById(R.id.fab_add_wall);


        fab_add_wall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wallContainer.addView(test);


            }
        });


        return v;
    }
}

这是应该发生膨胀的地方:(具有 CoordinatorLayout、Scrollview 等的更大布局的一部分)

    <LinearLayout
        android:id="@+id/new_wall_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

我第一次单击按钮时,用户界面成功添加了膨胀布局(按预期工作),但连续单击时,我收到错误:

java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。

我当然不想删除那个视图,但想继续添加它。 抓头

在我开始将所有内容更改为 Fragments 之前,我已经完成了这项工作。所以这告诉我,这可能与我现在在一个片段中的事实有关。

我花了一整天的时间试图弄清楚这一点。我不确定我的逻辑是否有缺陷,因为我刚开始使用 Fragments,或者是我对 LayoutInflator 缺乏了解。大概两者兼而有之。 O:-)

【问题讨论】:

  • 请将代码放入代码框中(这次我已经为您完成了)。你们真的会在发布问题后重新阅读您的问题吗? :-) 回到您的代码,似乎有不平衡的花括号。请更正这一点;否则,可能没有人能理解它。
  • 谢谢。我是这种格式的新手,实际上是在尝试编辑代码。我想我修复了你引用的花括号。我将代码减少到绝对最小值以显示手头的问题。
  • 好的,但似乎仍然缺少一个右花括号......(除非我的一天太长了)。
  • 我仍然不明白代码块是如何工作的。 (当我编辑它时)还有另一个缺少的花括号(所以实际上总共有 2 个)。但即使我修复了它,它也在我现在知道如何使用的“代码块”之外。 (我想!)感谢您的指导。很快再见;-)

标签: java android android-fragments android-linearlayout layout-inflater


【解决方案1】:

在您的 onClick 中,请放大视图

public void onClick(View v) { 

    inflater.inflate(R.layout.wall_add_section, wallContainer, true);
}

【讨论】:

  • 我一开始就是这么做的。我刚刚又试了一下。我在那里得到一个空对象引用。
  • 什么是空?显示一些日志
  • java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
  • 我什至不知道我以前在哪里搞砸了。我可能忘了申报我的容器什么的。但我又试了一次,它符合你的指示。太感谢了。我以为我实际上已经尝试过,但一定是在其他地方搞砸了,并驳回了这种尝试。非常感谢你。我无法解决这个(应该是一个简单的)问题,让我开始失去很多注意力,这让我感到非常恼火。再次感谢您。
猜你喜欢
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 2020-04-01
  • 2016-04-12
相关资源
最近更新 更多