2013-05-20 51 views
19

我有一個製表符+ ViewPager佈局,並且在其中一個選項卡中有一個列表視圖。當我在onclick上替換該列表片段時,我仍然可以看到新片段下的舊片段。請參閱:在新片段下可見的上一個片段

enter image description here

代碼:

FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction transaction = fragmentManager.beginTransaction(); 
HallsInStateFragment hallsForState = new HallsInStateFragment();   
transaction.replace(R.id.container, hallsForState); 
transaction.addToBackStack(null); 
transaction.commit(); 

其中R.id.container是的FrameLayout在視圖中。

+0

試圖給人一種背景顏色包含佈局 –

+1

沒有人可以幫助你,如果你不提供您使用的代碼片段。另外,請不要在Android的前面添加題目標題,底部的標籤就足夠了。 – Luksprog

+2

確保當你應該調用FragmentTransaction.replace()時你沒有調用FragmentTransaction.add()。 – Krylez

回答

6

而不是R.id.container把這樣的片段ID:((ViewGroup)getView().getParent()).getId()。實際上它並沒有替代片段,而是之前的佈局,即FrameLayout。它適用於我,我希望它也能適用於你的情況。

+3

getview方法未定義 –

+2

getId( )沒有定義 –

+0

getView()。getParent())。getId()沒有這樣的方法。請按照鏈接實現:http://stackoverflow.com/questions/16643167/previous-fragment-visible-under-the-new-fragment/42113563#42113563 – taranjeetsapra

3

片段的UI是活動視圖層次結構的一部分。因此,如果您在onCreateView()方法中創建了視圖,則使用ViewGroup容器爲您的佈局充氣。這個容器保持對你的片段視圖的引用。試圖覆蓋onDestroyView()的片段的方法和除去從父所有視圖:

@Override 
public void onDestroyView() { 
    //mContainer.removeAllViews(); 
    ViewGroup mContainer = (ViewGroup) getActivity().findViewById(R.id.container); 
    mContainer.removeAllViews(); 
    super.onDestroyView(); 
} 
+7

片段不應該管理父母,這是管理其孩子的父項責任。 –

+1

它有多諷刺! – lionelmessi

0

在根視圖中添加clickable = true並添加替換當前片段的片段的背景顏色(無論是在XML) 。 它只是解決方法

-1

時,需要從你需要調用removeAllViews()在容器中的onCreateView()您的片段的方法父視圖中刪除所有視圖修復。

下面是代碼:

公共視圖onCreateView(LayoutInflater充氣,容器的ViewGroup, 捆綁savedInstanceState){ container.removeAllViews(); //擴充這個片段的佈局 return inflater.inflate(R.layout.fragment_example,container,false); }

+0

這個問題已經超過3歲了,已經解決了 – SquiresSquire