【问题标题】:Dynamically adding fragments with xml layout to GridLayout is not workng将带有 xml 布局的片段动态添加到网格布局不起作用
【发布时间】:2013-04-20 01:11:57
【问题描述】:

在我们的应用程序中,我们尝试将片段动态添加到 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>

片段 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;
}

向布局添加片段

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 中指定大小时,问题就出现了,在这种情况下,我得到一个空白屏幕。

感谢您的想法、建议和沉思。 谢谢, 贾里德

【问题讨论】:

    标签: android android-layout android-fragments android-gridlayout


    【解决方案1】:

    这是很晚了,但如果有机会,这可能仍然有用。问题是您在动态设置新的 LayoutParams 时覆盖了 XML 布局参数。 IE,当你这样做时:

     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 中的宽度/高度。

    【讨论】:

    • 这实际上是我们所做的。我会给你选择的答案,因为你花时间写出来,而且很清楚。
    猜你喜欢
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多