2014-07-07 39 views
0

我想要替换Fragment1内的Fragment2。但是它带来了空白的布局。我究竟做错了什么?更改另一个片段内的片段

MainActivity.java

public class MainActivity extends FragmentActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    FragmentManager fm = getSupportFragmentManager(); 
    Fragment1 fragment = (Fragment1) fm.findFragmentById(R.id.fragment_content); 

    if (fragment == null) { 

     FragmentTransaction ft = fm.beginTransaction(); 
     ft.add(R.id.fragment_content, new Fragment1()); 
     ft.commit(); 
    } 
} 
} 

Fragment1.java

public class Fragment1 extends Fragment{ 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment1, container, false); 

    Button btn = (Button) view.findViewById(R.id.button1); 

    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      FragmentManager fm = getFragmentManager(); 

       if (fm != null) { 
        FragmentTransaction ft = fm.beginTransaction(); 
        ft.replace(R.id.fragment_content, new Fragment2()); 
        ft.commit(); 
       } 
     } 
    }); 

    return view; 
} 
} 

Fragment2.java

public class Fragment2 extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container,Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment2, container, false); 

    TextView txt = (TextView) view.findViewById(R.id.textView1); 

    return super.onCreateView(inflater, container, savedInstanceState); 
} 
} 
+0

ChildFragmentManager是要走的路。 – ElDuderino

回答

1

尝试改变的onCreateView(...)return type在第二把android:onClick="selectFrag"Fragment

代替

@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container,Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment2, container, false); 

    TextView txt = (TextView) view.findViewById(R.id.textView1); 

    return super.onCreateView(inflater, container, savedInstanceState); 
} 

回报view为您分配inflated layoutview

@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container,Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment2, container, false); 

    TextView txt = (TextView) view.findViewById(R.id.textView1); 

    return view ; 
} 
-1

你能解决你的问题,这些步骤如下:

1)添加一个方法在Fragment1命名changefragment()MainActivity

public static void changefragment() 
{ 
FragmentTransaction ft = fm.beginTransaction(); 
       ft.replace(R.id.fragment_content, new Fragment2()); 
       ft.commit(); 

} 

2)内部的按钮click event做到这一点

MainActivity ma=new MainActivity(); 
ma.changefragment(); 
+1

你永远不想自己调用Activity的构造函数。您应该使用'getActivity()'并将其转换为'MainActivity'。 –

0
if (fragment == null) { 

    FragmentTransaction ft = fm.beginTransaction(); 
    ft.add(R.id.fragment_content, new Fragment1()); 
    ft.commit(); 
} 

看到这个问题了吗?你在哪里引用Fragment2?

我会做一个区分功能:

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(android.support.v4.app.FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      Fragment fragment = new Fragment(); 
      switch (position) { 
      case 0: 
       return fragment = new Fragment1(); 
      case 1: 
       return fragment = new Fragment2(); 
      case 2: 
       return fragment = new Fragment3(); 
      default: 
       break; 

      } 
      return fragment; 
     } 


    @Override 
    public int getCount() { 

     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
     case 0: 
      return getString(R.string.first).toUpperCase(l); 
     case 1: 
      return getString(R.string.second).toUpperCase(l); 
     case 2: 
      return getString(R.string.third).toUpperCase(l); 
     } 
     return null; 
    } 
} 

我希望这有助于。我也会检查谷歌示例代码,它有助于理解片段。 http://developer.android.com/training/basics/fragments/creating.html

0

我有四个选项卡。而我做这样的..

public class MainActivity extends Activity 
{ 
    private final String TAG = "Main"; 

    public LinearLayout Tab1,Tab2,Tab3,Tab4; 

    public ImageView img1,img2,img3,img4; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.practicing); 

     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 

     init(); 
    } 

    private void init() 
    { 
     Tab1 = (LinearLayout) findViewById(R.id.tab1); 

     Tab2 = (LinearLayout) findViewById(R.id.tab2); 

     Tab3 = (LinearLayout) findViewById(R.id.tab3); 

     Tab4 = (LinearLayout) findViewById(R.id.tab4); 

     // 
     img1 = (ImageView)findViewById(R.id.tab_img1); 

     img2 = (ImageView)findViewById(R.id.tab_img2); 

     img3 = (ImageView)findViewById(R.id.tab_img3); 

     img4 = (ImageView)findViewById(R.id.tab_img4); 

     getFragmentManager().beginTransaction().replace(R.id.container, new Article()).commit(); 
     //short code to replace a fragment 

    } 

    public void selectFrag(View view) { 

     Fragment fr = null; 

     if (view == findViewById(R.id.tab1)) 
     {   
      img1.setBackgroundResource(R.drawable.articles_on); 

      img2.setBackgroundResource(R.drawable.forum_off); 

      img3.setBackgroundResource(R.drawable.video_off); 

      img4.setBackgroundResource(R.drawable.profile_off); 

      fr = new Article(); 

     } 
     else if(view == findViewById(R.id.tab2)) 
     { 
      img1.setBackgroundResource(R.drawable.articles_off); 

      img2.setBackgroundResource(R.drawable.forum_on); 

      img3.setBackgroundResource(R.drawable.video_off); 

      img4.setBackgroundResource(R.drawable.profile_off); 

      fr = new Forum(); 

     } 
     else if(view == findViewById(R.id.tab3)) 
     { 
      img1.setBackgroundResource(R.drawable.articles_off); 

      img2.setBackgroundResource(R.drawable.forum_off); 

      img3.setBackgroundResource(R.drawable.video_on); 

      img4.setBackgroundResource(R.drawable.profile_off); 

      fr = new Medias(); 
     } 
     else if(view == findViewById(R.id.tab4)) 
     { 
      img1.setBackgroundResource(R.drawable.articles_off); 

      img2.setBackgroundResource(R.drawable.forum_off); 

      img3.setBackgroundResource(R.drawable.video_off); 

      img4.setBackgroundResource(R.drawable.profile_on); 

      fr = new Profile(); 
     } 

     FragmentManager fm = getFragmentManager(); 

     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

     fragmentTransaction.replace(R.id.container, fr); 

     //fragmentTransaction.addToBackStack(null);//MainActivity.TAG);//.addToBackStack(null); 

     fragmentTransaction.commit(); 

    } 
} 

和practicing.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" > 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/bottom" /> 

    <LinearLayout 
     android:id="@+id/bottom" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:id="@+id/tab1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:onClick="selectFrag" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <ImageView 
       android:id="@+id/tab_img1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" 
       android:background="@drawable/articles_on" 
       android:padding="10dp" 
       android:scaleType="center" /> 

      <!-- <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Textview" /> --> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/tab2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:onClick="selectFrag" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <ImageView 
       android:id="@+id/tab_img2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" 
       android:background="@drawable/forum_off" 
       android:padding="10dp" 
       android:scaleType="center" /> 

     <!--  <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Textview" /> --> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/tab3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:onClick="selectFrag" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <ImageView 
       android:id="@+id/tab_img3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" 
       android:background="@drawable/video_off" 
       android:padding="10dp" 
       android:scaleType="fitXY" /> 

     <!--  <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Textview" /> --> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/tab4" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:onClick="selectFrag" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <ImageView 
       android:id="@+id/tab_img4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="10dp" 
       android:background="@drawable/profile_off" 
       android:padding="10dp" 
       android:scaleType="fitXY" /> 

     <!--  <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Textview" /> --> 
     </LinearLayout> 
    </LinearLayout> 

</RelativeLayout> 

我已经在lineaLayout持有一个标签

+0

FragmentActivity表现得一样。只需要将getFragmentManager()替换为getSupportFragmentManager(),然后再将其替换为支持 – Nepster