如何只从布局中访问片段?

时间:2015-04-27 05:20:35

标签: android actionscript-3 android-layout flex flex4

我的布局有四个片段布局,如下所示 现在我只需要访问两个或三个片段,具体取决于我从数据库中选择的内容。

如何只访问所需的片段?

<?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>

1 个答案:

答案 0 :(得分: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);
相关问题