动态添加带有XML布局的片段到GridLayout中无法正常工作。

9
在我们的应用程序中,我们试图动态地向GridLayout添加片段。空的网格布局在XML中定义,片段的布局也是如此。在运行时,我们检查一些数据,并从中确定要添加到布局中的片段数量以及每个片段要使用的布局。当我们为其生成的视图分配大小时,它可以正常工作,但是如果我们在片段的布局文件中指定大小,则网格布局中将不会显示任何内容。显然,我们可以在创建视图时指定大小,但我们更喜欢在片段的xml布局中这样做,因为这将允许我们利用Android为每个设备选择正确布局的内置系统。
我正在使用支持库片段。如果没有使用支持库GridLayout,那么这可能有所不同。
相关代码和xml如下:
GridLayout XML:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ScrollView
        android:id="@+id/grid_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_fragment"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="8dp"
        android:layout_marginTop="?android:attr/actionBarSize"
        android:overScrollMode="ifContentScrolls" >

        <GridLayout
            android:id="@+id/grid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alignmentMode="alignMargins"
            android:animateLayoutChanges="true"
            android:columnCount="3"
            android:columnOrderPreserved="true"
            android:orientation="horizontal"
            android:overScrollMode="ifContentScrolls"
            android:rowOrderPreserved="true" >
        </GridLayout>
    </ScrollView>
</merge>

一个Fragment XML的示例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:alpha="1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="1.0" />
</RelativeLayout>

Fragment的onCreateView()方法

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view;
    GridLayout.Spec rowSpec = GridLayout.spec(mRowStart, mRowSpan);
    GridLayout.Spec columnSpec;
    GridLayout.LayoutParams childParams;
    if (large) {;
        view = inflater.inflate(R.layout.my_place_large, container, false);
        columnSpec = GridLayout.spec(mColumnStart, 2);
        childParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
        //childParams.width = 200; //If I do this everything works regardless of the layout size
    } else {
        view = inflater.inflate(R.layout.my_place_small, container, false);
        columnSpec = GridLayout.spec(mColumnStart, 1);
        childParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
        //childParams.width = 100; //If I do this everything works regardless of the layout size
    }
    childParams.setMargins(0, 0, 0, 0);
    //childParams.height = 100; //If I do this everything works regardless of the layout size
    view.setLayoutParams(childParams);
    view.setId(ID);
    return view;
}

添加 Fragment 到布局

private void populateGrid() {
    RelativeLayout gridParent = (RelativeLayout) mParentActivity.findViewById(R.id.locations);
    mLocationsGrid = (GridLayout) gridParent.findViewById(R.id.grid);
    nColumns = mLocationsGrid.getColumnCount();
    mAdapter = new MyAdapter(mContext, this, mResolver); //This is how I keep track of the various fragments depending on my app's state
    int nCards = mAdapter.getNumberOfCards();
    FragmentManager fragmentManager = mParentActivity.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    for (int i = 0; i < nCards; ++i) {
        fragmentTransaction.add(mLocationsGrid.getId(), mAdapter.getFragmentAtIndex(i), String.valueOf(i));
    }
    fragmentTransaction.commit();
    mPopulated = true;
}

我认为应该涵盖它。再次强调,如果我取消在onCreateView()中显式设置尺寸的行的注释,它们会正确显示在GridLayout中,因此我知道跟踪片段等所有内容都有效,片段事务也是如此。问题出现在我尝试在片段的xml中指定大小时,此时我得到了一个空白屏幕。
欢迎您提出想法、建议和思考。 谢谢, Jared
1个回答

7
这篇文章可能有些迟到,但如果你仍然需要它的话,我很乐意帮忙。问题在于当你动态设置新的LayoutParams时,你覆盖了XML布局参数。例如,在你这样做的时候:
 view.setLayoutParams(childParams);

这将擦除XML的原始高度和宽度设置。仅仅因为你在代码中留空了childParams的宽度/高度并不意味着没有值被设置。在GridLayout的情况下,它们被设置为undefined

修复方法是首先保存视图现有的LayoutParam的高度/宽度,并在动态创建新的LayoutParam时使用它。示例:

if (large) {;
    view = inflater.inflate(R.layout.my_place_large, container, false);
    ViewGroup.LayoutParams oldParams = view.getLayoutParams();
    columnSpec = GridLayout.spec(mColumnStart, 2);
    childParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
    childParams.width = oldParams.width;
}

那将允许您在应用行/列规范的代码中保留XML中的宽度/高度。

1
这实际上就是我们所做的。不过,既然你花时间写了出来并且表述清晰,我会给你选择的答案。 - Jared

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接