通胀后,Android Fragment没有出现

时间:2017-07-19 03:17:43

标签: java android android-fragments

我有一个片段,我想在活动之上显示。这是我现在正在尝试的代码......

MainActivity.class

FragmentManager fragmentManager = getSupportFragmentManager();
DirectionsFragment directionsFragment = new DirectionsFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragmentTransaction.add(R.id.directions_fragment_framelayout, directionsFragment);
fragmentTransaction.commit();

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    <!-- irrelevant parameters -->
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/directions_fragment_framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <com.mapbox.mapboxsdk.maps.MapView>
        <!-- A BUNCH OF CHILDREN -->
    </com.mapbox.mapboxsdk.maps.MapView>
</RelativeLayout>

DirectionsFragment.class

public class DirectionsFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_directions, container, false);

        return viewGroup;
    }
}

fragment_directions.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    <!-- irrelevant params -->
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/directions_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        <!-- irrelevant params -->
    />
</android.support.constraint.ConstraintLayout>

活动本身运行良好;当我触发显示的片段时,它会通过FragmentTransaction和片段视图充满通胀(使用日志和断点确认)。由于片段被添加到FrameLayout,为什么片段不显示?提前谢谢!

1 个答案:

答案 0 :(得分:1)

我认为这会有所帮助

尝试在 activity_main.xml

中交换 FrameLayout MapView

您的代码工作正常,但它在地图视图后面,这就是为什么它没有显示。

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        <!-- irrelevant parameters -->
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


        <com.mapbox.mapboxsdk.maps.MapView>
            <!-- A BUNCH OF CHILDREN -->
        </com.mapbox.mapboxsdk.maps.MapView>

        <FrameLayout
            android:id="@+id/directions_fragment_framelayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>

</RelativeLayout>

在片段布局中添加背景颜色,因为已加载片段但透明布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:background="@android:color/white"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/directions_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        <!-- irrelevant params -->
    />
</android.support.constraint.ConstraintLayout>