android 中具有相同资源 ID 的多个视图
Multiple view with same resource id in android
我在一个包含其他视图的片段中只有一个列表视图。
如下面的视图转储所示,出于某种原因,层次结构中有两个列表视图(具有相同的资源 ID)。
我认为此处未填充的列表视图(第一个)掩盖了我填充的列表视图。
这个列表视图是什么(在屏幕截图中选择的)以及如何删除 it/find 它的来源。
我的这个片段代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/dark"
android:orientation="vertical">
<include layout="@layout/empty_view"/>
<include layout="@layout/progress_bar"/>
<com.application.custom.CustomListView
android:id="@+id/main_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
更新:
activity 只是加载片段:
HomeFrag homeFragment = new HomeFrag();
getSupportFragmentManager().beginTransaction()
.add(R.id.homelist_fragment_container, homeFragment, "home_fragment")
.commit();
这里我们使用add
添加片段,没有检查片段是否存在。这导致了具有相同资源 ID 的多个视图层次结构。
添加以下检查以查看片段是否已存在,围绕添加片段代码修复此问题:
if (getSupportFragmentManager().findFragmentByTag("my_frag_tag") == null) {
//add fragment with tag "my_frag_tag"
HomeFrag homeFragment = new HomeFrag();
getSupportFragmentManager().beginTransaction()
.add(R.id.homelist_fragment_container, homeFragment, "my_frag_tag")
.commit();
}
这也确保了在不需要创建片段时不会创建片段(与 replace
不同)。
我在一个包含其他视图的片段中只有一个列表视图。
如下面的视图转储所示,出于某种原因,层次结构中有两个列表视图(具有相同的资源 ID)。
我认为此处未填充的列表视图(第一个)掩盖了我填充的列表视图。
这个列表视图是什么(在屏幕截图中选择的)以及如何删除 it/find 它的来源。
我的这个片段代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/dark"
android:orientation="vertical">
<include layout="@layout/empty_view"/>
<include layout="@layout/progress_bar"/>
<com.application.custom.CustomListView
android:id="@+id/main_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
更新:
activity 只是加载片段:
HomeFrag homeFragment = new HomeFrag();
getSupportFragmentManager().beginTransaction()
.add(R.id.homelist_fragment_container, homeFragment, "home_fragment")
.commit();
这里我们使用add
添加片段,没有检查片段是否存在。这导致了具有相同资源 ID 的多个视图层次结构。
添加以下检查以查看片段是否已存在,围绕添加片段代码修复此问题:
if (getSupportFragmentManager().findFragmentByTag("my_frag_tag") == null) {
//add fragment with tag "my_frag_tag"
HomeFrag homeFragment = new HomeFrag();
getSupportFragmentManager().beginTransaction()
.add(R.id.homelist_fragment_container, homeFragment, "my_frag_tag")
.commit();
}
这也确保了在不需要创建片段时不会创建片段(与 replace
不同)。