2017-09-11 23 views
-1

您好我在这行的一个问题,我不知道为什么无法加载片段

FragmentManager manager = getFragmentManager(); 
     FragmentTransaction trans = manager.beginTransaction(); 
     trans.replace(R.id.containerr,new FragmentLastComments()); 
     trans.commit(); 

XML

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/containerr" 
    android:layoutDirection="ltr"> 

</RelativeLayout> 

片段XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layoutDirection="ltr"> 


    <LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginTop="55dp" 
     android:orientation="vertical"> 


     <TextView 
      android:id="@+id/txt_lastcomments" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:textSize="20dp" 
      android:layout_marginBottom="10dp" 
      android:text="last comments" /> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginRight="5dp" 
      android:layout_marginLeft="5dp" 
      android:id="@+id/rv_lastcomments"/> 


    </LinearLayout> 




</RelativeLayout> 

该行没有工作

trans.replace(R.id.containerr,new FragmentLastComments()); 

当我删除此行,我没有得到任何的问题,但是当我使用这条线我得到的问题,我觉得这里的问题

R.id.containerr 

我希望有人能帮助我解决这个

+0

u能张贴的错误? – Raghavendra

+0

试试解决方案https://stackoverflow.com/questions/14810348/android-fragment-replace-doesnt-replace-content-puts-it-on-top – Raghavendra

+0

https://i.stack.imgur.com/avPHl。 png – ShWaEkI

回答

0

尝试这种方式,

FragmentManager mFragmentManager=getFragmentManager(); 
Fragment mfragment = new FragmentLastComments(); 
mFragmentManager 
     .beginTransaction() 
     .replace(R.id.rl_main_new, fragmentDash, "tag") 
     .commit(); 

我希望这可以帮助你。

+0

不工作我认为这里的问题R.id.containerr – ShWaEkI

0
FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.frame_aaron, fragment); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 
+0

不工作我认为这里的问题R.id.containerr – ShWaEkI

+0

首先你应该检查你的容器id.Thai id和布局id相同。 –

0

把你的片段像这样的FrameLayout里面:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/containerr" 
android:layoutDirection="ltr"> 

        <FrameLayout 
         android:id="@+id/fragment_container" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"/> 

</RelativeLayout> 

,改变你的片段方法与此:

FragmentManager manager = getFragmentManager(); 
    FragmentTransaction trans = manager.beginTransaction(); 
    trans.replace(R.id.fragment_container,new FragmentLastComments()); 
    trans.commit();