从Activity切换到Fragment

时间:2016-06-28 11:36:00

标签: android android-fragments

我在Activity中使用Fragments时遇到问题。我想从活动中加入一个片段,我非常接近这样做。这是我的活动:

Utils u;
Fragment fragment;
FrameLayout frameLayout;
Button btn;

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

    btn = (Button)findViewById(R.id.galleryID);
    frameLayout = (FrameLayout)findViewById(R.id.fragmentContainer);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                fragment = new GalleryFragment();
                Utils.fragmentManager(frameLayout.getId(), fragment, MainActivity.this);
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    });

Utils:

public class Utils {
public static void fragmentManager(Integer contentId, Fragment fragment, Activity activity) {
    try {
        FragmentManager fm = activity.getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(contentId, fragment);
        fragmentTransaction.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

GalleryFragment:

 public class GalleryFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    // Defines the xml file for the fragment
    return inflater.inflate(R.layout.fragment_galleryfragment, parent, false);
}

// This event is triggered soon after onCreateView().
// Any view setup should occur here.  E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // Setup any handles to view objects here
    // EditText etFoo = (EditText) view.findViewById(R.id.etFoo);
}

}

我的xmls activity_main:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:baselineAligned="false"
 android:orientation="horizontal" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:id="@+id/fragmentContainer"
    android:layout_centerHorizontal="true">

</FrameLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/galleryID"
    android:text="gallery"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

fragment_galleryfragment.xml:

  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/ok">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

 </LinearLayout>

当我点击GALLERY按钮时,画廊的布局会出现在屏幕上。但activity_main布局保持原样。我想切换布局,比如从活动A切换到活动B.任何人都可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

尝试使用

FragmentManager fm = activity.getSupportFragmentManager();
//instead of this
FragmentManager fm = activity.getFragmentManager();

其余代码看起来是正确的。

你应该使用

  

getSupportFragmentManager()

如果您正在使用

  

android.support.v4.app.FragmentManager

如果你正在使用

  

android.app.FragmentManager

然后你应该使用

  

getFragmentManager()

答案 1 :(得分:0)

像这样更改您的activity_main

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:baselineAligned="false"
 android:orientation="horizontal" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/galleryID"
    android:text="gallery"
    android:layout_centerHorizontal="true"/>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:id="@+id/fragmentContainer"
    android:layout_centerHorizontal="true">

</FrameLayout>

</RelativeLayout>

在fragment_galleryfragment.xml中,让您的LinearLayout可点击,如果你的ok drawable没有完全覆盖你的片段,则更改它的背景。

答案 2 :(得分:0)

如果您想要替换整个活动的布局,只需将按钮放在framelayout中,您的framelayout将替换为您的片段布局:

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:baselineAligned="false"
   android:orientation="horizontal" >

   <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:id="@+id/fragmentContainer"
        android:layout_centerHorizontal="true">


              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/galleryID"
                  android:text="gallery"
                  android:layout_centerHorizontal="true"/>

    </FrameLayout>


  </RelativeLayout>