RecyclerView条目不能填充的问题

本文探讨了RecyclerView在使用LinearLayout作为根布局时无法正确填充的问题,并提出了两种解决方案:一是更改根布局为RelativeLayout;二是调整inflate方法参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RecyclerView的Item条目不能填充的问题

问题描述:

这里写图片描述

发现问题是是用这种方式去填充布局不能填充recyclerview,这个时候Item的根布局是LinearLayout:

        View childView = mLayoutInflater.inflate(R.layout.item_member_integral_explain, null,false);

方案1.

这个时候保持填充的代码不变化,去修改根布局为RelativeLayout发现可以填充。

方案2.

View childView = mLayoutInflater.inflate(R.layout.item_member_integral_explain, parent,false);//修改填充代码为parent

这样也是可以填充满的,网上多数说的都是该方案。

源码部分:

  // Temp is the root view that was found in the xml
                    final View temp = createViewFromTag(root, name, inflaterContext, attrs);//这里是拿到Item的rootview。

                    ViewGroup.LayoutParams params = null;
                //这里是需要看我们是否传入了root,如果没有传入的话下面都不会走了,直接返回了这个item的根view了
                    if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);//获取根view的参数
                        if (!attachToRoot) {//在设置参数进去
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }

                    if (DEBUG) {
                        System.out.println("-----> start inflating children");
                    }

                    // Inflate all children under temp against its context.
                    rInflateChildren(parser, temp, attrs, true);

                    if (DEBUG) {
                        System.out.println("-----> done inflating children");
                    }

                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now. 
                  //将我们的item加入到root上。
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    //直接把我们的Item的view返回了。
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }
                return result;

上面是填充Item的部分,如果我们传入的inflate方法第二个参数是null的话其实是没有给temp设置参数的,而是直接把temp赋值给了result并返回。

推测结论:

方案1能解决该问题,应该是LinearLayout和RelativeLayout两个view的不同特性导致,这里需要去了解两个view的区别了。

方案2解决问题,是因为对item的根view进行了设置,所以跟view的参数是已经生效了,所以在加入到root上的时候就保持了我们自己设置的属性。

结束语

希望了解这块的同学能提供一些完成的解答。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值