繁体   English   中英

以编程方式将布局添加到片段

[英]Add layouts to a fragment programmaticcaly

简介根据用户在导航抽屉中的选择,我希望有不同的片段。 对于导航抽屉中的每个项目,应调用一个不同的站点。 我想通过片段来实现。 通过在onCreate方法中执行以下操作,将首先显示主要片段

if ( savedInstanceState == null ) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment fragment = MainFragment.passList(hashMap);
    ft.replace(R.id.content_frame, new MainFragment());
    ft.commit();
}

如您所见,我正在将数据(哈希图)传递给片段的方法,该方法可以完美地工作。 然后将数据放入字符串数组。 这有效,并且数据也可以在片段的onCreate方法中使用。

这是主要活动的xml

    <android.support.v4.widget.DrawerLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/drawer_layout"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >

 <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <ListView
       android:id="@+id/left_drawer"
       android:layout_width="240dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:choiceMode="singleChoice"
       android:divider="@android:color/darker_gray"
       android:dividerHeight="0.1dp" />

</android.support.v4.widget.DrawerLayout>

这是主要片段的XML

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainRelativeLayout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?android:attr/activatedBackgroundIndicator"
   android:gravity="center_vertical"
   android:paddingRight="10dp"
   >
<ScrollView
        android:id="@+id/mainScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.03" >

        <LinearLayout
            android:id="@+id/mainListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>
    </RelativeLayout>

我现在想要实现的是在滚动视图中显示数据。 但是我有一个问题,就是我该如何填充它以及如何对其进行初始化?

通常,我会去那样创建它们-因为findViewById在Fragment中不起作用(必须由我自己编写方法)

ScrollView sv = new ScrollView(getActivity());
LinearLayout ll = new LinearLayout ( getActivity() );

sv.addView(ll); 
ll.addView(SAMPLETEXTVIEW);

View mainView = inflater
            .inflate(R.layout.main_fragment, container, false);
    return mainView;

但这只是让我空白,没有添加任何实际文本。 我该如何实现? 数据在那里-我知道,因为它是通过System.out.println打印出来的-我只需要一种显示它的方法。 提前谢谢!

编辑

当我那样做

View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 

我只是得到一个IllegalStateException,即ScrollView只能托管一个直接子代...但是我不明白。 linearlayout是唯一的直接子代。 为什么会有问题?

这是完整的onCreateView方法

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 
    source      = new TextView[hashMapSize];


    for (int i = 0; i < hashMapSize; i++) {

        source[i]       = new TextView(getActivity());
source      [i].setText( "Source: "     + sourceArr     [i] );
ll.addView(source[i]

    }
    return rootView;
}

收到IllegalStateException的原因是,您膨胀了R.layout.main_fragment,其中已经包含具有一个直接子级的 ScrollView(在布局xml中声明)。

您不应该使用sv.addView(ll)添加代码。 您需要做的就是像使用findViewById一样获取引用。

删除sv.addView(ll); 你应该没事的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM