【问题标题】:Android Fragment HeightAndroid 片段高度
【发布时间】:2019-06-26 03:33:42
【问题描述】:

我有一个片段,它以编程方式放置在 MainActivity 上的 FrameLayout 中。在片段中,我需要一个 GridView。但它将固定 9 行和 3 列。不会有卷轴。所以,我放置了 RecyclerView 并创建了自己的 LayoutManager,它从 GridLayoutManager 扩展而来。

@Override
public boolean canScrollVertically() {
    return false;
}

这部分代码提供了阻止滚动。 我需要该 FrameLayout 的 Height 参数并将其从 Fragment 发送到 RecyclerView 适配器。因为,我计划获取尺寸,并将其分成 9,然后将新的高度应用于每个项目。

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

        View view = inflater.inflate(R.layout.fragment_catalog_list, container, false);

        // Set the adapter
        if (view instanceof RecyclerView) {
            Context context = view.getContext();

            RecyclerView recyclerView = (RecyclerView) view;

            DisplayMetrics displaymetrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int height = displaymetrics.heightPixels;

            recyclerView.setLayoutManager(new NonScrollGridLayoutManager(context, columnCount));
            recyclerView.setAdapter(new CatalogRecyclerViewAdapter(DummyContent.ITEMS, catalogListener, height - 200));
        }
        return view;
}

正如您在代码中看到的,我从窗口度量中获得了高度大小。 不是来自 Fragment 的布局。

在 RecyclerViewAdapter 中,我计算高度

@Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_catalog, parent, false);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = layoutHeight / 9;
        view.setLayoutParams(params);
        return new ViewHolder(view);
}

我正在尝试从 Fragment 的布局中获取高度值。但它总是返回 0。我该怎么办?我期待听到您的回复。

【问题讨论】:

    标签: android layout android-recyclerview fragment


    【解决方案1】:

    onCreateView() 太早了。您可以将 ViewTreeObserver 附加到 Fragment 的布局,并在其具有非零高度和宽度值时捕获该值。我相信您可以为此使用ViewTreeObserver

        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if(view.getHeight() > 0 && view.getWidth() > 0) {
                    //cache values
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        });
    

    【讨论】:

    • 拯救了我的一天。谢谢
    【解决方案2】:

    如果你打电话

    yourLayout.getHeight();
    

    onCreate()onStart()onResume()中,大多数情况下会返回0。

    在其中一种方法中获取布局高度的更好方法是:

    int height;
    
    yourLayout.post( new Runnable() {
            @Override
            public void run() {
                height = yourLayout.getHeight();
            }
        });
    

    所有视图都有一个 Handler 对象,这样您就可以使用 Layout 的 Handler(即 View 类的子级)。

    【讨论】:

      猜你喜欢
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2012-04-08
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多