經驗解決Fragment被Replace后仍舊可見的問題


經驗解決Fragment被Replace后仍舊可見的問題

 

 

網上問的問題,大多會提到替換了Fragment而發現之前被替換的仍舊顯示在那里。我個人使用android 2.3 +support 開發包,在2.3系統上也出現類似問題。搜了下網上的問題,好像都沒有找到解決方法。之后自己摸索。最后發現其實,對於Fragment的替換 JAVA代碼基本上沒啥,網上都是正確的,比如:

 

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
OrderFragment orderFragment = new OrderFragment();
Bundle args = new Bundle();
args.putInt("card_id", LoginHelper.currentCard.getId());
args.putBoolean("create_order", true);
orderFragment.setArguments(args);
transaction.replace(R.id.layout_shopping1, orderFragment);
//transaction.addToBackStack(null);
transaction.commit();

 

但多數人並沒有意識到,貼出 XML布局文件的重要性:

正確的做法是必須使用FrameLayout作為Fragment被替換的布局容器

例如:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_shopping1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black" >

</FrameLayout>

 

 

不能夠使用比如線性布局LinearLayout 等,否則就會發生看得見的問題。

奉獻點經驗,期望可以幫助到遇到類似問題的開發者。

原文地址:http://www.apkbus.com/android-121899-1-35.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM