(未解决)反馈学习 - View的测量过程

文章目录

一 出现的问题

项目实践-RecyclerView(二) - 卡片侧滑中,我遇到一个问题,自定义LayoutManager的在onLayoutChildren()方法中,得到的高度一直都是0。我发现原因在于在MainActivity中FrameLayout的height设置为wrap_content。如果设置为match_parent即可正常显示。

我的布局情况如下:
MainActivity-layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    
    <FrameLayout
        android:id="@+id/fragment_placeholder"
        android:layout_gravity="top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"/>

</android.support.design.widget.CoordinatorLayout>

在Fragment中的布局为:
(如果RecyclerView改为wrap_content,也会出现无法显示的情况。)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

	<android.support.v7.widget.RecyclerView
	    android:id="@+id/ExploreFragment_RecyclerView"
	    android:layout_margin="20dp"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"/>
</LinearLayout>

RecyclerView的Item布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="230dp"
            android:src="@drawable/gakki"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:text="收藏夹"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:text="@string/gakki"
            />
    </LinearLayout>
</android.support.v7.widget.CardView>

在这样的情况下,自定义LayoutManager中的onLayouChildren()方法如下

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        //定义竖直方向的偏移量
        int offsetY = 0;
        Log.d(TAG, "onLayoutChildren: ItemCount is: " + getItemCount());
        for (int i = 0; i < getItemCount(); i++) {
            View view = recycler.getViewForPosition(i);
            addView(view);
            measureChildWithMargins(view, 0, 0);
            int width = getDecoratedMeasuredWidth(view);
            int height = getDecoratedMeasuredHeight(view);
            Log.d(TAG, "onLayoutChildren: the offsetY is " + offsetY + " width is " + width + " height is " + height);
            layoutDecorated(view, 0, offsetY, width, offsetY + height);
            offsetY += height;
        }
    }

在item的xml布局为match_parent 或者wrap_content的时候

...: onLayoutChildren: ItemCount is: 2
...:  onLayoutChildren: the offsetY is 0 width is 720 height is 0
...: onLayoutChildren: the offsetY is 0 width is 720 height is 0

如果在item的xml布局将根布局的height设置为400dp,log如下

...: onLayoutChildren: the offsetY is 0 width is 660 height is 600
...: onLayoutChildren: the offsetY is 600 width is 660 height is 600

两种情况下,布局依然是一片空白。

而如果使用官方提供的布局LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);就能正确显示。

这让我感到非常的困惑。也引起了我的好奇。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值