2015-04-27 85 views
0

我的佈局有四個片段佈局,如下所示。
現在我只需要訪問兩個或三個片段,具體取決於我從數據庫中選擇的片段。如何只從佈局訪問片段?

如何只訪問所需的碎片?

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <FrameLayout xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" android:layout_height="match_parent" 
     tools:context="com.example.rknikhil.myapplication.fragment_sample"> 
     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical"> 

      <fragment 
       android:id="@+id/list_Fragment" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       class="com.example.rknikhil.myapplication.ProfileFragment" > 
      </fragment> 
      <fragment 
       android:id="@+id/list_Fragment1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       class="com.example.rknikhil.myapplication.Notificationfragment"> 
      </fragment> 
      <fragment 
       android:id="@+id/list_Fragment2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       class="com.example.rknikhil.myapplication.ProfileFragment"> 
      </fragment> 
      <fragment 
       android:id="@+id/list_Fragment3" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       class="com.example.rknikhil.myapplication.Notificationfragment" > 
      </fragment> 
     </LinearLayout> 
    </FrameLayout> 
</ScrollView> 
+0

可以有人建議我如何從我的mainactivity –

回答

0

您可以根據選擇簡單顯示和隱藏片段。 在OnCreateView()方法中使用下面的代碼。即

Fragment f= getFragmentManager().findFragmentById(R.id.list_Fragment); 
Fragment f1= getFragmentManager().findFragmentById(R.id.list_Fragment1); 
Fragment f2= getFragmentManager().findFragmentById(R.id.list_Fragment2); 
f1.getView().setVisibility(View.GONE); 
+0

我正在逐漸f1.getview空異常錯誤()設置一個片段類領域; –

+0

您是否在此代碼之前使用了setContentView()來佈局? –