android中具有相同资源id的多个视图

android中具有相同资源id的多个视图,android,android-layout,android-listview,android-view,Android,Android Layout,Android Listview,Android View,在一个片段中,我只有一个listview和其他视图 如下面的视图转储所示,由于某些原因,层次结构中有两个ListView(具有相同的资源id) 我认为,这里未填充的listview(上一个)掩盖了我填充的listview 此listview是什么(在屏幕截图中选中),如何删除/查找其来源 我为这个片段编写的代码如下所示: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

在一个片段中,我只有一个listview和其他视图

如下面的视图转储所示,由于某些原因,层次结构中有两个ListView(具有相同的资源id)

我认为,这里未填充的listview(上一个)掩盖了我填充的listview

此listview是什么(在屏幕截图中选中),如何删除/查找其来源

我为这个片段编写的代码如下所示:

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

这里我们使用
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();
}
这还确保在不需要创建片段时不会创建片段(与
替换
不同)

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();
}