对于父布局,我将按以下方式添加连续的子布局
父布局(parent.xml)
<LinearLayout
android:id="@+id/ly_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- In principle, the cardview should not interfere with the task.
I put the code as a precaution. -->
<android.support.v7.widget.CardView
android:id="@+id/media_card_view"
...>
<LinearLayout
android:id="@+id/ly_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- First row: header -->
<LinearLayout
android:id="@+id/row_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
... />
<TextView
... />
</LinearLayout>
<!-- Second row: details -->
<!-- Programmatically included -->
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
子布局(child.xml)
<LinearLayout
android:id="@+id/child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
... />
<ImageView
android:id="@+id/iv_image"
... />
</LinearLayout>
布局嵌套在一个名为createView()的函数中,在这个函数中我解释了我的疑问
private void createView() {
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.parent, (ViewGroup) findViewById(R.id.ly_parent));
LinearLayout wrapper = parent.findViewById(R.id.ly_wrapper);
LinearLayout child = (LinearLayout) inflater.inflate(R.layout.child, (ViewGroup) findViewById(R.id.ly_child));
wrapper.addView(child);
ImageView imageView = parent.findViewById(R.id.iv_image);
//So far everything works correctly
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//In this event I need to add a new child (ly_child).
//The iv_image field of the newly inserted child must create another child,
//and so on, Something like this:
wrapper.addView(child);
//Get the new ImageView and create the onClickListener event,
//this process will be repeated an indeterminate number of times.
//I had managed to add the children's layout but after some tests I
//modified something and the code fails :(, throws the following exception:
//"The specified child already has a parent. You must call removeView() on the child's parent first."
//I have tried to put a different id to the wrapper layout within the
//onClickListener event but it still does not work. Something like this:
//child.setId(); <- Generation of a unique id.
//wrapper.addView(child);
}
});
}
总结可能是:我需要添加行(每行是子布局-child.xml-)和ImageView 每行必须有onClick事件才能创建另一行。这无限期地重复。
有什么想法吗?我有点受阻
答案 0 :(得分:0)
您需要更改此代码,如下所示。
LinearLayout parent;
LinearLayout wrapper;
private void createView() {
parent = (LinearLayout) inflater.inflate(R.layout.parent, (ViewGroup) findViewById(R.id.ly_parent));
wrapper = parent.findViewById(R.id.ly_wrapper);
addNewChild();
}
private void addNewChild(){
//update this below line
LinearLayout child = (LinearLayout) inflater.inflate(R.layout.child,null,false);
ImageView imageView = child.findViewById(R.id.iv_image);
wrapper.addView(child);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNewChild();
}
});
}
这将在每个onClick
事件上创建单个子项,您可以为每个子项设置唯一的ID和代码执行方式。