Android 片段在OnGlobalLayoutListener()之后膨胀视图两次

Android 片段在OnGlobalLayoutListener()之后膨胀视图两次,android,fragment,measure,Android,Fragment,Measure,我正在我的应用程序中使用主/细节流。ItemDetailFragment膨胀test_grid.xml。因为我想让视图完全适合片段,所以我在onViewCreated中插入了一个OnGlobalYoutListener(),以测量片段并计算视图的完美大小。到目前为止还不错,但是R.layout中的test_grid.xml使用默认视图大小膨胀,然后突然膨胀我的计算大小,这当然不是我计划要发生的 我尝试为我的rootView和Gridlayout输入了onViewCreated(),但无法使其可见

我正在我的应用程序中使用主/细节流。ItemDetailFragment膨胀test_grid.xml。因为我想让视图完全适合片段,所以我在onViewCreated中插入了一个OnGlobalYoutListener(),以测量片段并计算视图的完美大小。到目前为止还不错,但是R.layout中的test_grid.xml使用默认视图大小膨胀,然后突然膨胀我的计算大小,这当然不是我计划要发生的

我尝试为我的rootView和Gridlayout输入了onViewCreated(),但无法使其可见。所有这些麻烦只是为了防止以编程方式创建整个布局,它由42个单元组成。这似乎是更好的办法,只是我不能通过这个问题。有什么建议吗

test_grid.xml:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_test_grid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnCount="6"
    android:orientation="horizontal"
    android:rowCount="7"
    android:visibility="visible"
     >

    <Space
        android:id="@+id/space_6"
        android:layout_width="0dp"
        android:layout_height="0dp" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_gravity="fill"
        android:gravity="center_vertical|center_horizontal"
         />
    <EditText
        android:id="@+id/et_name"
        android:layout_gravity="fill"
        android:gravity="center_vertical|center_horizontal"
        />
... more views...
       <Space
        android:id="@+id/space_35"
        android:layout_width="0dp" />
</GridLayout>
正如您所看到的,我试图使视图在代码的不同位置可见或消失,但没有一个是可行的解决方案

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.Toast;

public class ItemDetailFragment extends Fragment {

    int fragmentWidth;
    int fragmentHeigth;
    int bestRowHeight;
    int bestSpaceWidth;
    int bestViewWidth;
    GridLayout mGrid;
    int view_show;

    public ItemDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.test_grid, container,
                false);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        final View rootView = getActivity().findViewById(R.id.my_test_grid);
        // mGrid = (GridLayout) rootView.findViewById(R.id.my_test_grid);
        // mGrid.setVisibility(View.GONE);

        rootView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

            @SuppressLint("NewApi")
        public void onGlobalLayout() {
             rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                 fragmentHeigth = rootView.getHeight();
                 fragmentWidth = rootView.getWidth();
                 bestRowHeight = fragmentHeigth / 7;
             bestSpaceWidth = (fragmentWidth / 100) * 10;
             bestViewWidth = (fragmentWidth / 100) * 20;

                // change views here!!!
                View sp6 = rootView.findViewById(R.id.space_6);
                ((GridLayout) rootView).removeView(sp6);
                View sp35 = rootView.findViewById(R.id.space_35);
                ((GridLayout) rootView).removeView(sp35);
                setRowsHeight(sp6, bestRowHeight);
                setColsWidth(sp35, bestSpaceWidth);
                ((GridLayout) rootView).addView(sp6, 5);
                // mGrid = (GridLayout)
                // rootView.findViewById(R.id.my_test_grid);
                // mGrid.setVisibility(View.VISIBLE);
            }
});

super.onViewCreated(view, savedInstanceState);
// mGrid = (GridLayout) rootView.findViewById(R.id.my_test_grid);
// mGrid.setVisibility(View.VISIBLE);

}

    private void setColsWidth(View v, int width) {
        LayoutParams lp;
        lp = v.getLayoutParams();
        lp.width = width;
        v.setLayoutParams(lp);
    }

    private void setRowsHeight(View v, int height) {
        LayoutParams lp;
        lp = v.getLayoutParams();
        lp.height = height;
        v.setLayoutParams(lp);
    }

    public void setWidthHeight(View v, int width, int height) {
        LayoutParams lp;
        lp = v.getLayoutParams();
        lp.width = width;
        lp.height = height;
        v.setLayoutParams(lp);
    }

}