2017-02-18 56 views
0

我有FragmentDetail.java文件,其中包括recylerview,當觸及recylerview相同的片段FragmentDetail.java被調用時,同樣的過程,當我重複4 5次觸摸兩個同時recyler視圖項目。它不立即顯示後面的片段。相反,它顯示了recyler視圖項目的片段。第一個Fragmet包含recylerview,單擊ProductMenu調用,然後在Product菜單的recyler視圖中單擊FragmentDetail.java調用。所以知道我的問題是什麼按片段沒有得到更換properly.Below是我的代碼: -片段沒有在第二次正確替換android

FirstFragment

list_recycler.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), list_recycler, new ClickListener() { 
      @Override 
      public void onClick(View view, int position) { 
       Fragment fragment = new ProductMenu(); 

       fragmentManager = getActivity().getSupportFragmentManager(); 


       fragmentTransaction = fragmentManager 
         .beginTransaction(); 
       fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out, R.anim.slide_enter, R.anim.slide_exit); 
       fragmentTransaction.replace(R.id.container_body, fragment); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 

     } 

ProductMenu.java

Also contains the same code.In spite of fragment FragmentDetail fragment = new FragmentDetail(); 

FragmentDetail.java

FragmentDetail fragment = new FragmentDetail(); 
       ProductsBean productsBean = arr_related_product.get(position);    

       fragmentManager = getActivity().getSupportFragmentManager(); 
       //final int newBackStackLength = fragmentManager.getBackStackEntryCount() +1; 
       fragmentTransaction = fragmentManager 
         .beginTransaction(); 
       fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out, R.anim.slide_enter, R.anim.slide_exit); 
       fragmentTransaction.replace(R.id.container_body, fragment); 
       fragmentTransaction.commit(); 

回答

0

你是什麼意思由 : 同樣的過程,當我重複4 5次觸摸兩個同時recyler查看項目。 - >?

在一審,我建議做

fragmentTransaction.add(R.id.container_body, fragment); 

,而不是

fragmentTransaction.replace(R.id.container_body, fragment); 

Let me know if it works ,also try to explain your problem little more with help of code 
+0

這意味着,當我繼續點擊recyler視圖中的項目,例如我點擊「A」項,然後「B」然後「A」再次「B」。現在當我點擊後退按鈕時,我的片段不會被替換。 –

+0

@ShivamKapoor,我的片段不替換 - 你可以通過在FragmentDetail.java的構造函數中添加一個日誌來檢查它,如果每次點擊並且顯示日誌,這意味着每次單擊時都會創建碎片。這意味着片段被替換。你能解釋一下你的確切需求和你在哪裏卡住? – DevKRos

+0

其實,我不知道問題發生在哪裏。但輸出看起來像片段沒有得到替換,就好像我從第3片段中得到3片段,它直接跳到第1片段上。並且當跳到第一片時,片段的佈局第3個片段不是第1個片段 –