【问题标题】:Multiple view with same resource id in androidandroid中具有相同资源ID的多个视图
【发布时间】:2015-09-10 07:57:53
【问题描述】:

我在一个片段中只有一个列表视图和其他视图。

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

我认为这里未填充的列表视图(顶部)掩盖了我填充的列表视图。

这个列表视图是什么(在屏幕截图中选择)以及如何删除它/找到它的来源。

我的这个片段的代码如下:

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

更新:

活动只是加载片段:

    HomeFrag homeFragment = new HomeFrag();
    getSupportFragmentManager().beginTransaction()
            .add(R.id.homelist_fragment_container, homeFragment, "home_fragment")
            .commit();

【问题讨论】:

    标签: android android-layout android-listview android-view


    【解决方案1】:

    这里我们使用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 不同)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 2018-12-22
      • 1970-01-01
      • 2023-03-11
      相关资源
      最近更新 更多