简体   繁体   中英

Multiple view with same resource id in android

I have only one listview in a fragment with other views.

As shown in the view dump below, for some reason, there are two listviews in the hierarchy (with same resource id).

The unpopulated listview (top one) here I think, masks my populated listview.

What is this listview (that is selected in the screenshot) and how can I remove it/find its origin.

我的应用程序的视图层次结构转储

My code for this fragment looks like:

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

UPDATE:

The activity simply loads the fragments:

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

Here we are using add to add the fragment, without checking if the fragment exists. This resulted in multiple view hierarchies with same resource ids.

Adding the following check to see if the fragment already exists, around the add fragment code fixes this:

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

This also ensures that the fragment isn't created when there is no need to create it (unlike replace ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM