【问题标题】:Changing fragment inside another fragment在另一个片段中更改片段
【发布时间】:2014-07-08 14:08:47
【问题描述】:

我正在尝试替换 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);
}
}

【问题讨论】:

  • ChildFragmentManager 是要走的路。

标签: android fragment


【解决方案1】:

尝试在第二个Fragment 中更改onCreateView(...)return type

而不是

@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 layout 时返回view

@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 ;
}

【讨论】:

    【解决方案2】:
    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;  
        }  
    }  
    

    我希望这会有所帮助。我还会查看 Google 的示例代码,它对理解片段有很大帮助。 http://developer.android.com/training/basics/fragments/creating.html

    【讨论】:

      【解决方案3】:

      我有四个标签。我正在这样做..

      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();   
      
          }
      }
      

      和练习.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>
      

      我已将android:onClick="selectFrag" 放入包含一个选项卡的 lineaLayout 中

      【讨论】:

      • FragmentActivity 的行为与相同。只是你必须用 getSupportFragmentManager() 替换 getFragmentManager() 和另一个地方只需用 support 替换它
      【解决方案4】:

      您可以通过以下步骤解决您的问题:

      1) 在MainActivity 中添加一个名为changefragment() 的方法

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

      2)在您的Fragment1 内部按钮click event 中执行此操作

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

      【讨论】:

      • 你永远不想自己调用 Activity 的构造函数。您应该改用 getActivity() 并将其转换为 MainActivity
      猜你喜欢
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      相关资源
      最近更新 更多