FragmenManager更换使得叠加 [英] FragmenManager replace makes overlay

查看:90
本文介绍了FragmenManager更换使得叠加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用supportlib V4达到主从流程。

问题: 细节片段的新实例覆盖第一个(创建XML),而不是取代它。

我的动态布局是:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向
    工具:上下文=。TrackListActivity>

    <片段
        机器人:ID =@ + ID / fragmentList
        机器人:名称=pl.com.digita.BikeComputerUi.TrackList.TrackListFragment
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_weight =1/>

    <片段
        机器人:ID =@ + ID / fragmentTrack
        机器人:名称=pl.com.digita.BikeComputerUi.TrackList.TrackInfoFragment
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_weight =2/>

< / LinearLayout中>
 

方法点击后调用:

 私人无效showDetails(长的TrackID){
    查看fragmentContainer = getActivity()findViewById(R.id.fragmentTrack)。
    TrackInfoFragment trackInfoFragment = TrackInfoFragment.newInstance(的TrackID);
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()代替(fragmentContainer.getId(),trackInfoFragment).commit()。

    }
 

解决方案
  

注意::当你通过定义布局XML文件中的片段添加一个片段一个活动的布局,你不能删除片段在运行时。如果您打算换你的片段,并进行用户交互过程中,必须将片段添加到该活动的第一个活动启动时,如在下一课。

这是最后的事情上<一个href="http://developer.android.com/training/basics/fragments/creating.html">http://developer.android.com/training/basics/fragments/creating.html

I'm using supportlib v4 to reach master-detail flow.

Problem: New instance of "details" fragment overlays the first one (xml created) instead replace it.

My Activity layout is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".TrackListActivity" >

    <fragment
        android:id="@+id/fragmentList"
        android:name="pl.com.digita.BikeComputerUi.TrackList.TrackListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragmentTrack"
        android:name="pl.com.digita.BikeComputerUi.TrackList.TrackInfoFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

Method called after click:

private void showDetails(long trackId){
    View fragmentContainer = getActivity().findViewById(R.id.fragmentTrack);
    TrackInfoFragment trackInfoFragment =  TrackInfoFragment.newInstance(trackId);
    FragmentManager fragmentManager =  getFragmentManager();
    fragmentManager.beginTransaction().replace(fragmentContainer.getId(), trackInfoFragment).commit();

    }

解决方案

Note: When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts, as shown in the next lesson.

Which is very last thing on http://developer.android.com/training/basics/fragments/creating.html

这篇关于FragmenManager更换使得叠加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆