RecyclerView条目不能填充的问题

来源:互联网 发布:陕西网络创新研究院 编辑:程序博客网 时间:2024/05/17 04:55

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上的时候就保持了我们自己设置的属性。

结束语

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

原创粉丝点击