2016-06-28 88 views
0

我在活动中使用片段存在问题。我想从活动中进入片段,我非常接近这样做。这里是我的活动:从活动切换到片段

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

}

而且我个XML 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> 

当我点击库按钮画廊的布局出现在屏幕上。但是activity_main布局保留它的位置。我想切换布局,如从活动A切换到活动B.任何人都可以帮忙吗?

+0

你能否解释一下你想做些什么?是否想从片段中开始另一个片段?或从你的活动一旦显示片段,你想显示另一个片段? –

+0

在更换片段时,您正在更改framelayout的内容,因此活动中的其余元素将保留。您必须以编程方式隐藏它们,或者让您的活动仅包含您的framelayout并使用两个片段(最初包含gallery按钮和gallery片段) –

+0

或者您可以使用buttonsetVisibility(false)隐藏按钮一旦按钮按下。 –

回答

0

尝试使用

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

休息你的代码看起来是正确的。

你,如果你使用

android.support.v4.app.FragmentManager

,如果要使用

getSupportFragmentManager()

你正在使用

android.app.FragmentManager

那么你应该使用

getFragmentManager()

+0

无法解析此方法。不给我任何进口。 –

+0

还确保您使用以下内容隐藏活动的其他内容(framelayout除外):setVisibility(View.GONE);或setVisibility(View.INVISIBLE);根据你的使用。 –

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可点击并更改其背景,如果您的可绘制不完全覆盖您的片段。

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>