Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用循环删除布局的某些视图_Java_Android_Loops_Layout - Fatal编程技术网

Java 使用循环删除布局的某些视图

Java 使用循环删除布局的某些视图,java,android,loops,layout,Java,Android,Loops,Layout,我正在使用添加到布局中的方法动态创建视图。 我正在尝试实现另一个按钮,该按钮应该通过使用循环访问布局并删除它们,但我的应用程序似乎总是崩溃。 我尝试使用循环来执行此操作,因为我不想通过方法removeAllViews()或removeAllViewsInLayout()删除所有视图 基本上,我想知道如何做到这一点而不让应用程序崩溃,以及是否有更好的方法从布局中删除多个视图 // 解决方案 多亏了Anmol的建议,在一个单独的布局中添加动态创建的视图,我实现了我想要的,尽管我无法使用循环。我把我的

我正在使用添加到布局中的方法动态创建视图。 我正在尝试实现另一个按钮,该按钮应该通过使用循环访问布局并删除它们,但我的应用程序似乎总是崩溃。 我尝试使用循环来执行此操作,因为我不想通过方法
removeAllViews()
removeAllViewsInLayout()
删除所有视图

基本上,我想知道如何做到这一点而不让应用程序崩溃,以及是否有更好的方法从布局中删除多个视图

//

解决方案

多亏了Anmol的建议,在一个单独的布局中添加动态创建的视图,我实现了我想要的,尽管我无法使用循环。我把我的解决方案包括在这里,因为它与他的有点不同,尽管我接受了他的答案。抱歉,这是相同的,但我已经包括了如何实现它的所有步骤。请在到期的地方记入贷方

第1步: 添加另一个要包含在activity_main.xml中的布局。我把它命名为
dynamic\u 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>
// 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();
}

而且完成了-享受吧视图有三种状态:可见、消失、不可见。。。。
请将视图状态设置为“已消失”,它将从内存中删除,但是如果您只是想隐藏视图,只需将其设置为“不可见”。

视图有三种状态:可见、已消失、不可见。。。。
请将“视图状态”设置为“已消失”,它将从内存中清除,但如果您只是想隐藏视图,只需将其设置为“不可见”…

您可以将容器作为视图组进行布局并循环:

ViewGroup viewGroup = (LinearLayout) findViewById(R.id.layout);

for(int index=0; index<((ViewGroup)viewGroup).getChildCount(); ++index) {
    View nextChild = ((ViewGroup)viewGroup).getChildAt(index);
    nextChild.setVisibility(View.GONE); // This will somehow remove the view from Ui
    viewGroup.removeView(nextChild); // Or really remove it to be wiped out of memory
}
ViewGroup ViewGroup=(LinearLayout)findViewById(R.id.layout);

对于(int index=0;index,您可以将容器作为视图组进行布局,并在其中循环:

ViewGroup viewGroup = (LinearLayout) findViewById(R.id.layout);

for(int index=0; index<((ViewGroup)viewGroup).getChildCount(); ++index) {
    View nextChild = ((ViewGroup)viewGroup).getChildAt(index);
    nextChild.setVisibility(View.GONE); // This will somehow remove the view from Ui
    viewGroup.removeView(nextChild); // Or really remove it to be wiped out of memory
}
ViewGroup ViewGroup=(LinearLayout)findViewById(R.id.layout);

对于(int index=0;index您将获得NullPointerException,因为您尝试访问不再存在的视图。删除视图时,ConstraintLayout中的视图减少了一个,但count变量保持不变

所以,您必须计算循环迭代中的视图数

每次删除视图时,都会减小i,因为当删除视图之后,所有视图都会移回一个元素

for(int i = 0; i < layout.getChildCount(); i++) {
    if(textView1Id == childId || fabId == childId) {}
    else {
        layout.removeView(childPos);
        i--;
    }
}
for(int i=0;i
由于尝试访问不再存在的视图,因此会出现NullPointerException。删除视图时,ConstraintLayout中的视图减少了一个,但count变量保持不变

所以,您必须计算循环迭代中的视图数

每次删除视图时,都会减小i,因为当删除视图之后,所有视图都会移回一个元素

for(int i = 0; i < layout.getChildCount(); i++) {
    if(textView1Id == childId || fabId == childId) {}
    else {
        layout.removeView(childPos);
        i--;
    }
}
for(int i=0;i
根据您的用例,您可以在布局中添加容器布局,并将所有动态视图添加到该容器中,当您想要删除动态视图时,只需执行以下操作

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按钮

但是这不应该是首选,因为目前您只有两个视图要保留,但是如果列表增长,那么对于简单的操作来说,开销太大,因此最好使用第一个解决方案,它也很容易管理和操作


谢谢。

根据您的用例,您可以在布局中添加一个容器布局,并将所有动态视图添加到该容器中,当您想要删除动态视图时,只需执行以下操作

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按钮

但是这不应该是首选,因为目前您只有两个视图要保留,但是如果列表增长,那么对于简单的操作来说,开销太大,因此最好使用第一个解决方案,它也很容易管理和操作


谢谢。

这是因为你正在为(int i=0;我请在你的问题中写下logcat中的错误消息!@Thelouras Hello,很抱歉,但是很长。有必要吗?你有
NullPointerException
对吗?请看我上面的评论-这是来自
getChildAt
文档:“返回指定位置的视图,如果该位置在组中不存在,则返回null”我建议在布局(MatchParent)顶部创建一个单独的容器,以添加所有动态视图并执行
removeAllView()
在该容器上,这是处理此类用例的最有效方法。@ErlendK.H。这是因为您正在为(int i=0;我请在您的问题中在logcat中写下错误消息!@Thelouras您好,很抱歉,但是它太长了。有必要吗?您有
NullPointerException
对吗?请参阅我上面的评论-这来自
getChildAt
文档:返回指定位置处的视图,如果该位置不在组“我有一个建议,建议在布局(MatchParent)顶部创建一个单独的容器以添加所有动态视图,则返回null,并且执行
removeAllView()
在该容器上,这是处理此类用例的最有效方法。@ErlendK.H.保留视图不会清除内存。保留视图并不意味着它不会加载到内存中,而是不可见。要从内存中清除视图,需要调用removeView()。保留视图不会清除内存。Gon