更改另一个片段内的片段

时间:2014-07-07 13:47:56

标签: android fragment

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

4 个答案:

答案 0 :(得分:1)

尝试在第二个return type中更改onCreateView(...)的{​​{1}}。

而不是

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 layout

view

答案 1 :(得分: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;  
    }  
}  

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

答案 2 :(得分: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();   

    }
}

和practice.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中

答案 3 :(得分:-1)

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

1)在changefragment()

中添加一个名为MainActivity的方法
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();