gpt4 book ai didi

java - 多个嵌套布局字段中的事件 (OnClickListener)

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:02 24 4
gpt4 key购买 nike

对于父布局,我通过以下方式添加连续的子布局

父布局(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 事件才能创建另一行。这会无限期地重复。

有什么想法吗?我有点受阻

最佳答案

您需要如下更改此代码。

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 和代码执行。

关于java - 多个嵌套布局字段中的事件 (OnClickListener),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49671727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com