java - 使用循环删除某些布局视图
问题描述
我正在使用添加到布局中的方法动态创建视图。我正在尝试实现另一个按钮,该按钮应该访问布局并使用循环删除它们,但我的应用程序似乎一直崩溃。我正在尝试使用循环来执行此操作,因为我不想通过方法removeAllViews()
或removeAllViewsInLayout()
.
public void RemoveViews() {
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constraintId);
// I'd like to keep a text view and a button, but remove the rest.
TextView textView1 = (TextView) findViewById(R.id.textView1);
int textView1Id = textView1.getId();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
int fabId = fab.getId();
int count = layout.getChildCount();
for(int i=0;i<count;i++) {
View childPos = layout.getChildAt(i);
int childId = layout.getChildAt(i).getId();
// Here I try to detect whether the child is the text view or button.
if(textView1Id == childId || fabId == childId) {}
else {
// The line below causes the crashing.
layout.removeView(childPos);
// This one also crashes:
//layout.removeViewAt(i);
}
}
}
这是activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintId"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:onClick="onClick_textView1"
android:padding="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center"
android:text="@string/text1"
android:textColor="#808080"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_refresh_black_24dp" />
</android.support.constraint.ConstraintLayout>
我认为这是错误的 logcat (FATAL EXCEPTION: main):
2018-12-31 14:57:33.004 7507-7507/com.example.test4 E/AndroidRuntime: FATAL
EXCEPTION: main
Process: com.example.test4, PID: 7507
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24774)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24774)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
at com.example.test4.MainActivity.RemoveKeyboardList(MainActivity.java:115)
at com.example.test4.MainActivity.onClick_textView1(MainActivity.java:64)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24774)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
基本上,我想知道如何在不让应用程序崩溃的情况下做到这一点,以及是否有更好的方法从布局中删除多个视图。
//
解决方案
感谢 Anmol 的建议,将动态创建的视图添加到单独的布局中,我实现了我想要的,尽管我无法让它与循环一起工作。我在这里包含了我的解决方案,因为它与他的有点不同,尽管我接受了他的回答。对不起,它是一样的,但我已经包含了如何实现它的所有步骤。请在到期时给予信用。
第 1 步:
添加另一个布局以包含在 activity_main.xml 中。我命名它dynamic_content.xml
。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dynamic_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</android.support.constraint.ConstraintLayout>
第 2 步:将新布局包含在activity_main.xml
.
// Add the line below anywhere as far as I know.
<include layout="@layout/dynamic_content" />
第 3 步:仅在新布局中包含所有动态创建的视图。
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.dynamic_content);
// Create new text views, buttons, etc.
layout.addView(newTextView);
第 4 步:创建删除它们的函数。
public void RemoveViews() {
ConstraintLayout dynamic = (ConstraintLayout) findViewById(R.id.dynamic_content);
dynamic.removeAllViews();
}
第 5 步:将功能添加到按钮。
public void onClick_buttonName(View v)
{
RemoveViews();
}
它已经完成了 - 享受吧!
解决方案
根据您的用例,您可以在布局中添加一个 containerLayout 并将所有动态视图添加到该容器中,并且当您想要删除动态视图时,只需执行以下操作。
public void AddViewToContainer(View view){
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.containerfordynamic_view);
layout.addView(view);
}
public void RemoveViews() {
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.containerfordynamic_view);
layout.removeAllViews();
}
第二个选项是在您的布局上执行 removeAllView() 并再次添加您的 editText 和 Fab 按钮,
但这不应该是首选,因为目前您只有两个视图要保留,但如果列表增长,对于简单操作来说开销太大,所以最好使用第一个易于管理和操作的解决方案。
谢谢。
推荐阅读
- python - 在外部保存通用 python 对象
- javascript - 如何将类构造函数的“this”绑定到外部对象
- javascript - 动态边界更改不会对“react-leaflet”产生任何影响
- regex - 在两个字符串之间查找包含某个值的文本
- python - 如何使用 Python 尽可能有效地下载和上传文件?
- azure - 验证 azure devops feed 的内容
- git - Gitlab SSH 部署到服务器权限错误
- django - 如何使用简单的 Django 过滤器进行基于位置的查询?
- mysql - 从数据库中返回最近的位置,在数据库中有多个位置
- c# - 如何使用 XmlSerializer 类对象将具有相同名称的 2 个子元素组合成单个属性是 C#