Android片段重叠 [英] Android fragments overlap

查看:406
本文介绍了Android片段重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个片段的活动,一个显示一个列表,一个显示点击项目的详细信息。当启动应用程序时,细节部分是静态的,一旦我点击一个应该被替换的项目。问题是旧的片段没有被替换,所以两个视图都是相互重叠的。



我的活动布局是:

 <?xml version =1.0encoding =utf-8?> 

  android:id =@ + id / listFragment
android:layout_width =0dp
android:layout_height =match_parent
android:layout_weight =1
class =com.fragments.FragmentOrderList>
< / fragment>

< fragment
android:id =@ + id / detailFragment
android:layout_width =0dp
android:layout_height =match_parent
android:layout_weight =2
class =com.fragments.FragmentOrderDetails>

< / fragment>



细节片段的布局是:

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


< TextView
android:id =@ + id / tvOrderDetail
android:layout_width =wrap_content
android:layout_height = wrap_content
android:text =测试细节片段视图>
< / TextView>



在上述布局中,我们请参阅我们最初看到的静态文本。我的活动代码替换片段是这样的

  FragmentTransaction transaction = getFragmentManager()。beginTransaction(); 
FragmentOrderDetails newFragment = new FragmentOrderDetails();
newFragment.setArguments(b);
transaction.replace(R.id.detailFragment,newFragment);

transaction.addToBackStack(null);
transaction.commit();

对我来说,它看起来不是替换,而是添加。我必须永远删除旧的片段吗?或者我必须在这里采用不同的方法?在我看来,只有原始片段停留在那里,并在第二,第三,...替换以前的片段被正确地替换,只是静态的一直在那里停留。

解决方案



<$ p $ b> < fragment
android:id =@ + id / detailFragment
android:layout_width =0dp
android:layout_height =match_parent
android:layout_weight =2
class =com.fragments.FragmentOrderDetails>

< / fragment>

使用像 LinearLayout FrameLayout 和一些containerId。
然后以编程方式首先使用containerId将碎片添加到此容器,然后用containerId替换此容器的内容。


I have an activity with two fragments, one to display a list and one to show the details of the clicked item. When starting the app the detail part is something static, once I click an item it should get replaced. The problem is that the old fragment is not being replaced, so both views are on top of each other.

My activity layout is:

<?xml version="1.0" encoding="utf-8"?>

<fragment
    android:id="@+id/listFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="com.fragments.FragmentOrderList" >
</fragment>

<fragment
    android:id="@+id/detailFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    class="com.fragments.FragmentOrderDetails" >

</fragment>

The layout for the detail fragment is:

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


<TextView
    android:id="@+id/tvOrderDetail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="test view of details fragment" >
</TextView>

And in the above layout we see as well the static text we see initially. The code in my activity to replace the fragment is this

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
    FragmentOrderDetails newFragment = new FragmentOrderDetails();
    newFragment.setArguments(b);
    transaction.replace(R.id.detailFragment, newFragment);

    transaction.addToBackStack(null);
    transaction.commit();

To me it looks like it is not a "replace" but rather an "add". Do I have to remove the old fragment always? Or do I have to follow a different approach here? It seems to me that only the original fragment stays there and on the second, third, ... replace the previous fragment is replaced correctly, just the static one stays there at all times.

解决方案

Instead of using in layout xml fragment:

<fragment
    android:id="@+id/detailFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    class="com.fragments.FragmentOrderDetails" >

</fragment>

Use some container like LinearLayout or FrameLayout with some containerId. Then programatically firstly add fragment to this container using containerId, and after that replace content of this container with containerId also.

这篇关于Android片段重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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